首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

图片高保真缩放,该如何处理

2013-08-01 
图片高保真缩放我现在 用的是这个nowImg new Bitmap(canvas)graphics Graphics.FromImage(nowImg)gr

图片高保真缩放
我现在 用的是这个
nowImg = new Bitmap(canvas);
            graphics = Graphics.FromImage(nowImg);
            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//让画线平滑
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
orignalImg = Image.FromFile(way);
                graphics.DrawImage(orignalImg, new Rectangle(50, 50, 325, 325));
nowImg.Save(str, System.Drawing.Imaging.ImageFormat.Jpeg);


canvas是一个400*400的白色图片
原始图片 way  是一个 180k的 1000*1000的图片  现在缩放到 300*300放在400*400背景中
保存 后 只有10.5k  图片完全达不到要求
求更好的方法 
[解决办法]
保存为bmp或者png,不要保存为Jpg的,jpg压缩的太厉害!
[解决办法]

/// <summary>
    /// 为图片生成缩略图
    /// </summary>
    /// <param name="phyPath">原图片的路径</param>
    /// <param name="width">缩略图宽</param>
    /// <param name="height">缩略图高</param>
    /// <returns></returns>
    public void GetThumbnail(string phyPath, int width, int height)
    {
        System.Drawing.Image image = System.Drawing.Image.FromFile(phyPath);
        string folder = Path.GetDirectoryName(phyPath);
        string dest1 = "\\s_" + Path.GetFileName(phyPath);


        string dest = folder + dest1;
        //生成的缩略图图像文件的绝对路径
        int thumbWidth = width;   //要生成的缩略图的宽度
        int thumbHeight = height;
        //接着创建一个System.Drawing.Bitmap对象,并设置你希望的缩略图的宽度和高度。
        int srcWidth = image.Width;
        int srcHeight = image.Height;
        //int thumbHeight = (srcHeight / srcWidth) * thumbWidth;

        Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);
        //从Bitmap创建一个System.Drawing.Graphics对象,用来绘制高质量的缩小图。
        System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
        //设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality
        gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //下面这个也设成高质量
        gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        //下面这个设成High
        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        //把原始图像绘制成上面所设置宽高的缩小图
        System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
        gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
        //保存图像,大功告成!
        bmp.Save(dest);
        //最后别忘了释放资源(译者PS:C#可以自动回收吧)
        bmp.Dispose();
        image.Dispose();
    }


[解决办法]
graphics.DrawImage(imageFrom, new Rectangle(50, 50, 300, 300), new Rectangle(0, 0, 1000, 1000), GraphicsUnit.Pixel);

热点排行