這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)如何在c#中生成自定義圖片,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
新絳網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,新絳網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為新絳上千提供企業(yè)網(wǎng)站建設(shè)服務。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請找那個售后服務好的新絳做網(wǎng)站的公司定做!
using System.Drawing;using System.IO;using System.Drawing.Imaging;using System;namespace treads { ////// 生成略縮圖 /// public class Class2 { private Image srcImage; private string srcFileName= @"X";//獲取圖片的路徑 private string srcFileName1 = @"x";//要保持圖片的新路徑 ////// 回調(diào) /// ///public bool ThumbnailCallback() { return false; } /// /// 保存縮略圖 /// /// /// 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; } } ////// 生成縮略圖并保存 /// /// 縮略圖的寬度 /// 縮略圖的高度 /// 保存的圖像格式 ///縮略圖的Image對象 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(); } } } }
制作網(wǎng)絡下載的略縮圖
////// 制作遠程縮略圖 /// /// 圖片URL /// 新圖路徑 /// 最大寬度 /// 最大高度 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); } ////// 獲取圖片流 /// /// 圖片URL ///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; } } /// /// 裁剪圖片并保存 /// /// 源圖路徑(絕對路徑) /// 縮略圖路徑(絕對路徑) /// 縮略圖寬度 /// 縮略圖高度 /// 裁剪寬度 /// 裁剪高度 /// X軸 /// Y軸 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)) { //設(shè)置高質(zhì)量插值法 g.InterpolationMode = InterpolationMode.HighQualityBicubic; //設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度 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#中生成自定義圖片了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。