Masutatsu
Masutatsu

Reputation: 444

How can I set a transparent background colour for a WebBrowser in VB.Net?

I'm using a web-browser to load up images from the web automatically in a VB.Net forms applications, however, there is a white background where the image doesn't fill the whole of the navigator object on the form.

How can I go about setting a transparent background for the web browser object in my application?

Thanks,
C.

Upvotes: 3

Views: 5075

Answers (1)

Afnan Makhdoom
Afnan Makhdoom

Reputation: 654

Set the form's transparency key to white. The color you choose as the transparency key is transparent-ed out. Anything on the entire form with that color is turned into transparent. As the browser's background is of white color, a white transparency key will make it transparent, you can use Windows Aero Glass DWM effect for a glassy transparency but it would only work on Windows Vista onwards, for previous version of Windows, you'll have to paint it manually which is a long job to do. The simplest and the most quickest thing for you is to set the Transparency Key to White :)

Me.TransparencyKey = Color.White

enter image description here

You can set the TransparencyKey in the form's properties

If you want to use Aero Glass DWM, use the code below:

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Runtime.InteropServices

Private mExtendedFrameMargins As MARGINS

Protected Overrides Sub _
OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
    'use either one
    e.Graphics.SmoothingMode = SmoothingMode.HighQuality
End Sub

Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    If IsGlassEnabled() Then
        'You should paint the extended frame black for proper composition, but I'm painting it white as you need it
        e.Graphics.FillRectangle(Brushes.White, 0, 0, Me.ClientRectangle.Width, mExtendedFrameMargins.cyTopHeight)
    End If
End Sub

Private Function IsGlassEnabled() As Boolean
    If Environment.OSVersion.Version.Major < 6 Then
        Return False
    End If

    Dim isGlassSupported As Boolean = False
    DwmIsCompositionEnabled(isGlassSupported)
    Return isGlassSupported
End Function

<DllImport("dwmapi.dll")> _
Private Shared Function DwmIsCompositionEnabled(<MarshalAs(UnmanagedType.Bool)> ByRef pfEnabled As Boolean) As Integer
End Function

<DllImport("dwmapi.dll")> _
Private Shared Function DwmExtendFrameIntoClientArea(ByVal hwnd As IntPtr, ByRef pMarInset As MARGINS) As Integer
End Function


<StructLayout(LayoutKind.Sequential)> _
Private Structure MARGINS
    Public cxLeftWidth As Integer
    Public cxRightWidth As Integer
    Public cyTopHeight As Integer
    Public cyBottomHeight As Integer
End Structure



Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 If IsGlassEnabled() Then
  mExtendedFrameMargins = New MARGINS
  mExtendedFrameMargins.cyTopHeight = Me.Height 'type height here, this is going to be a number (integer)
  DwmExtendFrameIntoClientArea(Me.Handle, mExtendedFrameMargins)
 End If
End Sub

I've used this code in an app that I'm creating enter image description here

Upvotes: 3

Related Questions