C#中对图片的操作主要是通过System.Drawing.Image等类进行。
一、将图片转换为字节流
/// <summary> /// 图片处理帮助类 /// </summary> public static class PicProcessHelper { /// <summary> /// 将图片转换为指定的字节流 /// </summary> /// <param name="filePath">图片路径</param> /// <returns>指定的字节流</returns> public static byte[] ConvertToByte(String filePath) { var m = new System.IO.MemoryStream(); var bp = new System.Drawing.Bitmap(filePath); bp.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg); //将此图像以指定的格式保存到指定的流中。 byte[] imgByte = m.GetBuffer(); //从内存缓冲区中读取 return imgByte; } }
二、将字节流转换回图片
/// <summary> /// 根据字节流返回Image类型 /// </summary> /// <param name="streamByte"></param> /// <returns></returns> public static Image ReturnImage(byte[] streamByte) { System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte); Image img = Image.FromStream(ms); return img; }
三、将Image对象转换为字节流
//将Image转换成流数据,并保存为byte[] public static byte[] PhotoImageInsert(System.Drawing.Image imgPhoto) { MemoryStream mstream = new MemoryStream(); imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp); byte[] byData = new Byte[mstream.Length]; mstream.Position = 0; mstream.Read(byData, 0, byData.Length); mstream.Close(); return byData; }
四、保存图片
var oldFilename = @"E:\环境部署\图片集\熊猫1.jpg"; var oldImage = System.Drawing.Image.FromFile(oldFilename); var newFilename = @"E:\我的新熊猫.jpg"; oldImage.Save(newFilename, ImageFormat.Jpeg);
五、生成缩略图
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /// <summary> 2 /// 生成图片缩略文件 3 /// </summary> 4 /// <param name="originalImage">图片源文件</param> 5 /// <param name="width">缩略图宽度</param> 6 /// <param name="height">缩略图高度</param> 7 /// <param name="mode">生成缩略图的方式</param> 8 /// <returns>缩率处理后图片文件</returns> 9 public static Image MakeThumbnail(Image originalImage, int width, int height, ThumbnailModel mode) 10 { 11 int towidth = width; 12 int toheight = height; 13 14 int x = 0; 15 int y = 0; 16 int ow = originalImage.Width; 17 int oh = originalImage.Height; 18 19 switch (mode) 20 { 21 case ThumbnailModel.HighWidth: //指定高宽缩放(可能变形) 22 break; 23 case ThumbnailModel.Width: //指定宽,高按比例 24 toheight = originalImage.Height * width / originalImage.Width; 25 break; 26 case ThumbnailModel.Hight: //指定高,宽按比例 27 towidth = originalImage.Width * height / originalImage.Height; 28 break; 29 case ThumbnailModel.Default: //指定高,宽按比例 30 if (ow <= towidth && oh <= toheight) 31 { 32 x = -(towidth - ow) / 2; 33 y = -(toheight - oh) / 2; 34 ow = towidth; 35 oh = toheight; 36 } 37 else 38 { 39 if (ow > oh)//宽大于高 40 { 41 x = 0; 42 y = -(ow - oh) / 2; 43 oh = ow; 44 } 45 else//高大于宽 46 { 47 y = 0; 48 x = -(oh - ow) / 2; 49 ow = oh; 50 } 51 } 52 break; 53 case ThumbnailModel.Auto: 54 if (originalImage.Width / originalImage.Height >= width / height) 55 { 56 if (originalImage.Width > width) 57 { 58 towidth = width; 59 toheight = (originalImage.Height * width) / originalImage.Width; 60 } 61 else 62 { 63 towidth = originalImage.Width; 64 toheight = originalImage.Height; 65 } 66 } 67 else 68 { 69 if (originalImage.Height > height) 70 { 71 toheight = height; 72 towidth = (originalImage.Width * height) / originalImage.Height; 73 } 74 else 75 { 76 towidth = originalImage.Width; 77 toheight = originalImage.Height; 78 } 79 } 80 break; 81 case ThumbnailModel.Cut: //指定高宽裁减(不变形) 82 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) 83 { 84 oh = originalImage.Height; 85 ow = originalImage.Height * towidth / toheight; 86 y = 0; 87 x = (originalImage.Width - ow) / 2; 88 } 89 else 90 { 91 ow = originalImage.Width; 92 oh = originalImage.Width * height / towidth; 93 x = 0; 94 y = (originalImage.Height - oh) / 2; 95 } 96 break; 97 default: 98 99 break; 100 } 101 102 //新建一个bmp图片 103 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); 104 105 //新建一个画板 106 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); 107 108 //设置高质量插值法 109 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 110 111 //设置高质量,低速度呈现平滑程度 112 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 113 114 //清空画布并以透明背景色填充 115 g.Clear(System.Drawing.Color.White); 116 117 //在指定位置并且按指定大小绘制原图片的指定部分 118 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), 119 new System.Drawing.Rectangle(x, y, ow, oh), 120 System.Drawing.GraphicsUnit.Pixel); 121 122 return bitmap; 123 }
其中缩略图模式定义如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
/// <summary> /// 缩率图处理模式 /// </summary> public enum ThumbnailModel { /// <summary> /// 指定高宽缩放(可能变形) /// </summary> HighWidth, /// <summary> /// 指定宽,高按比例 /// </summary> Width, /// <summary> /// 默认 全图不变形 /// </summary> Default, /// <summary> /// 指定高,宽按比例 /// </summary> Hight, /// <summary> /// 指定高宽裁减(不变形)??指定裁剪区域 /// </summary> Cut, /// <summary> /// 自动 原始图片按比例缩放 /// </summary> Auto }