您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關如何在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#中生成自定義圖片了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。