我送你一个程序做这个吧,见红的有?
授人以渔,不授人以鱼。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Drawing; namespace myTools { public static partial class bk { //------------------------------------------------------------ // 功能:将图像转换为指定尺寸的图像,并在目标区域内居中 // 参数:sourceImageFileName -- 源文件名 // targetImageFileName -- 目标文件名 // width -- 目标区域像素宽度 // height -- 目标区域像素高度 // 备注:转换图像为96DPI、白色背景、24位色的PNG格式 //------------------------------------------------------------ public static Boolean Convert_Image(String sourceImageFileName, String targetImageFileName, Int32 width, Int32 height) { Boolean success = false; if (sourceImageFileName.ToUpper().EndsWith(".JPG") || sourceImageFileName.ToUpper().EndsWith(".PNG")) { Int32 top = 0; // 新图像在目标区域中的顶坐标 Int32 left = 0; // 新图像在目标区域中的左坐标 Image sourceImage = Image.FromFile(sourceImageFileName); Bitmap newBitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); newBitmap.SetResolution(96, 96); if (sourceImage.Width > sourceImage.Height) { height = sourceImage.Height * width / sourceImage.Width; top = (newBitmap.Height - height) / 2; } else { width = sourceImage.Width * height / sourceImage.Height; left = (newBitmap.Width - width) / 2; } Rectangle rect = new Rectangle(left, top, width, height); Graphics theGraphics = Graphics.FromImage(newBitmap); theGraphics.Clear(System.Drawing.Color.White); theGraphics.DrawImage(sourceImage, rect); newBitmap.Save(targetImageFileName, System.Drawing.Imaging.ImageFormat.Png); sourceImage.Dispose(); newBitmap.Dispose(); theGraphics.Dispose(); success = true; } return success; } } }