zoukankan      html  css  js  c++  java
  • 生成缩略图片

    图片上传并按指定大小保存

    图片上传并按指定大小保存
            /// <summary>
            /// 按指定大小保存图片
            /// </summary>
            /// <param name="postedFile"></param>
            /// <param name="fileSavePath"></param>
            private void SaveAsSize(HttpPostedFile postedFile, string fileSavePath, int width, int height, ThumbnailModel mode)
            {
                int toWidth = width;
                int toHeight = height;
                int x = 0;
                int y = 0;
    
                Image image = Image.FromStream(postedFile.InputStream);
                int imgW = image.Width;
                int imgH = image.Height;
    
                switch (mode)
                {
                    case ThumbnailModel.Width: //指定宽,高按比例   
                        toHeight = image.Height * width / image.Width;
                        break;
                    case ThumbnailModel.Hight: //指定高,宽按比例  
                        toWidth = image.Width * height / image.Height;
                        break;
                    case ThumbnailModel.Default: //默认,全图不变形  
                        if (imgW <= toWidth && imgH <= toHeight)//若原图小,则原图居中
                        {
                            x = -(toWidth - imgW) / 2;
                            y = -(toHeight - imgH) / 2;
                            imgW = toWidth;
                            imgH = toHeight;
                        }
                        else
                        {
                            if (imgW > imgH)//宽大于高
                            {
                                x = 0;
                                y = -(imgW - imgH) / 2;
                                imgH = imgW;
                            }
                            else//高大于宽
                            {
                                y = 0;
                                x = -(imgH - imgW) / 2;
                                imgW = imgH;
                            }
                        }
                        break;
                    case ThumbnailModel.Auto://自动,原始图片按比例缩放
                        if (image.Width / image.Height >= width / height)
                        {
                            if (image.Width > width)
                            {
                                toWidth = width;
                                toHeight = (image.Height * width) / image.Width;
                            }
                            else
                            {
                                toWidth = image.Width;
                                toHeight = image.Height;
                            }
                        }
                        else
                        {
                            if (image.Height > height)
                            {
                                toHeight = height;
                                toWidth = (image.Width * height) / image.Height;
                            }
                            else
                            {
                                toWidth = image.Width;
                                toHeight = image.Height;
                            }
                        }
                        break;
                    case ThumbnailModel.Cut: //指定高宽裁减(不变形)   
                        if ((double)image.Width / image.Height > (double)toWidth / toHeight)
                        {
                            imgH = image.Height;
                            imgW = image.Height * toWidth / toHeight;
                            y = 0;
                            x = (image.Width - imgW) / 2;
                        }
                        else
                        {
                            imgW = image.Width;
                            imgH = image.Width * height / toWidth;
                            x = 0;
                            y = (image.Height - imgH) / 2;
                        }
                        break;
                    case ThumbnailModel.HighWidth: //指定高宽缩放(可能变形)   
                    default:
                        break;
                }
    
                Image newImg = new Bitmap(toWidth, toHeight);
                Graphics graphics = Graphics.FromImage(newImg);
                graphics.Clear(Color.White);//空白背景
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;//插值模式-高质量
                graphics.SmoothingMode = SmoothingMode.HighQuality;//平滑模式-高质量
                graphics.DrawImage(image, new Rectangle(0, 0, toWidth, toHeight), new Rectangle(0, 0, imgW, imgH), GraphicsUnit.Pixel);
                EncoderParameters encoderParameters = new EncoderParameters();
                EncoderParameter encoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
                encoderParameters.Param[0] = encoderParameter;
                ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo encoder = null;
                for (int i = 0; i < imageEncoders.Length; i++)
                {
                    if (imageEncoders[i].FormatDescription.Equals("JPEG"))
                    {
                        encoder = imageEncoders[i];
                        break;
                    }
                }
                newImg.Save(fileSavePath, encoder, encoderParameters);//保存文件
                encoderParameters.Dispose();
                encoderParameter.Dispose();
                image.Dispose();
                newImg.Dispose();
                graphics.Dispose();
            }

       

     End

  • 相关阅读:
    服务部署 RPC vs RESTful
    模拟浏览器之从 Selenium 到splinter
    windows程序设计 vs2012 新建win32项目
    ubuntu python 安装numpy,scipy.pandas.....
    vmvare 将主机的文件复制到虚拟机系统中 安装WMware tools
    ubuntu 修改root密码
    python 定义类 简单使用
    python 定义函数 两个文件调用函数
    python 定义函数 调用函数
    python windows 安装gensim
  • 原文地址:https://www.cnblogs.com/huhunet/p/7402873.html
Copyright © 2011-2022 走看看