0

I have a Bitmap and a Panel. Both are 500 x 500 px. Why is the bitmap so much bigger than the panel?

Code sample:

Bitmap img = new Bitmap(500, 500);
//panel properties set by properties tab
Graphics g = Graphics.FromImage(img);
g.DrawEllipse(new Pen(Color.Black, 2), 0, 0, 500, 500);
pnl.BackgroundImage = img;
pnl.Refresh();

What am I doing wrong / missing? Thanks in advance!

  • Make sure it has the same dpi as the screen ! – TaW Oct 24 '16 at 21:05
  • how to I change that value? – Kenneth McKanders Oct 24 '16 at 21:07
  • You can for example do a `this.CreateGraphics` to get at the current dpi values: `Bitmap img = new Bitmap(500, 500); using (Graphics g = this.CreateGraphics()) img.SetResolution(g.DpiX, g.DpiY); ` – TaW Oct 24 '16 at 21:08
  • Hm, looking at your code again, I wonder where your problem comes from, Since you don't set the dpi and create the image from scratch it already has the screen dpi. And it works just fine here but for one pixel, which is the pen.aligmnment problem with DrawRectangle or Drawellipse.. So there there must other things be happening in your code which you don't show! – TaW Oct 24 '16 at 21:15
  • @TaW only thing I haven't show is: pnl.BackgroundImageLayout is set to Center, and pnl.Size is set to 500, 500. – Kenneth McKanders Oct 24 '16 at 21:20
  • 2
    Setting a control size does not mean much today, especially since programmers are updating their machine to Windows 10. VS is a dpiAware app, most Winforms apps are not. So the actual panel size will be smaller at runtime, typically by 96 / 125. As it will be when you run the program on an older machine. The code is too fake to give a lead to talk about writing dpiAware code. Just [make your app dpiAware](http://stackoverflow.com/questions/13228185/how-to-configure-an-app-to-run-correctly-on-a-machine-with-a-high-dpi-setting-e/13228495#13228495) and you can continue to ignore it. – Hans Passant Oct 24 '16 at 21:50
  • @Hans Passant the post you linked was very informative, and solved my question. Can you make this a formal answer please? – Kenneth McKanders Oct 25 '16 at 15:09

0 Answers0