Problems with Bitblt/Memory

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
The below code works with some programs like the calculator but not with other programs. Here is the problem:
1) I save the Window Image to the Desktop
2) When I try to save an updated Image of that same Window to the Desktop it saves the initial Image only and not the updated one
This problem happens each time I reopen the program I am trying to obtain the image from.
Why would this program happen to some windows but not others?


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Imports System.Runtime.InteropServices
<span style="color:Blue; Imports System.Diagnostics
<span style="color:Blue; Imports System.Runtime.CompilerServices
<span style="color:Blue; Imports System.Drawing
<span style="color:Blue; Imports System.Windows.Forms

<span style="color:Blue; Public <span style="color:Blue; Class Form1

<span style="color:Blue; Private <span style="color:Blue; Declare <span style="color:Blue; Function FindWindow <span style="color:Blue; Lib <span style="color:#A31515; "user32" <span style="color:Blue; Alias <span style="color:#A31515; "FindWindowA" (<span style="color:Blue; ByVal lpClassName <span style="color:Blue; As <span style="color:Blue; String, <span style="color:Blue; ByVal lpWindowName <span style="color:Blue; As <span style="color:Blue; String) <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Private <span style="color:Blue; Declare <span style="color:Blue; Function SetForegroundWindow <span style="color:Blue; Lib <span style="color:#A31515; "user32" <span style="color:Blue; Alias <span style="color:#A31515; "SetForegroundWindow" (<span style="color:Blue; ByVal hWnd <span style="color:Blue; As <span style="color:Blue; Integer) <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Private <span style="color:Blue; Declare <span style="color:Blue; Function GetWindowRect <span style="color:Blue; Lib <span style="color:#A31515; "user32" (<span style="color:Blue; ByVal hwnd <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByRef lpRect <span style="color:Blue; As RECT) <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Private <span style="color:Blue; Declare <span style="color:Blue; Function GetWindowDC <span style="color:Blue; Lib <span style="color:#A31515; "user32" (<span style="color:Blue; ByVal hwnd <span style="color:Blue; As <span style="color:Blue; Integer) <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Private <span style="color:Blue; Declare <span style="color:Blue; Function BitBlt <span style="color:Blue; Lib <span style="color:#A31515; "gdi32" (<span style="color:Blue; ByVal hDestDC <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal x <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal y <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal nWidth <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal nHeight <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal hSrcDC <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal xSrc <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal ySrc <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal dwRop <span style="color:Blue; As <span style="color:Blue; Integer) <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Private <span style="color:Blue; Declare <span style="color:Blue; Function DeleteDC <span style="color:Blue; Lib <span style="color:#A31515; "GDI32" (<span style="color:Blue; ByVal hDC <span style="color:Blue; As <span style="color:Blue; Integer) <span style="color:Blue; As <span style="color:Blue; Long
<span style="color:Blue; Private <span style="color:Blue; Declare <span style="color:Blue; Function ReleaseDC <span style="color:Blue; Lib <span style="color:#A31515; "user32" (<span style="color:Blue; ByVal hwnd <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal hdc <span style="color:Blue; As <span style="color:Blue; Integer) <span style="color:Blue; As <span style="color:Blue; Integer

<span style="color:Blue; Private <span style="color:Blue; Const SRCCOPY = &HCC0020

<span style="color:Blue; Private <span style="color:Blue; Structure RECT
<span style="color:Blue; Dim Left <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Dim Top <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Dim Right <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Dim Bottom <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; End <span style="color:Blue; Structure

<span style="color:Blue; Private <span style="color:Blue; Sub Button1_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles Button1.Click
<span style="color:Blue; Dim hWnd <span style="color:Blue; As <span style="color:Blue; Long
<span style="color:Blue; Dim gr1 <span style="color:Blue; As Graphics
<span style="color:Blue; Dim dc1 <span style="color:Blue; As IntPtr
<span style="color:Blue; Dim dc2 <span style="color:Blue; As <span style="color:Blue; Long
<span style="color:Blue; Dim r <span style="color:Blue; As <span style="color:Blue; Long = 0
<span style="color:Blue; Dim Rec <span style="color:Blue; As RECT

<span style="color:Green; Obtain Window Handle
hWnd = FindWindow(vbNullString, <span style="color:#A31515; "Calculator")

<span style="color:Green; Bring Window to the Top
SetForegroundWindow(hWnd)

<span style="color:Green; Get Window Dimentions
GetWindowRect(hWnd, Rec)

<span style="color:Green; New Bitmap
<span style="color:Blue; Dim Window_Image <span style="color:Blue; As <span style="color:Blue; New Bitmap(Rec.Right - Rec.Left, Rec.Bottom - Rec.Top)

<span style="color:Green; Get the graphics object for the bitmap
gr1 = Graphics.FromImage(Window_Image)

<span style="color:Green; Get the device Context from the graphics object
dc1 = gr1.GetHdc()

<span style="color:Green; Get the DC of the Window
dc2 = GetWindowDC(hWnd)

<span style="color:Green; Copy the other windows DC into the one we created
BitBlt(dc1, 0, 0, Rec.Right - Rec.Left, Rec.Bottom - Rec.Top, dc2, 0, 0, SRCCOPY)

<span style="color:Green; Clean up
r = DeleteDC(dc1)
r = ReleaseDC(hWnd, dc2)
gr1.ReleaseHdc(dc1)

<span style="color:Green; Save the picture as a bitmap to the Desktop
Window_Image.Save(<span style="color:#A31515; "C:DesktopPic.bmp", System.Drawing.Imaging.ImageFormat.Bmp)

<span style="color:Blue; End
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; End <span style="color:Blue; Class
[/code]
<br/>
<br/>


View the full article
 
Back
Top