【问题标题】:Resizing a resized bitmap image back to original size in WinForms在 WinForms 中将调整大小的位图图像调整回原始大小
【发布时间】:2017-11-11 00:29:59
【问题描述】:

我有一个分辨率为 6000x4000 的 PNG 图像,我必须在其上进行绘制。所以我将图像加载到大小为 1280x800 的图片框中。在绘制之后,我需要将 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) );
        }

【问题讨论】:

  • 这应该很简单。 DrawImage 有很多重载。其中一些允许指定目标大小。
  • 谢谢,我会调查的。如果可能的话,你能给我一些例子来实现这一点吗?
  • 1) 如果你打电话给CreateGraphics(),那你就错了。 2) 要在绘图时缩放位图,只需为Graphics 对象设置变换或使用DrawImage() 重载,让您指定绘图的目标矩形。 3) 你发布的代码很奇怪,甚至超出了调用CreateGraphics();您将b 绘制到btm 中,然后您尝试将btm 绘制回PictureBox 控件上? 4) 如果您想要一个好的答案,您需要提供一个好的minimal reproducible example 以可靠地重现您的问题。
  • 嗨,我这样做的原因是,在鼠标移动事件和图片框内,我有一个在图像上绘制的功能。根据您的建议,我将更新问题的代码部分,以使其更加清晰。

标签: c# winforms bitmap picturebox


【解决方案1】:

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

【讨论】:

  • 是的,我尝试使用 g.DrawImage(btm, new Rectangle(0,0, 6000, 4000));但我最终得到了相同的结果。
【解决方案2】:

以下是一种可以帮助您放大/缩小图像的方法。 请注意,尽管如此大的放大率在任何情况下都会导致严重的图像失真。 您也可以尝试使用其他 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;
    }
}

【讨论】:

    猜你喜欢
    • 2011-10-25
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    相关资源
    最近更新 更多