簡單的GDI+處理圖片大小(C#代碼)
來源:互聯網
上載者:User
/// <summary>
/// 縮放圖片
/// </summary>
/// <param name="img">原圖片</param>
/// <param name="xWith">縮放寬比例,如果想縮小圖片,小於100</param>
/// <param name="yHeight">縮放高比例</param>
/// <returns>返回處理後圖片</returns>
public Image scaleImg(System.Drawing.Image img, int xWith, int yHeight)
{
//計算處理後圖片寬
int i = Convert.ToInt32(img.Width * xWith / 100);
//計算處理後圖片高
int j = Convert.ToInt32(img.Height * yHeight / 100);
//格式化圖片
System.Drawing.Image imgScale = new System.Drawing.Bitmap(i, j, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgScale);
System.Drawing.Rectangle srcRect = new System.Drawing.Rectangle(0, 0, img.Width, img.Height);
System.Drawing.Rectangle desRect = new System.Drawing.Rectangle(0, 0, imgScale.Width, imgScale.Height);
g.Clear(System.Drawing.Color.White);
g.DrawImage(img, desRect, srcRect, System.Drawing.GraphicsUnit.Pixel);
//處理後的圖片另存
imgScale.Save("E:\\1111.jpg", System.Drawing.Imaging.ImageFormat.Gif);
g.Dispose();
return imgScale;
}