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 }