简单的GDI+处理图片大小(C#代码)
/// <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;
}
/// 缩放图片
/// </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;
}
白虎
Hello! I Am David.DU.