ZFYCH_Love

Simply but Powerful

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  115 随笔 :: 1 文章 :: 36 评论 :: 18万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
复制代码
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)";
            }

        }
复制代码

 

posted on   xiaoyang_  阅读(394)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 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学习笔记(三)
点击右上角即可分享
微信分享提示