C#中Bitmap类实现对图像操作的⼀些⽅法(Asp(C#)-放
⼤缩⼩图⽚尺⼨)
导⼊以下两个包:
System.Drawing;
System.Drawing.Imaging;
建产对象:
Bitmap bm = new Bitmap("c:/1.bmp");
缩放:
Bitmap bm1 = new Bitmap(bm,width,height);
格式转换:
bm.save("c:/1.jpg",ImageFromat.Jpeg);
bm1.Save("c:/1.gif", ImageFormat.Gif);
剪切⼀个区域:
//剪切⼤⼩
int cutwidth;
int cutheight;
Graphics g;
//以⼤⼩为剪切⼤⼩,像素格式为32位RGB创建⼀个位图对像
Bitmap bm1 = new Bitmap(width,height,PixelFormat.Format32bppRgb) ;
//定义⼀个区域
Rectangle rg = new Rectangle(0,0,cutwidth,cutheight);
//要绘制到的位图
g = Graphics.FromImage(bm1);
//将bm内rg所指定的区域绘制到bm1
g.DrawImage(bm,rg)
Asp(C#)-放⼤缩⼩图⽚尺⼨
/// <summary>
/// 放⼤缩⼩图⽚尺⼨
/// </summary>
/// <param name="picPath"></param>
/// <param name="reSizePicPath"></param>
/// <param name="iSize"></param>
/// <param name="format"></param>
public void PicSized(string picPath, string reSizePicPath, int iSize, ImageFormat format)
{
Bitmap originBmp = new Bitmap(picPath);
int w = originBmp.Width * iSize;
int h = originBmp.Height * iSize;
Bitmap resizedBmp = new Bitmap(w, h);
Graphics g = Graphics.FromImage(resizedBmp);
//设置⾼质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置⾼质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//消除锯齿
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawImage(originBmp, new Rectangle(0, 0, w, h), new Rectangle(0, 0, originBmp.Width, originBmp.Height), GraphicsUnit.Pixel); resizedBmp.Save(reSizePicPath, format);
g.Dispose();
resizedBmp.Dispose();
originBmp.Dispose();
}resized
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论