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/archive/2012/11/16/2767594.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值