public int UpdateFileImg(HttpRequestBase request, HttpSessionStateBase session, AYBTSubitemDataContext _adc) { string fileName = ImageClass.GetFileName(request.Files[0].FileName); string filePath = ImageClass.GetFilePath(((FunctionModule)(2)).ToString()); string fileType = ((FunctionModule)(2)).ToString(); ////////////////////保存原图 Image picFile = Image.FromStream(request.Files[0].InputStream); picFile.Save(filePath + fileName); ImageClass ic = new ImageClass(picFile); List<PictureView> subItems = new List<PictureView>(); //获取图片的所有尺寸 var picTypes = DictionaryManager.GetDictionariesByTypeCode("AdvPictureSize"); //获取图片尺寸 var resolutionsA = picTypes.Where(o => o.Key == 1).FirstOrDefault().Value.Split('*'); var resolutionsB = picTypes.Where(o => o.Key == 2).FirstOrDefault().Value.Split('*'); var resolutionsC = picTypes.Where(o => o.Key == 3).FirstOrDefault().Value.Split('*'); //压缩后保存(第一张符合规定的广告大图) Image SubPicA = ic.GetReducedImage(int.Parse(resolutionsA[0]), int.Parse(resolutionsA[1])); string subFileName = ImageClass.GetFileName(fileName); SubPicA.Save(filePath + subFileName, System.Drawing.Imaging.ImageFormat.Jpeg); this.SavePic(subItems, SubPicA.Width, SubPicA.Height, picTypes.Where(o => o.Key == 1).FirstOrDefault().Key, subFileName, filePath, fileType, request.Files[0].ContentLength); //剪切为广告中图 string cutImage = ImageClass.CutImage(filePath + subFileName, (int.Parse(resolutionsA[0]) - int.Parse(resolutionsB[0])) / 2, (int.Parse(resolutionsA[1]) - int.Parse(resolutionsB[1])) / 2, int.Parse(resolutionsB[0]), int.Parse(resolutionsB[1]), ImageClass.GetFileName(subFileName), filePath, fileName.Split('.')[1]); var nameAndPath = cutImage.Split('*'); //把中图保存 this.SavePic(subItems, int.Parse(resolutionsB[0]), int.Parse(resolutionsB[1]), picTypes.Where(o => o.Key == 2).FirstOrDefault().Key, nameAndPath[1], nameAndPath[0], fileType, request.Files[0].ContentLength); //压缩广告小图,并保存 //ic = new ImageClass(Image.FromStream(Image.FromFile(nameAndPath[0]+nameAndPath[1])); Image SubPicC = ic.GetReducedImage(int.Parse(resolutionsC[0]), int.Parse(resolutionsC[1])); subFileName = ImageClass.GetFileName(nameAndPath[1]); SubPicC.Save(filePath + subFileName, System.Drawing.Imaging.ImageFormat.Jpeg); this.SavePic(subItems, SubPicC.Width, SubPicC.Height, picTypes.Where(o => o.Key == 3).FirstOrDefault().Key, subFileName, filePath, fileType, request.Files[0].ContentLength);
剪裁、压缩
public static string CutImage(string url, int beginX, int beginY, int getX, int getY, string fileName, string savePath, string fileExt) { if ((beginX < getX) && (beginY < getY)) { Bitmap bitmap = new Bitmap(url); if (((beginX + getX) <= bitmap.Width) && ((beginY + getY) <= bitmap.Height)) { Bitmap destBitmap = new Bitmap(getX, getY); Rectangle destRect = new Rectangle(0, 0, getX, getY);//容器 Rectangle srcRect = new Rectangle(beginX, beginY, getX, getY); Graphics.FromImage(destBitmap).DrawImage(bitmap, destRect, srcRect, GraphicsUnit.Pixel); Graphics g = Graphics.FromImage(bitmap); ImageFormat format = ImageFormat.Png; switch (fileExt.ToLower()) { case "png": format = ImageFormat.Png; break; case "bmp": format = ImageFormat.Bmp; break; case "gif": format = ImageFormat.Gif; break; } destBitmap.Save(savePath + "//" + fileName , format); return savePath + "\\" + "*" + fileName.Split('.')[0] + "." + fileExt; } else { return "截取范围超出图片范围"; } } else { return "请确认(beginX < getX)&&(beginY < getY)"; } }