0

I have a PNG image of resolution 6000x4000, on which I have to draw upon. So I load the image into a pictureBox with of size 1280x800. After drawing on it, I need to save the PNG image in its original resolution of 6000x4000. So I reload it into a new bitmap of size 6000x4000 using

btm = new Bitmap(6000, 4000);
image = Graphics.FromImage(btm);
g.DrawImage(btm, Point.Empty);

And save it using

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

Now I end up with the a white background png image of resolution 6000x4000 but with the edited image of 1280x800 on it like this Saved Image

How do I resize the image back to its original (6000x4000) size. Thank you.

Also please find my 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) );
        }
SwamJay
  • 27
  • 3
  • This should be simple. DrawImage has many overloads. Some of them allow to specify destination size. – Al Kepp Nov 11 '17 at 00:35
  • Thank you, I'll look into it. Could you provide me some examples to achieve this if possible? – SwamJay Nov 11 '17 at 00:44
  • **1)** if you're calling `CreateGraphics()`, you're doing it wrong. **2)** to scale a bitmap when drawing, just set the transform for the `Graphics` object or use a `DrawImage()` overload that lets you specify the target rectangle for the drawing. **3)** the code you posted is strange, even beyond calling `CreateGraphics()`; you draw `b` into `btm`, but then you try to draw `btm` back over the `PictureBox` control? **4)** if you want a good answer, you need to provide a good [mcve] that reliably reproduces your problem. – Peter Duniho Nov 11 '17 at 01:07
  • Hi the reason I'm doing that is, During mouse move event and inside picture box I have a functionality to paint on the image. As per your suggestion, I'll update the code part of the question so as to make it more clear. – SwamJay Nov 11 '17 at 21:49

2 Answers2

0

What about this? g.DrawImage(btm, new Rectangle(0,0,6000,4000));

Al Kepp
  • 5,831
  • 2
  • 28
  • 48
  • Yeah I tried using, g.DrawImage(btm, new Rectangle(0,0, 6000, 4000)); but I end up with the same result. – SwamJay Nov 11 '17 at 00:42
0

Below is a method that may help you with images upscaling/downscaling. Note though that big upscaling ratio will cause serious image distortions in any case. You may also try to play with other then graphics.CompositingMode properties like graphics.CompositingQuality You will need following usings:

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

The method works fine in my app.

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