C# 上传图片后,生成缩略图

string imgpath = Server.MapPath("\\CommodityImg\\img\\");

string newguid = Guid.NewGuid().ToString();

HttpPostedFile file = files[i];
string extension = System.IO.Path.GetExtension(file.FileName);

调取方法:Editpic(imgpath + newguid + extension, smallimgpath + "small" + newguid + extension, size);

        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="aimpath">路径</param>
        /// <param name="scale">0L-100L</param>
        public static void Editpic(string oldfile, string newfile, int bzpicsize)
        {
            //string originalFilename = sourcepath;
            //生成的高质量图片名称   
            string strGoodFile = newfile;
            int width = 0, hight = 0;
            //从文件取得图片对象 
            System.Drawing.Image image = System.Drawing.Image.FromFile(oldfile); 
            //新建一个bmp图片 
            int w = image.Width;
            int h = image.Height;

            if (w <= h && h > bzpicsize)
            {
                hight = bzpicsize;
                width = (w * bzpicsize) / h;

            }
            else if (h <= w && w > bzpicsize)
            {
                width = bzpicsize;
                hight = (h * bzpicsize) / w;
            }
            else
            {
                width = w;
                hight = h;
            }

            System.Drawing.Image bitmap = new System.Drawing.Bitmap(width, hight);
            //新建一个画板   
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            //设置高质量插值法   
            //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度   
            //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空一下画布   
            g.Clear(Color.Transparent); 

            //在指定位置画图   
            g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
             new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
             System.Drawing.GraphicsUnit.Pixel);

            System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.Quality;//获取品质(压缩率)编码
            EncoderParameter mycoder = new EncoderParameter(encoder, 100L);//0压缩率最大,100品质最高
            EncoderParameters myCoders = new EncoderParameters(1);//参数数组,大小为1
            myCoders.Param[0] = mycoder;//添加一个参数
            ImageCodecInfo jpgInfo = GetEncoder(ImageFormat.Jpeg);//获取JPG格式编解码信息

            Bitmap myBitmap = new Bitmap(bitmap);
            for (int i = 0; i < myBitmap.Width; i++)
            {
                Color Color2 = myBitmap.GetPixel(i, 1);                    //调用GetPixel方法获取像素点的颜色
                double red2 = Color2.R;
                double green2 = Color2.G;
                double blue2 = Color2.B;
                myBitmap.SetPixel(i, 0, Color.FromArgb((int)red2, (int)green2, (int)blue2));
            }

            for (int i = 0; i < myBitmap.Height; i++)
            {
                Color Color2 = myBitmap.GetPixel(1, i);                    //调用GetPixel方法获取像素点的颜色
                double red2 = Color2.R;
                double green2 = Color2.G;
                double blue2 = Color2.B;
                myBitmap.SetPixel(0, i, Color.FromArgb((int)red2, (int)green2, (int)blue2));      //用SetPixel()方法设置像素点的颜色
            }


            try
            {
                //以jpg格式保存缩略图  
                bitmap.Save(strGoodFile);
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                image.Dispose();
                bitmap.Dispose();
                g.Dispose();
                //bitmap.MakeTransparent(Color.Transparent);
            }

             
        }

        private static ImageCodecInfo GetEncoder(ImageFormat format)//获取特定的图像编解码信息
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                {
                    return codec;
                }
            }
            return null;
        }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值