_ Public Class TransparentPicturebox Inherits PictureBox Public Sub New() Me.SetStyle(ControlStyles.Opaque, True) Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False) End Sub Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams Get Dim cp As CreateParams = MyBase.CreateParams cp.ExStyle = cp.ExStyle Or &H20 ' Turn on WS_EX_TRANSPARENT Return cp End Get End Property Protected Overrides Sub OnPaint(e As PaintEventArgs) If _image IsNot Nothing Then e.Graphics.DrawImage(_image, ImageRectangleFromSizeMode(SizeMode)) End If End Sub Protected Overrides Sub OnPaintBackground(e As PaintEventArgs) End Sub Private _image As Image = Nothing Public Overloads Property Image() As Image Get Return _image End Get Set(value As Image) _image = value MyBase.RecreateHandle() End Set End Property Private Function DeflateRect(rect As Rectangle, padding As Padding) As Rectangle rect.X += padding.Left rect.Y += padding.Top rect.Width -= padding.Horizontal rect.Height -= padding.Vertical Return rect End Function Private Function ImageRectangleFromSizeMode(mode As PictureBoxSizeMode) As Rectangle Dim result As Rectangle = DeflateRect(ClientRectangle, Padding) If _image IsNot Nothing Then Select Case mode Case PictureBoxSizeMode.Normal, PictureBoxSizeMode.AutoSize ' Use image's size rather than client size. result.Size = _image.Size Exit Select Case PictureBoxSizeMode.StretchImage ' Do nothing, was initialized to the available dimensions. Exit Select Case PictureBoxSizeMode.CenterImage ' Center within the available space. result.X = CInt(result.X + ((result.Width - _image.Width) / 2)) result.Y = CInt(result.Y + ((result.Height - _image.Height) / 2)) result.Size = _image.Size Exit Select Case PictureBoxSizeMode.Zoom Dim imageSize As Size = _image.Size Dim ratio As Single = Math.Min(CSng(ClientRectangle.Width) / CSng(imageSize.Width), CSng(ClientRectangle.Height) / CSng(imageSize.Height)) result.Width = CInt(imageSize.Width * ratio) result.Height = CInt(imageSize.Height * ratio) result.X = CInt((ClientRectangle.Width - result.Width) / 2) result.Y = CInt((ClientRectangle.Height - result.Height) / 2) Exit Select Case Else Exit Select End Select End If Return result End Function End Class