I recently spent hours looking for a solution to a strange problem I had in my custom user-drawn control when using the DrawImage method, and I thought I'd share my misery and hopefully keep you from wasting your time figuring this out as well. What was happening was that using images that were not 96 dpi were scaled wrong using the DrawImage method in C# (I also tried using the DrawImageUnscaled methods to no avail). I figured that the DrawImage method should account for the different screen resolutions and I wouln't have too, but it turns out that there is a SetResolution method on the Image (Bitmap object). So I delved further on how to get the system's resolution (or dots per inch, dpi) from .NET and I ran across this page:
How to: Handle Orientation and Resolution Changes that talks about the Microsoft .NET Compact framework, but there was a very interesting tidbit near the bottom of the page:
"If your application contains graphics that are drawn in the OnPaint method, they will not scale automatically. You will need to use the DpiX and DpiY properties of your Graphics objects to determine appropriate scaling."
That was all I needed, one line in my OnPaint method and my images are now scaled properly:
(g is my Graphics object)
this.myBitmap.SetResolution(g.DpiX, g.DpiY);
It's amazing how long it takes to find the answer to questions when you don't know how to ask google for them!
Sunday, July 30, 2006
Subscribe to:
Post Comments (Atom)

1 comments:
Great! I've been searching for this. Thanks a lot!
Post a Comment