http://community.csdn.net/Expert/topic/5400/5400258.xml?temp=.1330683
前段时间,我发帖请教了.net下缩放图片后质量不好的问题。非常感谢net_lover(【孟子E章】) 给我一个解决方法是使用Image.GetThumbnailImage方法来实现,的确这样的图像的确很高,几乎可以和ps缩放后生成的最高质量jpg相比。但是这个方法有一个问题,如果原图中带有一个缩略图信息的话(比如这张图:http://adow.thmz.com/pk/images/k.jpg),GetThumbnailImage就会直接从图像中取得这个缩略图返回,而不是根据整张图片来生成。问题在于,我希望缩放后的图片大小是我定义的,如果一张原图中包括了缩略图被返回的话,就会从一个很小的尺寸(一般图像信息中包括的所录图只有128*96这样级别的)被放大到我指定的尺寸,那样的图片惨不忍睹。对这样的图片,大家看有什么方法解决。

解决方案 »

  1.   

    自己顶一下,
    msdn下是这样解释的:如果 Image 包含一个嵌入式缩略图像,则此方法会检索嵌入式缩略图,并将其缩放为所需大小。如果 Image 不包含嵌入式缩略图像,此方法会通过缩放主图像创建一个缩略图像。缩略图信息可以用exif工具察看到。
      

  2.   

    http://www.microsoft.com/china/MSDN/library/archives/library/DNAspp/html/colorquant.asp
      

  3.   

    to :net_lover(【孟子E章】) 
    是的,以前我也不知道
    http://adow.thmz.com/pk/images/k.jpg
    这张图就有
      

  4.   

    http://adow.thmz.com/pk/images/k.jpg
    打不开
      

  5.   

    楼主看看我写的代码,如果原图小于缩放以后的图的话,是不会执行的
            /// <summary>
            /// 按比例缩放图片,并非按原来大小裁剪
            /// 水平长度优先.
            /// </summary>
            public static string ResizeImage(string _srcImage, int _DstX, string _dstPath)
            {
                System.Drawing.Image img = System.Drawing.Image.FromFile(_srcImage);
                int newHight = img.Height;
                int newWidth = img.Width;
                while (newWidth > _DstX)//按比例缩小图片
                {
                    newHight = Convert.ToInt32(newHight / 1.01);
                    newWidth = Convert.ToInt32(newWidth / 1.01);
                }
                System.Drawing.Image newimg = img.GetThumbnailImage(newWidth, newHight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbCallBack), IntPtr.Zero);
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(newimg);
                string tempName=GenRandomNumber(22) + ".jpg";
                bmp.Save(_dstPath + tempName , System.Drawing.Imaging.ImageFormat.Jpeg);
                return tempName;
            }        /// <summary>
            /// 按比例缩放图片,并非按原来大小裁剪
            /// 垂直长度优先
            /// </summary>
            public static string ResizeImage(int _DstY, string _srcImage, string _dstPath)
            {
                System.Drawing.Image img = System.Drawing.Image.FromFile(_srcImage);
                int newHight = img.Height;
                int newWidth = img.Width;
                while (newWidth > _DstY)//按比例缩小图片
                {
                    newHight = Convert.ToInt32(newHight / 1.01);
                    newWidth = Convert.ToInt32(newWidth / 1.01);
                }
                System.Drawing.Image newimg = img.GetThumbnailImage(newWidth, newHight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbCallBack), IntPtr.Zero);
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(newimg);
                string tempName = GenRandomNumber(22) + ".jpg";
                bmp.Save(_dstPath + tempName, System.Drawing.Imaging.ImageFormat.Jpeg);
                return tempName;
            }
            public static bool ThumbCallBack()
            {
                return true;
            }
            public static string GenRandomNumber(int _seed)
            {
                Random rdm = new Random(_seed);
                return System.DateTime.Now.Millisecond.ToString() + rdm.Next(1, 65536).ToString();
            }
      

  6.   

    http://adow.thmz.com/pk/images/k.jpg应该可以的吧。我想做的是将用户上传的照片生成一张750高的图片(原图可能很大)用GetThumbnailImage之后,他直接从照片中取得了自带的缩略图,然后给我强制到750,就糟糕了
      

  7.   

    http://farm1.static.flickr.com/182/437392751_b774f270ae_o.jpg
    再试试这个呢
      

  8.   

    http://blog.thmz.com/user1/9/archives/2007/14246.shtml
    最后我得出的结论
      

  9.   

    protected void btnUp_Click(object sender, EventArgs e)
        {
            string mPath;
            if (upImage.PostedFile.ContentLength > 4096000)//判断是否大于4M
            {            Response.Write("<Script language = JavaScript>alert('大!')</Script>");
            }
            else
            {
                if ("" != upImage.PostedFile.FileName)
                {
                    string imagePath = upImage.PostedFile.FileName;
                    string imageType = imagePath.Substring(imagePath.LastIndexOf(".") + 1);
                    string imageName = imagePath.Substring(imagePath.LastIndexOf("\\") + 1);
                    if ("jpg" != imageType && "gif" != imageType && "GIF" != imageType && "JPG" != imageType)
                    {
                        Response.Write("<script language='javascript'>alert('请选择jpg或者gif格式!');</script>");
                        return;
                    }
                    else
                    {
                        try
                        {
                            mPath = Server.MapPath("upfile");
                            upImage.PostedFile.SaveAs(mPath + "\\" + imageName);
                            imageSource.ImageUrl = "upfile/" + imageName;
                            System.Drawing.Image image = System.Drawing.Image.FromFile(mPath + "\\" + imageName);                        System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                            System.Drawing.Image newimage = image.GetThumbnailImage(50, 50, myCallback, IntPtr.Zero);                        newimage.Save(Server.MapPath("upfile") + "\\small" + imageName);
                            image.Dispose();
                            newimage.Dispose();
                            imageSmall.ImageUrl = "upfile/" + "small" + imageName;
                            Response.Write("成功");
                        }
                        catch
                        {
                            Response.Write("失败");
                        }
                    }
                }
            }
        }image.GetThumbnailImage(50, 50, myCallback, IntPtr.Zero);
    这里的数字不就是控制大小吗?