匯入以下兩個包:
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.net(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();
}