Resize panel image and maintain aspect ratio

C

Carl Gilbert

Hi

I am looking for either a component or technique to allow me to do the
following:

* Provide a panel with a background image
* Resize the image to best fit the panel to maintain aspect ratio
* Provide white (or other color) borders at the sides or the top/bottom

The last point would be used to allow users to resize the panel to any ratio
whilst maintaining the images ratio.

Regards, Carl Gilbert
 
P

Peter Proost

Hi,

maybe this can help you, place a panel on a form and then two picturboxes in
the panel,
send the second picturebox to the back. For both pictureboxes chose dock =
fill and for
the panel anchor the top, bottom,left and right. set an image in the image
property of the second
picturebox, copy paste the code below and run the app and resize the form to
see what happens.
Don't mind about the comments in the code, they're in dutch. I know it's not
everything you asked, but it can be a start

hth Peter

<<<<<code>>>>>>



Private bmp As Bitmap
Private ptbBreedte, ptbHoogte As Integer
Private imgBr, imgHo As Integer
Private schaalH, schaalB, schaal As Double

Private Sub Form1_Resize(ByVal sender As Object, ByVal e As
System.EventArgs) _ Handles MyBase.Resize
Try

ptbBreedte = PictureBox1.Width
ptbHoogte = PictureBox1.Height
imgBr = PictureBox2.Image.Width
imgHo = PictureBox2.Image.Height
schaalH = ptbHoogte / imgHo
schaalB = ptbBreedte / imgBr
'schalen gebaseerd op wie de grootste aanpassing moet doen, de hoogte
'of de breedte, in dit geval kan de schaal niet gebaseerd worden op de
'breedte en hoogte van de image omdat de picturebox geresized kan
worden
if schaalH > schaalB Then
schaal = ptbBreedte / imgBr
Else
schaal = ptbHoogte / imgHo
End If

bmp = New Bitmap(Panel1.Width, Panel1.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.Clear(Color.White)
g.DrawImage(PictureBox2.Image, New Rectangle(5, 5, CInt(imgBr *
schaal) - 10, _ CInt(imgHo * schaal) - 10), 0, 0, PictureBox2.Image.Width,
PictureBox2.Image.Height, GraphicsUnit.Pixel)
PictureBox1.Image = bmp
g.Dispose()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

<<<<<<<<code>>>>>>>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top