将对图片的操作单独写一个内,代码如下:
1 public class DoPic 2 { 3 4 /// <summary> 5 /// 将图片转成二进制流 6 /// </summary> 7 /// <param name="path"></param> 8 /// <returns></returns> 9 public static byte[] SaveImage(String path) 10 { 11 FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); //将图片以文件流的形式进行保存 12 BinaryReader br = new BinaryReader(fs); 13 byte[] imgBytesIn = br.ReadBytes((int)fs.Length); //将流读入到字节数组中 14 return imgBytesIn; 15 } 16 17 18 /// <summary> 19 /// 现实二进制流代表的图片 20 /// </summary> 21 /// <param name="imgBytesIn"></param> 22 public static void ShowImgByByte(byte[] imgBytesIn, string filePathIncludeName) 23 { 24 MemoryStream ms = new MemoryStream(imgBytesIn); 25 26 Bitmap bmp = new Bitmap(ms); 27 bmp.Save(filePathIncludeName, ImageFormat.Bmp); 28 ms.Close(); 29 } 30 31 32 33 34 35 /// <summary> 36 /// 图像缩略图处理,无损压缩图片 37 /// </summary> 38 /// <param name="bytes">图像源数据</param> 39 /// <param name="compression">压缩质量 1-100</param> 40 /// <param name="thumbWidth">缩略图的宽</param> 41 /// <param name="thumbHeight">缩略图的高</param> 42 /// <returns></returns> 43 public static byte[] ConvertToThumbnail(byte[] bytes, int compression = 100, int thumbWidth = 0, int thumbHeight = 0) 44 { 45 byte[] bs = null; 46 47 try 48 { 49 if (bytes != null) 50 { 51 using (MemoryStream ms = new MemoryStream(bytes)) 52 { 53 using (Bitmap srcimg = new Bitmap(ms)) 54 { 55 if (thumbWidth == 0 && thumbHeight == 0) 56 { 57 thumbWidth = srcimg.Width; 58 thumbHeight = srcimg.Height; 59 } 60 using (Bitmap dstimg = new Bitmap(thumbWidth, thumbHeight))//图片压缩质量 61 { 62 //从Bitmap创建一个System.Drawing.Graphics对象,用来绘制高质量的缩小图。 63 using (Graphics gr = Graphics.FromImage(dstimg)) 64 { 65 //把原始图像绘制成上面所设置宽高的缩小图 66 Rectangle rectDestination = new Rectangle(0, 0, thumbWidth, thumbHeight); 67 gr.Clear(Color.WhiteSmoke); 68 gr.CompositingQuality = CompositingQuality.HighQuality; 69 gr.SmoothingMode = SmoothingMode.HighQuality; 70 gr.InterpolationMode = InterpolationMode.HighQualityBicubic; 71 gr.DrawImage(srcimg, rectDestination, 0, 0, srcimg.Width, srcimg.Height, GraphicsUnit.Pixel); 72 73 EncoderParameters ep = new EncoderParameters(1); 74 ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, compression);//设置压缩的比例1-100 75 ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); 76 ImageCodecInfo jpegICIinfo = arrayICI.FirstOrDefault(t => t.FormatID == System.Drawing.Imaging.ImageFormat.Png.Guid); 77 using (MemoryStream dstms = new MemoryStream()) 78 { 79 if (jpegICIinfo != null) 80 { 81 dstimg.Save(dstms, jpegICIinfo, ep); 82 } 83 else 84 { 85 dstimg.Save(dstms, System.Drawing.Imaging.ImageFormat.Png);//保存到内存里 86 } 87 bs = new Byte[dstms.Length]; 88 dstms.Position = 0; 89 dstms.Read(bs, 0, bs.Length); 90 } 91 } 92 } 93 } 94 } 95 } 96 } 97 catch (Exception ex) 98 { 99 //LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("ConvertToThumbnail error.", thumbWidth, " ", thumbHeight)); 100 } 101 return bs; 102 } 103 104 105 }
然后调用:
1 byte[] bys = DoPic.SaveImage(System.AppDomain.CurrentDomain.BaseDirectory + "timg.png"); //得到图片的Byte数组数据 2 3 4 byte[] newPic = DoPic.ConvertToThumbnail(bys, 1, 30, 30); //将图片进行压缩 5 6 7 DoPic.ShowImgByByte(newPic, "E:\a.png"); //重新将新的图片保存