-
April 5th, 2004, 07:24 AM
#1
StretchBlt
I am trying to use stretchBlt in VB.net and cannot get it to work. What I am doing is taking a screenshot of the desktop and trying to resize it to a different resolution size. Ex. 1024 X 768 to 800 X 600. Here is the code I am using. Any help would be appriciated.
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Integer) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Integer) As Long
Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Private Const SRCCOPY = &HCC0020
Private Hgt As Integer = 600
Private Wid As Integer = 800
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myImage As Image = New Bitmap(Wid, Hgt)
Dim desktop_win As Long
Dim desktop_dc As Long
Dim desktop_wid As Long
Dim desktop_hgt As Long
desktop_win = GetDesktopWindow()
desktop_dc = GetDC(desktop_win)
Dim gr1 As Graphics = Graphics.FromImage(myImage)
Dim dc1 As IntPtr = gr1.GetHdc()
StretchBlt(dc1.ToInt64, 0, 0, 800, 600, desktop_dc, 0, 0,
1024, 768, SRCCOPY)
myImage.Save("C:\tempDll.jpg",
System.Drawing.Imaging.ImageFormat.Jpeg)
gr1.ReleaseHdc(dc1)
End Sub
-
April 5th, 2004, 08:12 AM
#2
Change all your "As Long" to "As Integer", the datatypes are different from vb6 to .NET
-
April 5th, 2004, 09:42 AM
#3
Thanks for your quick Reply DSJ but it did not work. I changed all "AS Long" to "As Integer" and no dice.
-
April 5th, 2004, 09:44 AM
#4
I don't see where you are attaching "myImage" to the desktop's screenshot.
-
April 5th, 2004, 09:51 AM
#5
Gizmo. I thought that is what StretchBlt would do. I am new to the StretchBlt and BitBlt methods and am playing with them trying to learn them. Am I gonna have to use BitBlt first then StretchBlt to stretch it? I got BitBlt to work but I need to stretch the image.
Thanks in advance.
-
April 5th, 2004, 10:25 AM
#6
Once you have the screenshot of your screen captured as an image into "myImage", resizing is a simple job
grfx.drawimage(myImage,0,0,NewWidth,NewHeight)
where grfx is a new Graphics into which you draw the entire image.
I am not sure how to copy a screenshot. May be somebody else can help you with it.
-
April 5th, 2004, 11:46 AM
#7
Here's code to capture the window, given a hWnd, don't know about resizing though:
Private Declare Function GetDesktopWindow Lib "user32" () As Integer
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Integer) As Integer
Private Declare Function GetDC Lib "user32" (ByVal hwnd 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 xSrc As Integer, ByVal ySrc As Integer, ByVal dwRop As Integer) As Integer
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Integer, ByRef lpRect As RECT) As Integer
Private Const SRCCOPY = &HCC0020
Private Structure RECT
Dim Left As Integer
Dim Top As Integer
Dim Right As Integer
Dim Bottom As Integer
End Structure
Private Sub ScreenCapture(ByVal hWnd As Integer)
'Create a bitmap big enough to hold picture
Dim Rec As RECT
GetWindowRect(hWnd, Rec)
Dim myImage As Image = New Bitmap(Rec.Right - Rec.Left, Rec.Bottom - Rec.Top)
'Get the graphics object for the bitmap
Dim gr1 As Graphics = Graphics.FromImage(myImage)
'Get the device Context from the graphics object
Dim dc1 As IntPtr = gr1.GetHdc()
'Get the DC of the window you want to capture
Dim dc2 As Integer = GetWindowDC(hWnd) '***Use GetDC if you only want Client Portion of window...
'Copy the Other windows DC into the one we created
BitBlt(dc1.ToInt64, 0, 0, Rec.Right - Rec.Left, Rec.Bottom - Rec.Top, dc2, 0, 0, SRCCOPY)
'cleanup
gr1.ReleaseHdc(dc1)
'Save the bitmap...
myImage.Save("C:\temp.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
End Sub
-
April 5th, 2004, 12:17 PM
#8
Thanks for all your help guys but I need to use stretchBlt. I can use BitBlt and it works fine. I looked into stretchBlt and have some code in VB 6.0 but it no work with .Net.
-
April 5th, 2004, 01:35 PM
#9
If you need to resize DSJ's image, let me know. I don't understand why you must use stretchblt.
-
April 5th, 2004, 02:26 PM
#10
Thanks. I can get the desktop and I used your method of graphics.drawimage to resize (and it worked. Thanks) but I hear that stretchBlt is better quality. That is why I am trying to use it.
-
April 6th, 2004, 07:49 AM
#11
OK, if you must, then you can try this site (http://www.planet-source-code.com/vb...=309&lngWId=10) to get the code without Stretchblt. After downloading, replace the line containing
"r = bltblt(....)" in the capturescreen sub with
"r= STretchblt(...)" from your code
Be sure to include Private Declare StretchBlt... in the declaration area and replace "long" with "integer".
I don't know if it does exactly what you want to do. But you can play around with it and the Stretchblt parameters to do what you want.
Good Luck.
-
April 6th, 2004, 09:22 AM
#12
Thanks Gizmo. Worked like a charm.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|