picture box problem

  • Thread starter Thread starter Tim Mulholland
  • Start date Start date
T

Tim Mulholland

I have a picturebox with a simple icon in it. When i set the border of the
picturebox to be outlined, the image shrinks just a little bit, but
noticably.
Is there anyway to make this not happen? its sort of distracting to have the
picture change like that.

Thanks in advance,


Tim
 
I noticed that i could do that and it didn't seem to change.
But i need the image to be the size of the picturebox all the time, and to
grow as the picturebox grows - so its my understanding i have to have
SizeMode set to StretchImage, correct?
 
Tim,

If I correctly understand what you're saying, you have the SizeMode property
set to StretchImage because you need the image to resize with the
PictureBox. Unfortunately as you noticed the image is automatically resized
when you change the value of the BorderStyle property from BorderStyle.None
to BorderStyle.Fixed3D (or BorderStyle.FixedSingle).

One way to work around this problem is to manually handle the drawing of
the border, by implementing the Paint event.

First of all you need to keep track of the status of the border, and a
boolean variable will do the job (if you are planning to use more than one
instance of this PictureBox I would recommend that you create a user
control). To display the border you set this variable to True (or False to
hide) and then force a synchronous paint of the PictureBox control.

In the Paint event of the PictureBox all you have to do is check if the
border should be visible, and draw it with the help of the ControlPaint
class.

Here's a sample:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
....
#End Region

Dim UseBorder As Boolean = False

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

' display border
UseBorder = True

' invalidate region and force a paint
PictureBox1.Invalidate()
PictureBox1.Update()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

' hide border
UseBorder = False

' invalidate region and force a paint
PictureBox1.Invalidate()
PictureBox1.Update()

End Sub

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

Dim r As System.Drawing.Rectangle
Dim s As System.Windows.Forms.Border3DStyle

' set dimensions
r.Width = PictureBox1.Width
r.Height = PictureBox1.Height

' check if border is required
If UseBorder Then

' draw a 3D style sunken border
s = Border3DStyle.Sunken
ControlPaint.DrawBorder3D(e.Graphics, r, s)

End If

End Sub

End Class

Regards,

Gabriele
 
You confirmed my fears - i was hoping to not have to handle the drawing
manually, but i guess i'll have to. At least its not that hard to do.
Thanks for your help and the great example!

Tim
 
You confirmed my fears - i was hoping to not have to handle the
drawing manually, but i guess i'll have to. At least its not that hard
to do. Thanks for your help and the great example!

Maybe this is an obvious comment, but if you are allowing the image to be
stretched, why are you concerned about it starting a bit shrunken when you
start?? Can't you just increase the size of the picturebox just slightly
so that the image comes out the size you want it when you start??

-mbray
 
Back
Top