您好,登錄后才能下訂單哦!
怎么在c#中生成自定義圖片?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
using System.Drawing;using System.IO;using System.Drawing.Imaging;using System;namespace treads { /// <summary> /// 生成略縮圖 /// </summary> public class Class2 { private Image srcImage; private string srcFileName= @"X";//獲取圖片的路徑 private string srcFileName1 = @"x";//要保持圖片的新路徑 /// <summary> /// 回調 /// </summary> /// <returns></returns> public bool ThumbnailCallback() { return false; } /// <summary> /// 保存縮略圖 /// </summary> /// <param name="Width"></param> /// <param name="Height"></param> public void SaveThumbnailImage(int Width, int Height) { switch (Path.GetExtension(srcFileName).ToLower()) { case ".png": SaveImage(Width, Height, ImageFormat.Png); break; case ".gif": SaveImage(Width, Height, ImageFormat.Gif); break; default: SaveImage(Width, Height, ImageFormat.Jpeg); break; } } /// <summary> /// 生成縮略圖并保存 /// </summary> /// <param name="Width">縮略圖的寬度</param> /// <param name="Height">縮略圖的高度</param> /// <param name="imgformat">保存的圖像格式</param> /// <returns>縮略圖的Image對象</returns> public void SaveImage(int Width, int Height, ImageFormat imgformat) { srcImage = Image.FromFile(srcFileName); if (imgformat != ImageFormat.Gif && (srcImage.Width > Width) || (srcImage.Height > Height)) { Image img; Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback); img = srcImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero); srcImage.Dispose(); img.Save(srcFileName1, imgformat); img.Dispose(); } } } }
制作網絡下載的略縮圖
/// <summary> /// 制作遠程縮略圖 /// </summary> /// <param name="url">圖片URL</param> /// <param name="newFileName">新圖路徑</param> /// <param name="maxWidth">最大寬度</param> /// <param name="maxHeight">最大高度</param> public static void MakeRemoteThumbnailImage(string url, string newFileName, int maxWidth, int maxHeight) { Stream stream = GetRemoteImage(url); if (stream == null) return; Image original = Image.FromStream(stream); stream.Close(); MakeThumbnailImage(original, newFileName, maxWidth, maxHeight); } /// <summary> /// 獲取圖片流 /// </summary> /// <param name="url">圖片URL</param> /// <returns></returns> private static Stream GetRemoteImage(string url) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "GET"; request.ContentLength = 0; request.Timeout = 20000; HttpWebResponse response = null; try { response = (HttpWebResponse)request.GetResponse(); return response.GetResponseStream(); } catch { return null; } } /// <summary> /// 裁剪圖片并保存 /// </summary> /// <param name="fileName">源圖路徑(絕對路徑)</param> /// <param name="newFileName">縮略圖路徑(絕對路徑)</param> /// <param name="maxWidth">縮略圖寬度</param> /// <param name="maxHeight">縮略圖高度</param> /// <param name="cropWidth">裁剪寬度</param> /// <param name="cropHeight">裁剪高度</param> /// <param name="X">X軸</param> /// <param name="Y">Y軸</param> public static bool MakeThumbnailImage(string fileName, string newFileName, int maxWidth, int maxHeight, int cropWidth, int cropHeight, int X, int Y) { byte[] imageBytes = File.ReadAllBytes(fileName); Image originalImage = Image.FromStream(new System.IO.MemoryStream(imageBytes)); Bitmap b = new Bitmap(cropWidth, cropHeight); try { using (Graphics g = Graphics.FromImage(b)) { //設置高質量插值法 g.InterpolationMode = InterpolationMode.HighQualityBicubic; //設置高質量,低速度呈現平滑程度 g.SmoothingMode = SmoothingMode.AntiAlias; g.PixelOffsetMode = PixelOffsetMode.HighQuality; //清空畫布并以透明背景色填充 g.Clear(Color.Transparent); //在指定位置并且按指定大小繪制原圖片的指定部分 g.DrawImage(originalImage, new Rectangle(0, 0, cropWidth, cropHeight), X, Y, cropWidth, cropHeight, GraphicsUnit.Pixel); Image displayImage = new Bitmap(b, maxWidth, maxHeight); SaveImage(displayImage, newFileName, GetCodecInfo("image/" + GetFormat(newFileName).ToString().ToLower())); return true; } } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); b.Dispose(); } }
C#是一個簡單、通用、面向對象的編程語言,它由微軟Microsoft開發,繼承了C和C++強大功能,并且去掉了一些它們的復雜特性,C#綜合了VB簡單的可視化操作和C++的高運行效率,以其強大的操作能力、優雅的語法風格、創新的語言特性和便捷的面向組件編程從而成為.NET開發的首選語言,但它不適用于編寫時間急迫或性能非常高的代碼,因為C#缺乏性能極高的應用程序所需要的關鍵功能。
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。