zoukankan      html  css  js  c++  java
  • 图片处理

    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)";
                }
    
            }
  • 相关阅读:
    python目录操作【os和os.path】
    Zabbix4.0 zabbix 快速监控主机
    Zabbix 4.0 钉钉报警
    MySql:sql99语法的连接查询
    bat脚本中存在多条指令,但只执行到某条指令不继续向下执行的一种解决方法
    基类与接口类中的虚析构函数(virtual destructor)
    TortoiseGit使用指南;
    Rust编译问题Blocking waiting for file lock on package cache
    win10安装visual C++ 6.0,在最后显示安装程序正在更新您的系统,然后就无响应
    从实现装饰者模式中思考C++指针和引用的选择
  • 原文地址:https://www.cnblogs.com/xyang/p/2922718.html
Copyright © 2011-2022 走看看