locked
draw an image in original pixel size RRS feed

  • Question

  • Hi,

    I'd like to draw an image in original size (in pixels). According to doc DrawImage uses physical size by default. does that mean that:
    Code Snippet

    e.Graphics.DrawImage(image, new Rectangle(new Point(x, y), image.Size), new Rectangle(new Point(0, 0), image.Size), GraphicsUnit.Pixel);


    is the simplest way to do this, or am I missing something here?
    Sunday, August 19, 2007 8:29 PM

Answers

  • MSFT developers don't come around here anymore.  I wrote a test program to check this.  DrawImage(Image, int, int) does indeed do the exact same thing as DrawImageUnscaled(). 

    This was my test program:

      public partial class Form1 : Form {
        private Bitmap mBmp;
        public Form1() {
          InitializeComponent();
          mBmp = new Bitmap(@"c:\temp\test1.bmp");
        }
        private void Form1_Paint(object sender, PaintEventArgs e) {
          if (chkUnscaled.Checked) e.Graphics.DrawImageUnscaled(mBmp, 0, 0);
          else e.Graphics.DrawImage(mBmp, 0, 0);
        }
        private void chkUnscaled_CheckedChanged(object sender, EventArgs e) {
          this.Refresh();
        }
        private void chkRes_CheckedChanged(object sender, EventArgs e) {
          if (chkRes.Checked) mBmp.SetResolution(120, 120);
          else mBmp.SetResolution(96, 96);
          this.Refresh();
        }
      }

    Replace the code in the Paint event to draw the image one-pixel to one-pixel, regardless of the resolution with this:

          e.Graphics.DrawImage(mBmp, new Rectangle(0, 0, mBmp.Width, mBmp.Height));
    Monday, August 20, 2007 4:15 PM

All replies

  • Just use e.Graphics.DrawImage(image, x, y).  One pixel in the image will be rendered as one pixel on the Graphics context.  To render images in their original physical size, you'd have to use Graphics.DrawImageUnscaled().
    Sunday, August 19, 2007 9:06 PM
  • But The doc says "Draws the specified image, using its original physical size, at the location specified by a coordinate pair".
    Sunday, August 19, 2007 9:24 PM
  • I stand corrected, learn something every day.  Now I wonder what DrawImageUnscaled(Image, int, int) does.  Please post back and let me know.
    Sunday, August 19, 2007 10:26 PM
  • I don't notice a difference between DrawImage and DrawImageUnscaled... This is very weird... Perhaps one of the Microsoft Developers can solve this mystery?
    Monday, August 20, 2007 3:34 PM
  • MSFT developers don't come around here anymore.  I wrote a test program to check this.  DrawImage(Image, int, int) does indeed do the exact same thing as DrawImageUnscaled(). 

    This was my test program:

      public partial class Form1 : Form {
        private Bitmap mBmp;
        public Form1() {
          InitializeComponent();
          mBmp = new Bitmap(@"c:\temp\test1.bmp");
        }
        private void Form1_Paint(object sender, PaintEventArgs e) {
          if (chkUnscaled.Checked) e.Graphics.DrawImageUnscaled(mBmp, 0, 0);
          else e.Graphics.DrawImage(mBmp, 0, 0);
        }
        private void chkUnscaled_CheckedChanged(object sender, EventArgs e) {
          this.Refresh();
        }
        private void chkRes_CheckedChanged(object sender, EventArgs e) {
          if (chkRes.Checked) mBmp.SetResolution(120, 120);
          else mBmp.SetResolution(96, 96);
          this.Refresh();
        }
      }

    Replace the code in the Paint event to draw the image one-pixel to one-pixel, regardless of the resolution with this:

          e.Graphics.DrawImage(mBmp, new Rectangle(0, 0, mBmp.Width, mBmp.Height));
    Monday, August 20, 2007 4:15 PM
  • Thanx.

    I notice that If I don't call SetResolution at all then:
    Code Snippet

    DrawImage(mBmp, 0, 0)



    gives the same result as
    Code Snippet

    DrawImage(mBmp, new Rectangle(0, 0,mBmp.Width, mBmp.Height))


    Is it always like this?
    Monday, August 20, 2007 5:16 PM
  • The vast majority of bitmaps are created with the default Windows video resolution, 96 dots per inch.  You will only see a difference if the resolution of the bitmap doesn't match the video resolution (Graphics.DpiX).
    Monday, August 20, 2007 6:40 PM