c#生成自定義圖片方法代碼實(shí)例
本篇文章給大家?guī)?lái)的內(nèi)容是關(guān)于c# 如何生成自定義圖片?c# 生成自定義圖片方法,有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)你有所幫助。
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> /// 回調(diào) /// </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對(duì)象</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(); } } } }
制作網(wǎng)絡(luò)下載的略縮圖
/// <summary> /// 制作遠(yuǎn)程縮略圖 /// </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">源圖路徑(絕對(duì)路徑)</param> /// <param name="newFileName">縮略圖路徑(絕對(duì)路徑)</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)) { //設(shè)置高質(zhì)量插值法 g.InterpolationMode = InterpolationMode.HighQualityBicubic; //設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度 g.SmoothingMode = SmoothingMode.AntiAlias; g.PixelOffsetMode = PixelOffsetMode.HighQuality; //清空畫(huà)布并以透明背景色填充 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(); } }
以上就是對(duì)c# 如何生成自定義圖片?c# 生成自定義圖片方法的全部介紹,感謝大家對(duì)腳本之家的支持。
相關(guān)文章
WPF利用LiveCharts實(shí)現(xiàn)動(dòng)態(tài)曲線圖繪制
LiveCharts是一個(gè)比較漂亮的WPF圖表控件,在數(shù)據(jù)發(fā)生變化后,還可以設(shè)置相對(duì)于的動(dòng)畫(huà)效果,本文就來(lái)利用LiveCharts繪制簡(jiǎn)單的動(dòng)態(tài)曲線圖吧2023-10-10關(guān)于C#版Nebula客戶端編譯的問(wèn)題
這篇文章主要介紹了C#版Nebula客戶端編譯的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07C# Winform DataGridView數(shù)據(jù)刷新問(wèn)題的解決辦法
DataGridView 是比較常用的表格控件,在 DataGridView 中顯示數(shù)據(jù), 一般使用 dataGridView1.DataSource = 數(shù)據(jù)源,來(lái)綁定數(shù)據(jù),那么如何做到及時(shí)刷新數(shù)據(jù)呢,本文給大家介紹了C# Winform DataGridView數(shù)據(jù)刷新問(wèn)題的解決辦法,需要的朋友可以參考下2024-09-09解析StreamReader與文件亂碼問(wèn)題的解決方法
本篇文章是對(duì)StreamReader與文件亂碼問(wèn)題的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05基于C#編寫(xiě)一個(gè)合并多個(gè)Word文檔的工具
這篇文章主要為大家詳細(xì)介紹了如何使用C#編寫(xiě)一個(gè)小工具,可以實(shí)現(xiàn)把多個(gè)word文檔進(jìn)行合并成一個(gè)word文檔,感興趣的小伙伴可以了解下2024-02-02c#中SqlHelper封裝SqlDataReader的方法
這篇文章主要介紹了c#中SqlHelper封裝SqlDataReader的方法,涉及C#針對(duì)數(shù)據(jù)庫(kù)相關(guān)操作封裝與使用的技巧,需要的朋友可以參考下2015-05-05C#在驗(yàn)證文件共享模式下實(shí)現(xiàn)多線程文件寫(xiě)入
這篇文章主要為大家詳細(xì)介紹了C#在驗(yàn)證文件共享模式下實(shí)現(xiàn)多線程文件寫(xiě)入的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-01-01