Asp.Net 高清图片缩略图生成

生成缩略图核心代码:

/// <summary>
        /// 图片上传 生成缩略图
        /// </summary>
        /// <param name="files">文件上传控件</param>
        /// <param name="path">文件夹名称</param>
        /// <param name="fname">文件名称</param>
        /// <param name="w">缩略图宽度</param>
        /// <param name="h">缩略图高度</param>
        public static void UploadImageThumbs(HttpPostedFile files, string filePath, string fname, int w, int h)
        {
            if (UtilsFile.IsOrNoFileUp(files) && fname.Equals(String.Empty) == false)
            {
                UtilsFile.DirectorysCreate(filePath);
                Image image = Image.FromStream(files.InputStream, true);
                //等比例缩放
                if (w > 0 && h > 0)
                {
                    if (image.Width > image.Height)
                    {
                        if (image.Width > w)
                            h = (int)(image.Height * ((decimal)w / image.Width));
                        else
                        {
                            h = image.Height;
                            w = image.Width;
                        }
                    }
                    else
                    {
                        if (image.Height > h)
                            w = (int)(image.Width * ((decimal)h / image.Height));
                        else
                        {
                            h = image.Height;
                            w = image.Width;
                        }
                    }
                }
                else if (w > 0 && h == 0)
                {
                    if (image.Width < w)
                        w = image.Width;
                    h = (int)(image.Height * ((decimal)w / image.Width));
                }
                else if (w == 0 && h > 0)
                {
                    if (image.Height < h)
                        h = image.Height;
                    w = (int)(image.Width * ((decimal)h / image.Height));
                }
                else
                {
                    w = image.Width;
                    h = image.Height;
                }

                Bitmap ret = new Bitmap(w, h);

                using (Graphics g = Graphics.FromImage(ret))
                {
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.FillRectangle(Brushes.White, 0, 0, w, h);
                    g.DrawImage(image, 0, 0, w, h);
                    EncoderParameters parms = new EncoderParameters();
                    long[] quality = new long[1];
                    quality[0] = 80;
                    EncoderParameter parm = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                    parms.Param[0] = parm;
                    ImageCodecInfo[] arr = ImageCodecInfo.GetImageEncoders();
                    ImageCodecInfo ar = null;
                    for (int x = 0; x < arr.Length; x++)
                    {
                        if (arr[x].FormatDescription.Equals("JPEG"))
                        {
                            ar = arr[x];
                            break;
                        }
                    }

                    ret.Save(filePath + fname, ar, parms);
                    ret.Dispose();
                    image.Dispose();
                }
            }
        }

 

总结:以上生成缩略图代码,效果是比较好的,就是有时候红色的点会失真,比较无解。不过这问题基本可以忽略不计。较真的朋友,如有较好的生成代码,可以共享。

原文转载: http://www.dyxue.com/tech/id2498.html

Asp.Net 高清图片缩略图生成,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。