Search code examples
vb.netpaneldrawtobitmap

DrawtoBitmap not showing contents of Panel


My first post here so here goes,

i have a program that i need to get the color of a pixel in a panel, the panel contains a IE browser as shown in the code below (I cant use a webBrowser as it doesn't load some webpages the same as IE/Firefox (usually java))

    Dim TheBrowser = New SHDocVw.InternetExplorer
    asd = TheBrowser.HWND
    fgh = Me.Panel2.Handle
    SetParent(asd, fgh)
    SendMessage(asd, WM_SYSCOMMAND, SC_MAXIMIZE, 0)

This bit works Fine, however the user can change the entire forms Opacity so to get the Pixel color i found that if i use DrawtoBitmap on the Form it ignores the Opacity, so i can use this to get the pixel color, however the drawtobitmap is leaving the contents of the Panel blank (I've used a picturebox to test the image)

    Dim mybitmap As New Bitmap(Me.Width, Me.Height)
    Me.DrawToBitmap(mybitmap, Me.ClientRectangle)
    PictureBox1.BackgroundImage = mybitmap

the form and all its buttons etc are shown fine on the picturebox but the panel is blank (Ive all tried to convert just the Panel to a bitmap to but that comes out completely blank). Am i linking the IE to the Panel incorrectly or is it just DrawtoPanel not working right? (I tried using Printscreen but as expected it includes the Opacity). heres my Sendmessage and Setparent just for completness.

<DllImport("user32.dll")> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
End Function
 <DllImport("user32.dll")> _
Public Shared Function SetParent(ByVal HWNDChild As IntPtr, ByVal HWNDNewParent As IntPtr) As IntPtr
End Function

Any help would be greatly appreciated, Thanks Ryan


Solution

  • Solved it, don't need the Drawtobitmap now, Tried this a bit ago but had the GetWindowDC set with IntPtr.Zero which uses the entire screen not just the Form and so included the Background colors of windows.

    Dim hdc As IntPtr = GetWindowDC(Form2.Handle)
    Dim pixel As UInteger = GetPixel(hdc, x - Form2.Top, y - Form2.Left)
    Dim color As Color
    
    ReleaseDC(Form2.Handle, hdc)
    color = color.FromArgb(Int(pixel And &HFF), _
    Int(pixel And &HFF00) >> 8, _
    Int(pixel And &HFF0000) >> 16)
    

    now it doesnt care if the Form Opacity is set to 0 it still finds the color on the Form.