前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >图片处理

图片处理

作者头像
小端
发布2018-04-16 11:19:43
1.8K0
发布2018-04-16 11:19:43
举报
文章被收录于专栏:java架构师java架构师
代码语言:javascript
复制
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);

剪裁、压缩

代码语言:javascript
复制
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)";
            }

        }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-02-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
图像处理
图像处理基于腾讯云深度学习等人工智能技术,提供综合性的图像优化处理服务,包括图像质量评估、图像清晰度增强、图像智能裁剪等。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档