图像处理
1 public class ImageDraw 2 { 3 public Image NewBitmap(int w, int h, float dpix, float dpiy) 4 { 5 var rt = new Bitmap(w, h); 6 rt.SetResolution(dpix, dpiy); 7 return rt; 8 } 9 10 /// <summary> 得到图像的指定区域. 11 /// </summary> 12 /// <param name="source"></param> 13 /// <param name="rect"></param> 14 /// <returns></returns> 15 public Image GetImageRect(Image source, Rectangle rect) 16 { 17 var rt = NewBitmap(rect.Width, rect.Height, source.HorizontalResolution, source.VerticalResolution); 18 Graphics gh = Graphics.FromImage(rt); 19 gh.DrawImage(source, 0, 0, rect, GraphicsUnit.Pixel); 20 gh.Save(); 21 gh.Dispose(); 22 return rt; 23 } 24 25 /// <summary> 拉伸图像. 26 /// </summary> 27 /// <param name="source"></param> 28 /// <param name="newSize"></param> 29 /// <returns></returns> 30 public Image GetStretchImage(Image source, Size newSize) 31 { 32 var w = source.Width; 33 var h = source.Height; 34 35 var rt = NewBitmap(newSize.Width, newSize.Height, source.HorizontalResolution, source.VerticalResolution); 36 var gh = Graphics.FromImage(rt); 37 38 gh.DrawImage(source, new Rectangle(0, 0, newSize.Width, newSize.Height), new Rectangle(0, 0, w, h), GraphicsUnit.Pixel); 39 40 gh.Save(); 41 gh.Dispose(); 42 43 return rt; 44 } 45 46 /// <summary> 拉伸图像至合适大小,并保持纵横比.. 47 /// </summary> 48 /// <param name="source"></param> 49 /// <param name="newSize"></param> 50 /// <returns></returns> 51 public Image GetAdaptiveImage(Image source, Size newSize) 52 { 53 var sw = source.Width; 54 var sh = source.Height; 55 var tw = newSize.Width; 56 var th = newSize.Height; 57 58 double wb = sw * 1.0 / tw; 59 double hb = sh * 1.0 / th; 60 61 //计算目标图像宽高. 62 if (wb > hb) 63 { 64 //取宽度作为标准 65 th = (int)(sh / wb); 66 } 67 else 68 { 69 tw = (int)(sw / hb); 70 } 71 72 //生成图像 73 return GetStretchImage(source, new Size(tw, th)); 74 } 75 76 /// <summary> 剪裁图像至合适大小. 77 /// </summary> 78 /// <param name="image"></param> 79 /// <param name="size"></param> 80 /// <returns></returns> 81 public Image GetFillImage(Image source, Size newSize) 82 { 83 var sw = source.Width; 84 var sh = source.Height; 85 var tw = newSize.Width; 86 var th = newSize.Height; 87 88 double wb = sw * 1.0 / tw; 89 double hb = sh * 1.0 / th; 90 91 Image rt; 92 Graphics gh; 93 94 //计算目标图像宽高. 95 if (wb > hb) 96 { 97 th = sh; 98 tw = (int)(tw * hb); 99 100 rt = NewBitmap(newSize.Width, newSize.Height, source.HorizontalResolution, source.VerticalResolution); 101 gh = Graphics.FromImage(rt); 102 103 gh.DrawImage(source, new Rectangle(new Point(0, 0), newSize), new Rectangle(new Point((sw - tw) / 2, 0), new Size(tw, th)), GraphicsUnit.Pixel); 104 gh.Dispose(); 105 106 return rt; 107 } 108 else 109 { 110 //计算出原图中要裁剪的尺寸. 111 tw = sw; 112 th = (int)(th * wb); 113 114 rt = NewBitmap(newSize.Width, newSize.Height, source.HorizontalResolution, source.VerticalResolution); 115 gh = Graphics.FromImage(rt); 116 117 gh.DrawImage(source, new Rectangle(0, 0, 100, 100), new Rectangle(30, 30, 100, 100), GraphicsUnit.Pixel); 118 119 gh.DrawImage(source, new Rectangle(new Point(0, 0), newSize), new Rectangle(new Point(0, (sh - th) / 2), new Size(tw, th)), GraphicsUnit.Pixel); 120 gh.Dispose(); 121 122 return rt; 123 } 124 } 125 126 /// <summary> 获取图像数据 127 /// </summary> 128 /// <param name="img"></param> 129 /// <returns></returns> 130 public byte[] GetImageData(Image img) 131 { 132 //将图像转化为32位位图 ( 24位计算色差有些麻烦.^^ 32位,则直接转换为整数.) 133 var bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppPArgb); 134 135 bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution); 136 137 var gh = Graphics.FromImage(bmp); 138 139 gh.DrawImageUnscaled(img, 0, 0); 140 141 gh.Save(); 142 143 img = bmp; 144 145 gh.Dispose(); 146 147 var ms = new MemoryStream(img.Width * img.Height * 4 + 2048); 148 149 img.Save(ms, ImageFormat.Bmp); 150 ms.Flush(); 151 ms.Position = 0; 152 var data = new byte[ms.Length]; 153 ms.Read(data, 0, data.Length); 154 ms.Close(); 155 return data; 156 } 157 158 /// <summary> 保存图片 貌似是0 - 100 159 /// </summary> 160 /// <param name="lm"></param> 161 /// <param name="p"></param> 162 /// <param name="fn"> </param> 163 /// <param name="quality"> </param> 164 public void SaveImageToJpeg(Image lm, string fn, int quality) 165 { 166 System.IO.Directory.CreateDirectory(Path.GetDirectoryName(fn)); 167 168 var sm = new FileStream(fn, FileMode.Create); 169 SaveImageToJpeg(lm, sm, quality); 170 sm.Close(); 171 } 172 173 public void SaveImageToJpeg(Image lm, Stream stream, int quality) 174 { 175 if (quality == 0) 176 { 177 quality = 80; 178 } 179 180 var jgpEncoder = System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders().First(j => j.FormatID == ImageFormat.Jpeg.Guid); 181 182 //参数数组 183 var myEncoderParameters = new System.Drawing.Imaging.EncoderParameters(1); 184 185 //质量 186 var myEncoder = System.Drawing.Imaging.Encoder.Quality; 187 188 //质量包含参数 189 //EncoderParameters paramList = Image.FromFile("c:\\http_imgload.jpg").GetEncoderParameterList(jgpEncoder.Clsid); 190 191 var myEncoderParameter = new System.Drawing.Imaging.EncoderParameter(myEncoder, (Int64)quality); 192 193 myEncoderParameters.Param[0] = myEncoderParameter; 194 195 lm.Save(stream, jgpEncoder, myEncoderParameters); 196 stream.Flush(); 197 } 198 }