上传图片生成缩略图

 
        /// <summary>
        /// 修改图片分辨率并保存图片
        /// </summary>
        /// <param name="oldPath">原图片路径</param>
        /// <param name="fileName">图片名</param>
        /// <param name="path">图片存储路径</param>
        /// <param name="width">图片宽度</param>
        /// <param name="height">图片高度</param>
        /// <returns></returns>
        public int CreatePic(string oldPath, string fileName, string path, int width, int height)
        {
            try
            {
                if (width != 0 && height != 0)
                {

                    //System.Drawing.Bitmap ret = null;
                    System.Drawing.Image source = Image.FromFile(oldPath);
                    CreateCompressThumbnail(source, width, height, path, fileName);

                }
                else
                {
                    FileStream fs = File.OpenRead(oldPath);
                    byte[] bytes = new byte[fs.Length];
                    fs.Read(bytes, 0, bytes.Length);
                    fs.Close();

                    FileStream file = new FileStream(path + fileName, FileMode.OpenOrCreate);
                    file.Write(bytes, 0, bytes.Length);
                    file.Close();
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        private static Bitmap CreateThumbnail(Image source, int thumbWi, int thumbHi)
        {

            System.Drawing.Bitmap ret = null;
            try
            {
                int wi, hi;

                wi = thumbWi;
                hi = thumbHi;

                ret = new Bitmap(wi, hi);
                using (Graphics g = Graphics.FromImage(ret))
                {
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.FillRectangle(Brushes.White, 0, 0, wi, hi);
                    g.DrawImage(source, 0, 0, wi, hi);
                    g.Dispose();
                }
            }
            catch
            {
                ret = null;
            }
            return ret;
        }

        public static void CreateCompressThumbnail(Image source, int thumbWi, int thumbHi, string SavePathThumb, string fileName)
        {
            using (System.Drawing.Image myThumbnail = CreateThumbnail(source, thumbWi, thumbHi))
            {
                System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
                long[] quality = new long[1];
                quality[0] = 75;
                System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                encoderParams.Param[0] = encoderParam;

                System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
                System.Drawing.Imaging.ImageCodecInfo jpegICI = null;
                for (int x = 0; x < arrayICI.Length; x++)
                {
                    if (arrayICI[x].FormatDescription.Equals("JPEG"))
                    {
                        jpegICI = arrayICI[x];
                        break;
                    }
                }
                myThumbnail.Save(Path.Combine(SavePathThumb, fileName), jpegICI, encoderParams);               
                myThumbnail.Dispose();
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值