创建大图小图
void CreatBSimg(string extension,string path,string filename,string imgs) {
#region 得到图片尺寸 int imgWidth = 0; int imgHeight = 0; string[] imgExtension = new string[] { ".jpg", ".gif", ".png", ".bmp", ".jpeg" }; foreach (string item in imgExtension) { if (item == extension) { System.Drawing.Image img = System.Drawing.Image.FromFile(imgs); imgWidth = img.Width; imgHeight = img.Height;
#region 新增 用于同时保存大图,和缩略图
//设置指定大小的上传 UploadImageFixedSize(img, path + "\\T" + filename, 100, 100); //设置指定大小的上传 UploadImageFixedSize(img, path + "\\B" + filename, 800, 600); #endregion
img.Dispose(); } } #endregion }
保存图片 public void UploadImageFixedSize(System.Drawing.Image image, string savePath, int width, int height) { Graphics g1 = null; Bitmap tImage = null; try {
int oWidth = image.Width;//原图宽度 int oHeight = image.Height;//原图高度 int tWidth = width;//设置缩略图初始宽度 int tHeight = height;//设置缩略图初始高度 int outwidth = 0; int outheight = 0; ChangeImageSize(oWidth, oHeight, tWidth, tHeight, ref outwidth, ref outheight);
tImage = new Bitmap(outwidth, outheight); g1 = Graphics.FromImage(tImage); g1.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法 g1.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 g1.Clear(Color.Transparent);//清空画布并以透明背景色填充 g1.DrawImage(image, new Rectangle(0, 0, outwidth, outheight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
//以JPG格式保存图片 tImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Png);
} catch (Exception ex) { } finally { //释放资源 //image.Dispose(); g1.Dispose(); tImage.Dispose(); } }
计算图片大小 public void ChangeImageSize(float imgWidth, float imgHeight, int width, int height, ref int outwidth, ref int outheight) { try { if (imgHeight > height || imgWidth > width) { if (imgHeight / height > imgWidth / width) { imgWidth = height / imgHeight * imgWidth; imgHeight = height; } else { imgHeight = width / imgWidth * imgHeight; imgWidth = width; } } outwidth = Convert.ToInt32(imgWidth); outheight = Convert.ToInt32(imgHeight); } catch (Exception ce) { } }
转载于:https://www.cnblogs.com/wanheng/archive/2012/01/11/2319214.html
