Results 1 to 4 of 4

Thread: How To: Fake a png alpha-blending background...

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    13

    How To: Fake a png alpha-blending background...

    I have a way to fake a png background for a form. I am sure other people have thought of this though it works perfect for stationary windows or splash screens.

    It looks like this:



    I can't upload my actual source code because the attach file icon is X'ed out.


    paste this into a module:

    I don't take credit for the screenshot code. I downloaded it from codeproject.com I think but I'm not sure.

    Code:
    'Imports System.Runtime.InteropServices
    'Imports System.Text
    
    Public Class modScreenCapture
    
        ' The ScreenCapture class allows you to take screenshots (printscreens)
        ' of the desktop or of individual windows.
        '
        ' Usage:
        '
        ' PictureBox1.Image = ScreenCapture.GrabScreen()
        ' PictureBox1.Image = ScreenCapture.GrabActiveWindow()
        ' PictureBox1.Image = ScreenCapture.GrabWindow(SomeHwnd)
        '
        ' PictureBox1.Image = ScreenCapture.GrabScreen(X, Y, Width, Height)
        ' PictureBox1.Image = ScreenCapture.GrabScreen(Rect)
        ' PictureBox1.Image = ScreenCapture.GrabScreen(Location, Size)
    
    
    #Region "Constants"
    
        Private Const HORZRES As Integer = 8
        Private Const VERTRES As Integer = 10
        Private Const SRCCOPY = &HCC0020
        Private Const SRCINVERT = &H660046
    
        Private Const USE_SCREEN_WIDTH = -1
        Private Const USE_SCREEN_HEIGHT = -1
    
    #End Region
    
    #Region "API's"
    
        Private Structure RECT
            Public Left As Int32
            Public Top As Int32
            Public Right As Int32
            Public Bottom As Int32
        End Structure
    
        Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As String) As Integer
        Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Integer) As Integer
        Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Integer) As Integer
        Private Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As Integer) As Integer
        Private Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As Integer, ByVal hdc As Integer) As Long
        Private Declare Function GetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As Integer, ByVal nIndex As Integer) As Integer
        Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer
        Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer
        Private Declare Function DeleteObject Lib "GDI32" (ByVal hObj As Integer) As Integer
        Private Declare Function BitBlt Lib "GDI32" (ByVal hDestDC As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal SrcX As Integer, ByVal SrcY As Integer, ByVal Rop As Integer) As Integer
        Private Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As Integer
        Private Declare Function IsWindow Lib "user32" Alias "IsWindow" (ByVal hwnd As Integer) As Long
        Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hWnd As Integer, ByRef lpRect As RECT) As Int32
        Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Integer) As Long
    
    #End Region
    
    #Region "Public Methods."
    
        Public Shared Function GrabScreen() As Bitmap
    
            Return GrabScreen(0, 0, USE_SCREEN_WIDTH, USE_SCREEN_HEIGHT)
    
        End Function
    
        Public Shared Function GrabScreen(ByVal Rect As Rectangle) As Bitmap
    
            Return GrabScreen(Rect.X, Rect.Y, Rect.Width, Rect.Height)
    
        End Function
    
        Public Shared Function GrabScreen(ByVal Location As System.Drawing.Point, ByVal Size As System.Drawing.Size) As Bitmap
    
            Return GrabScreen(Location.X, Location.Y, Size.Width, Size.Height)
    
        End Function
    
        Public Shared Function GrabScreen(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer) As Bitmap
    
            Dim hDesktopDC As Integer
            Dim hOffscreenDC As Integer
            Dim hBitmap As Integer
            Dim hOldBmp As Integer
            Dim MyBitmap As Bitmap
    
            ' Get the desktop device context.
            hDesktopDC = CreateDC("DISPLAY", "", "", "")
            If hDesktopDC Then
                ' Adjust width and height.
                If Width = USE_SCREEN_WIDTH Then
                    Width = GetDeviceCaps(hDesktopDC, HORZRES)
                End If
                If Height = USE_SCREEN_HEIGHT Then
                    Height = GetDeviceCaps(hDesktopDC, VERTRES)
                End If
                ' Create an offscreen device context.
                hOffscreenDC = CreateCompatibleDC(hDesktopDC)
                If hOffscreenDC Then
                    ' Create a bitmap for our offscreen device context.
                    hBitmap = CreateCompatibleBitmap(hDesktopDC, Width, Height)
                    If hBitmap Then
                        ' Copy the image and create an instance of the Bitmap class.
                        hOldBmp = SelectObject(hOffscreenDC, hBitmap)
                        BitBlt(hOffscreenDC, 0, 0, Width, Height, hDesktopDC, X, Y, SRCCOPY)
                        MyBitmap = Bitmap.FromHbitmap(New IntPtr(hBitmap))
                        ' Clean up.
                        DeleteObject(SelectObject(hOffscreenDC, hOldBmp))
                    End If
                    DeleteDC(hOffscreenDC)
                End If
                DeleteDC(hDesktopDC)
            End If
            ' Return our Bitmap instance.
            Return MyBitmap
    
        End Function
    
        Public Shared Function GrabActiveWindow() As Bitmap
    
            Return GrabWindow(GetForegroundWindow())
    
        End Function
    
        Public Shared Function GrabWindow(ByVal hWnd As Integer) As Bitmap
    
            Dim hWindowDC As Integer
            Dim hOffscreenDC As Long
            Dim rec As RECT
            Dim nWidth As Long
            Dim nHeight As Long
            Dim hBitmap As Long
            Dim hOldBmp As Long
            Dim MyBitmap As Bitmap
    
    
            ' Verify if a valid window handle was provided.
            If hWnd <> 0 And IsWindow(hWnd) Then
                ' Get the window's device context.
                hWindowDC = GetWindowDC(hWnd)
                If hWindowDC Then
                    ' Get width and height.
                    If GetWindowRect(hWnd, rec) Then
                        nWidth = rec.Right - rec.Left
                        nHeight = rec.Bottom - rec.Top
    
                        ' Create an offscreen device context.
                        hOffscreenDC = CreateCompatibleDC(hWindowDC)
                        If hOffscreenDC Then
                            ' Create a bitmap for our offscreen device context.
                            hBitmap = CreateCompatibleBitmap(hWindowDC, nWidth, nHeight)
                            If hBitmap Then
                                ' Copy the image and create an instance of the Bitmap class.
                                hOldBmp = SelectObject(hOffscreenDC, hBitmap)
                                BitBlt(hOffscreenDC, 0, 0, nWidth, nHeight, hWindowDC, 0, 0, SRCCOPY)
                                MyBitmap = Bitmap.FromHbitmap(New IntPtr(hBitmap))
                                ' Clean up.
                                DeleteObject(SelectObject(hOffscreenDC, hOldBmp))
                            End If
                            DeleteDC(hOffscreenDC)
                        End If
                    End If
                    ReleaseDC(hWnd, hWindowDC)
                End If
            End If
            ' Return our Bitmap instance.
            Return MyBitmap
    
        End Function
    
    #End Region
    
    End Class
    and put this in a timer event:

    set the timer event to 100

    set the transparency key below to whatever it is on your form.

    Code:
    Me.TransparencyKey = Color.Magenta
    Me.BackgroundImage = modScreenCapture.GrabScreen(Me.Left, Me.Top, Me.Width, Me.Height)
    make sure the transparency is set on your form and the background color

    add a picturebox to your form and load a png, set the background color to transparent. Make sure that the png file doesn't have a background color already in it.


    All this is doing is taking a screenshot of the desktop and setting it to the background image of the form. Use this for a splashscreen or something because if you try to move the form it doesn't update fast enough.

    I know this is a round about way of doing this, but it's easy to do and understand. This is just a work around for me until I understand GDI+ and setting pixels. It works for me.
    Last edited by mtlca401; Jul 13th, 2007 at 02:12 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width