c# bitmap的拷贝及一个图像工具类
1 using (Bitmap bmp = new Bitmap(scanImgPath)) 2 { 3 Bitmap bitmap = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format16bppRgb555); 4 using (Graphics draw = Graphics.FromImage(bitmap)) 5 { 6 draw.DrawImage(bmp, 0, 0, bitmap.Width, bitmap.Height); 7 picScanImg.Image = bitmap as Image; 8 } 9 }
1 using System; 2 using System.Collections.Generic; 3 using System.Drawing; 4 using System.Text; 5 6 namespace TJCFinanceWriteOff.BizLogic.Common 7 { 8 public class ImageUtil 9 { 10 /// <summary> 11 /// 图片镜像翻转 12 /// </summary> 13 /// <param name="imagePath">图片路径</param> 14 /// <param name="isCover">是否覆盖</param> 15 /// <returns></returns> 16 public static Image ImageFlip(string imagePath,bool isCover = false) 17 { 18 Bitmap bitmap = Bitmap.FromFile(imagePath) as Bitmap; 19 bitmap.RotateFlip(RotateFlipType.Rotate180FlipY); //图片镜像翻转 20 if (isCover is true) bitmap.Save(imagePath,System.Drawing.Imaging.ImageFormat.Jpeg); 21 return bitmap; 22 } 23 24 /// <summary> 25 /// 图片顺时针旋转180度 26 /// </summary> 27 /// <param name="imagePath">图片路径</param> 28 /// <param name="isCover">是否覆盖</param> 29 /// <returns></returns> 30 public static Image ImageRotate(string imagePath,bool isCover = false) 31 { 32 Bitmap bitmap = Bitmap.FromFile(imagePath) as Bitmap; 33 bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone); //图片顺时针180度 34 if (isCover is true) bitmap.Save(imagePath,System.Drawing.Imaging.ImageFormat.Jpeg); 35 return bitmap; 36 } 37 38 /// <summary> 39 /// 图片等比缩放 40 /// </summary> 41 /// <param name="imagePath"></param> 42 /// <returns></returns> 43 public static Image ImageScaleZoom(string imagePath, double process) 44 { 45 Bitmap bitmap = Bitmap.FromFile(imagePath) as Bitmap; 46 double width = bitmap.Width * process; ;//图片最终的宽 47 double height = bitmap.Height * process;//图片最终的高 48 return bitmap.GetThumbnailImage((int)width, (int)height, () => { return false; }, IntPtr.Zero); 49 } 50 51 public static Image ImageScaleZoom(Image sourceImage, double process) 52 { 53 double width = sourceImage.Width * process; ;//图片最终的宽 54 double height = sourceImage.Height * process;//图片最终的高 55 try 56 { 57 System.Drawing.Imaging.ImageFormat format = sourceImage.RawFormat; 58 Bitmap targetPicture = new Bitmap((int)width, (int)height); 59 Graphics g = Graphics.FromImage(targetPicture); 60 g.DrawImage(sourceImage, 0, 0, (int)width, (int)height); 61 sourceImage.Dispose(); 62 return targetPicture; 63 } 64 catch (Exception ex) 65 { 66 67 } 68 return null; 69 } 70 71 public static Image ImageAssignZoom(Image sourceImage, int targetWidth, int targetHeight) 72 { 73 int width;//图片最终的宽 74 int height;//图片最终的高 75 try 76 { 77 System.Drawing.Imaging.ImageFormat format = sourceImage.RawFormat; 78 Bitmap targetPicture = new Bitmap(targetWidth, targetHeight); 79 Graphics g = Graphics.FromImage(targetPicture); 80 81 if (sourceImage.Width > targetWidth && sourceImage.Height <= targetHeight) 82 { 83 width = targetWidth; 84 height = (width * sourceImage.Height) / sourceImage.Width; //噶 85 } 86 else if (sourceImage.Width <= targetWidth && sourceImage.Height > targetHeight) 87 { 88 height = targetHeight; 89 width = (height * sourceImage.Width) / sourceImage.Height; 90 } 91 else if (sourceImage.Width <= targetWidth && sourceImage.Height <= targetHeight) 92 { 93 width = sourceImage.Width; 94 height = sourceImage.Height; 95 } 96 else 97 { 98 width = targetWidth; 99 height = (width * sourceImage.Height) / sourceImage.Width; 100 if (height > targetHeight) 101 { 102 height = targetHeight; 103 width = (height * sourceImage.Width) / sourceImage.Height; 104 } 105 } 106 g.DrawImage(sourceImage, 0, 0, width, height); 107 sourceImage.Dispose(); 108 109 return targetPicture; 110 } 111 catch (Exception ex) 112 { 113 114 } 115 return null; 116 } 117 } 118 }
作者:奇
出处:https://www.cnblogs.com/fanqisoft/p/11757053.html
版权:本作品采用「本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。」许可协议进行许可。
分类:
开发填坑
如果文章内容对您有所帮助,欢迎赞赏.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!