码迷,mamicode.com
首页 > 其他好文 > 详细

图片类处理

时间:2017-01-18 14:44:32      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:select   比较   url   params   graphics   dimen   str   填充   void   

1、读取图片两种方式

System.Drawing.Bitmap imgFrom = new System.Drawing.Bitmap(fromImg)

System.Drawing.Image gif = System.Drawing.Image.FromFile(fromImg);

2、把图片从一个绘制到另一个,首先要先实例化一个Bitmap(背景默认黑色)

 System.Drawing.Bitmap bmpOut = new System.Drawing.Bitmap(Width, Height);

using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmpOut))
{
g.FillRectangle(System.Drawing.Brushes.White, 0, 0, bmpOut.Width, bmpOut.Height); //填充背景颜色
g.DrawImage(imgIn, new System.Drawing.Rectangle(0, 0, Width, Height), new System.Drawing.Rectangle(0, 0, Width, Height), System.Drawing.GraphicsUnit.Pixel);
}

3、如果需要把特定颜色去掉

bmpOut .MakeTransparent(System.Drawing.Color.FromArgb(252, 252, 252)); //把该颜色设置为透明

4、图片保存后,一般会比较大,可以设置压缩

System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
long[] quality = new long[1] { 100 };
encoderParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

bmpOut.Save(fileSaveUrl, GetCodecInfo("image/gif"), encoderParams);

5、完整代码

public void Cut(System.Drawing.Bitmap imgIn, int Width, int Height, string fileSaveUrl)
{
System.Drawing.Imaging.FrameDimension fd = new System.Drawing.Imaging.FrameDimension(imgIn.FrameDimensionsList[0]);
if (imgIn.GetFrameCount(fd) > 0)
{
imgIn.SelectActiveFrame(fd, 0); //取第一帧图片
imgIn.MakeTransparent(System.Drawing.Color.FromArgb(252, 252, 252)); //把该颜色设置为透明
System.Drawing.Bitmap bmpOut = new System.Drawing.Bitmap(Width, Height);
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmpOut))
{
g.FillRectangle(System.Drawing.Brushes.White, 0, 0, bmpOut.Width, bmpOut.Height);
g.DrawImage(imgIn, new System.Drawing.Rectangle(0, 0, Width, Height), new System.Drawing.Rectangle(0, 0, Width, Height), System.Drawing.GraphicsUnit.Pixel);
}

//以下代码为保存图片时,设置压缩质量
System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
long[] quality = new long[1] { 100 };
encoderParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

bmpOut.Save(fileSaveUrl, GetCodecInfo("image/gif"), encoderParams);
}
}

图片类处理

标签:select   比较   url   params   graphics   dimen   str   填充   void   

原文地址:http://www.cnblogs.com/honzhez/p/6296630.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!