Getting zoomed background image

bufer24

Newcomer
Joined
Jun 5, 2008
Hi there,
I have a picturebox with BackgroundImageLayout set to Zoom. So when I resize the picturebox, the background resizes too maintaining its aspect ratio. How do I get the zoomed image instead of the original background image?
 

snarfblam

Ultimate Contributor
Joined
Jun 10, 2003
Location
USA
Not sure what you mean by "get the zoomed image." Do you want to create a scaled-up copy of the bitmap? If so, the Bitmap class has a constructor that creates a scaled copy. For instance, to create a 2x scaled bitmap, you could do this:
Code:
// C#
System.Drawing.Bitmap scaledImage = new System.Drawing.Bitmap(originalImage, new Size(originalImage.Width * 2, originalImage.Height * 2));

' VB
Dim scaledImage As New System.Drawing.Bitmap(originalImage, New Size(originalImage.Width * 2, originalImage.Height * 2))

But unless you're looking to save the scaled image to a file, it may not make much sense to create it in the first place. If you are using it for drawing, the Graphics.DrawImage method has overloads that allow you to draw an image scaled on the fly, without creating new copies of the image.
 

bufer24

Newcomer
Joined
Jun 5, 2008
Yes I need a scaled bitmap. Imagine this:
1- put a picturebox (width=200, height=100) onto a form in designer
2 - choose a background image (image width=50, image height=300) for the picturebox
3 - set the backgroundimagelayout property to "zoom"

You´ll see that the background image zoomed nicely to fit in the picturebox by maintaining aspect ratio (ratio between width and height).

My question is: how to get the image that I actually see in the picturebox?

Sure, one way is to compare the aspect ratio of original image with the aspect ratio of the picturebox, calculate the actual size and then scale the original image to that. But is there some easier way?
 

snarfblam

Ultimate Contributor
Joined
Jun 10, 2003
Location
USA
Well... you need to know what size you want the image to be. Other than that, it only takes one line of code, as shown above. I don't think you can ask for anything easier.
 

boops boops

Newcomer
Joined
Jun 27, 2007
It's a bit late because I don't visit this forum much any more, but I'm amazed no one has posted the simple answer to your question: use the PictureBox.DrawToBitmap method. If you need a code example, post again.

BB
 
Top Bottom