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

问题描述:

我有一张分辨率为6000x4000的PNG图像,我必须借助它。所以我把图片加载到1280x800大小的图片框中。绘制完成后,我需要将PNG图像保存为原始分辨率6000x4000。所以我将其重新装入尺寸6000x4000的新位图使用在WinForms中将调整大小后的位图图像调回原始大小

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)。谢谢。

也请找我codebelow

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)); 
     } 
+0

这应该很简单。 DrawImage有许多重载。其中一些允许指定目的地大小。 –

+0

谢谢,我会研究它。如果可能,你能否提供一些例子来达到这个目的? – SwamJay

+0

** 1)**如果你打电话给'CreateGraphics()',你做错了。 ** 2)**在绘制时缩放位图,只需设置Graphics对象的变换或使用“DrawImage()”重载,以便指定图形的目标矩形。 ** 3)**你发布的代码很奇怪,除了调用'CreateGraphics()';你把'b'绘制成'btm',但是你试图在'PictureBox'控件上画'btm'? ** 4)**如果您想要一个很好的答案,您需要提供一个良好的[mcve],可以可靠地再现您的问题。 –

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

+0

是的,我试过使用,g.DrawImage(btm,new Rectangle(0,0,6000,4000));但我最终得到了同样的结果。 – SwamJay

下面是一个可以帮助您进行图像放大/缩小的方法。 请注意,尽管如此大的放大倍率在任何情况下都会导致严重的图像失真。 你也可以尝试与其他然后graphics.CompositingMode性能如graphics.CompositingQuality玩 您将需要以下usings:

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; 
    } 
}