C# 图片操作 常用方法 总结

Image To byte[]
1     //image to byte[]
2        static byte[] Image2Bytes(System.Drawing.Image photo)
3        {
4             //System.Drawing.Image photo = new System.Drawing.Bitmap(path);
5             System.IO.MemoryStream ms = new System.IO.MemoryStream();
6             photo.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
7             byte[] imagedata = ms.GetBuffer();
8             return imagedata;
9        }
byte[] to image
1        //byte[] to image
2         static System.Drawing.Image Bytes2Image(byte[] imagedata)
3         {
4             System.IO.MemoryStream ms = new  System.IO.MemoryStream(imagedata);
5             System.Drawing.Image b =new  System.Drawing.Bitmap(ms);
6             return b;
7         }
Image To Binary
1   //数据库 字段 DbType="Image"  对应类型System.Data.Linq.Binary
2        static System.Data.Linq.Binary Image2DBType(System.Drawing.Image image)
3        {   
4 
5            System.Data.Linq.Binary Barcode=new Binary(Image2Bytes(image));
6            return Barcode;
7         
8 
9        }
Binary To Image
1    static System.Drawing.Image DBType2Image(System.Data.Linq.Binary  barcode)
2        {
3            System.Drawing.Image img=Bytes2Image(barcode.ToArray());
4            return img;
5        }
Image Cut
 1    // 剪切 并保存
 2         //在指定原图大小  以及截取的位置和大小 
 3        public void CutAndSaveImage(System.Drawing.Image originalImage,string dstpath, int cutX, int cutY, int cutWidth, int cutHeight, int drawX, int drawY, int drawWidth, int drawHeight, int PhotoWidth, int PhotoHeight)
 4         {
 5             //新建一个bmp图片
 6             System.Drawing.Image bitmap = new System.Drawing.Bitmap(PhotoWidth, PhotoHeight);
 7 
 8             //新建一个画板
 9             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
10 
11             //设置高质量插值法
12             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
13 
14             //设置高质量,低速度呈现平滑程度
15             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
16 
17             //清空画布并以透明背景色填充
18             g.Clear(System.Drawing.Color.White);
19 
20             //在指定位置并且按指定大小绘制原图片的指定部分         
21             g.DrawImage(originalImage, new System.Drawing.Rectangle((drawX <= 0 ? 0 : drawX), (drawY <= 0 ? 0 : drawY), drawWidth, drawHeight),
22                 new System.Drawing.Rectangle((cutX < 0 ? 0 : cutX), (cutY<0 ? 0 : cutY), cutWidth, cutHeight), System.Drawing.GraphicsUnit.Pixel);
23 
24             originalImage.Dispose();
25 
26             if (File.Exists(dstpath))
27                 File.Delete(dstpath);
28 
29             if (dstpath.EndsWith("jpg"))
30                 bitmap.Save(dstpath, System.Drawing.Imaging.ImageFormat.Jpeg);
31             else if (dstpath.EndsWith("png"))
32                 bitmap.Save(dstpath, System.Drawing.Imaging.ImageFormat.Png);
33             else
34                 bitmap.Save(dstpath);
35          
36             bitmap.Dispose();
37             g.Dispose();
38 
39         }
翻转[Flip]
1  originalImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
2  originalImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
3  originalImage.RotateFlip(RotateFlipType.RotateNoneFlipXY);
旋转[Rotate]
1  originalImage.RotateFlip(RotateFlipType.Rotate90FlipNone);        originalImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
原文地址:https://www.cnblogs.com/AspDotNetMVC/p/2767594.html