[转][C#]ImageHelper
{ internal static class ImageHelper { public static Bitmap CloneBitmap(Image source) { if (source == null) return null; Bitmap image = new Bitmap(source.Width, source.Height); image.SetResolution(source.HorizontalResolution, source.VerticalResolution); using (Graphics g = Graphics.FromImage(image)) { g.DrawImageUnscaled(source, 0, 0); } return image; // this can throw OutOfMemory when creating a grayscale image from a cloned bitmap // return source.Clone() as Bitmap; } public static void Save(Image image, Stream stream) { Save(image, stream, ImageFormat.Png); } public static void Save(Image image, string fileName, ImageFormat format) { using (FileStream stream = new FileStream(fileName, FileMode.Create)) { Save(image, stream, format); } } public static void Save(Image image, Stream stream, ImageFormat format) { if (image == null) return; if (image is Bitmap) image.Save(stream, format); else if (image is Metafile) { Metafile emf = null; using (Bitmap bmp = new Bitmap(1, 1)) using (Graphics g = Graphics.FromImage(bmp)) { IntPtr hdc = g.GetHdc(); emf = new Metafile(stream, hdc); g.ReleaseHdc(hdc); } using (Graphics g = Graphics.FromImage(emf)) { g.DrawImage(image, 0, 0); } } } public static byte[] Load(string fileName) { if (!String.IsNullOrEmpty(fileName)) return File.ReadAllBytes(fileName); return null; } public static Image Load(byte[] bytes) { if (bytes != null && bytes.Length > 0) { try { return new ImageConverter().ConvertFrom(bytes) as Image; } catch { Bitmap errorBmp = new Bitmap(10, 10); using (Graphics g = Graphics.FromImage(errorBmp)) { g.DrawLine(Pens.Red, 0, 0, 10, 10); g.DrawLine(Pens.Red, 0, 10, 10, 0); } return errorBmp; } } return null; } public static byte[] LoadURL(string url) { if (!String.IsNullOrEmpty(url)) { using (WebClient web = new WebClient()) { return web.DownloadData(url); } } return null; } public static Bitmap GetTransparentBitmap(Image source, float transparency) { if (source == null) return null; ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.Matrix33 = 1 - transparency; ImageAttributes imageAttributes = new ImageAttributes(); imageAttributes.SetColorMatrix( colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); int width = source.Width; int height = source.Height; Bitmap image = new Bitmap(width, height); image.SetResolution(source.HorizontalResolution, source.VerticalResolution); using (Graphics g = Graphics.FromImage(image)) { g.Clear(Color.Transparent); g.DrawImage( source, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel, imageAttributes); } return image; } public static Bitmap GetGrayscaleBitmap(Image source) { Bitmap grayscaleBitmap = new Bitmap(source.Width, source.Height, source.PixelFormat); // Red should be converted to (R*.299)+(G*.587)+(B*.114) // Green should be converted to (R*.299)+(G*.587)+(B*.114) // Blue should be converted to (R*.299)+(G*.587)+(B*.114) // Alpha should stay the same. ColorMatrix grayscaleMatrix = new ColorMatrix(new float[][]{ new float[] {0.299f, 0.299f, 0.299f, 0, 0}, new float[] {0.587f, 0.587f, 0.587f, 0, 0}, new float[] {0.114f, 0.114f, 0.114f, 0, 0}, new float[] { 0, 0, 0, 1, 0}, new float[] { 0, 0, 0, 0, 1}}); ImageAttributes attributes = new ImageAttributes(); attributes.SetColorMatrix(grayscaleMatrix); // Use a Graphics object from the new image using (Graphics graphics = Graphics.FromImage(grayscaleBitmap)) { // Draw the original image using the ImageAttributes we created graphics.DrawImage(source, new Rectangle(0, 0, grayscaleBitmap.Width, grayscaleBitmap.Height), 0, 0, grayscaleBitmap.Width, grayscaleBitmap.Height, GraphicsUnit.Pixel, attributes); } return grayscaleBitmap; } } }
来自:https://github.com/FastReports/FastReport
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2018-01-04 [转]IIS 允许/禁止 目录浏览