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