Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • Leaders
Posted

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:

// 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.

[sIGPIC]e[/sIGPIC]
Posted

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?

  • Leaders
Posted
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.
[sIGPIC]e[/sIGPIC]
  • 3 weeks later...
  • 2 years later...
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...