在WinForms中将调整大小的位图图像的大小调整为原始大小

时间:2017-11-11 00:30:00

标签: c# winforms bitmap picturebox

我有一张分辨率为6000x4000的PNG图像,我必须借鉴它。所以我将图像加载到尺寸为1280x800的pictureBox中。在绘制之后,我需要将PNG图像保存为原始分辨率6000x4000。所以我使用

将其重新加载到大小为6000x4000的新位图中
btm = new Bitmap(6000, 4000);
image = Graphics.FromImage(btm);
g.DrawImage(btm, Point.Empty);

并使用

保存
btm.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);

现在我最终获得了分辨率为6000x4000的白色背景png图像,但其上编辑的图像为1280x800,如Saved Image

如何将图像尺寸调整回原始尺寸(6000x4000)。谢谢。

另请查找我的代码

 private void drawImage(string imgLocation)
        {
            Bitmap b = new Bitmap(imgLocation);

            ////test
            pictureBox1.Height = 800;
            pictureBox1.Width = 1280;

            g = pictureBox1.CreateGraphics();

            btm = new Bitmap(6000, 4000);

            image = Graphics.FromImage(btm);
            image.CompositingMode = CompositingMode.SourceCopy;
            image.CompositingQuality = CompositingQuality.HighQuality;
            image.InterpolationMode = InterpolationMode.HighQualityBicubic;
            image.SmoothingMode = SmoothingMode.HighQuality;
            image.PixelOffsetMode = PixelOffsetMode.HighQuality;
            image.Clear(Color.White);        

            image.DrawImage(b, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
            //image.DrawImage(btm, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            //g.DrawImage(btm, Point.Empty);
            g.DrawImage(btm, new Rectangle(0, 0, 6000,4000) );
        }

2 个答案:

答案 0 :(得分:0)

这个怎么样? g.DrawImage(btm, new Rectangle(0,0,6000,4000));

答案 1 :(得分:0)

以下是一种可以帮助您进行图像放大/缩小的方法。 请注意,在任何情况下,大的升级比率都会导致严重的图像失真。 您也可以尝试使用其他graphics.CompositingMode属性,如graphics.CompositingQuality 您将需要以下使用:

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

该方法在我的应用中运行良好。

public static Image ImageResize(Image img, int width, int height)
{
    var tgtRect = new Rectangle(0, 0, width, height);
    var tgtImg = new Bitmap(width, height);

    tgtImg.SetResolution(img.HorizontalResolution, img.VerticalResolution);

    using (var graphics = Graphics.FromImage(tgtImg))
    {
        graphics.CompositingMode = CompositingMode.SourceCopy;
        using (var wrapMode = new ImageAttributes())
        {
            wrapMode.SetWrapMode(WrapMode.TileFlipXY);
            graphics.DrawImage(img, tgtRect, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, wrapMode);
        }
        return tgtImg;
    }
}