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)"; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2012-02-22 WCF学习笔记(三)