StretchBlt
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: StretchBlt

  1. #1
    Join Date
    Apr 2004
    Posts
    10

    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

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    Change all your "As Long" to "As Integer", the datatypes are different from vb6 to .NET

  3. #3
    Join Date
    Apr 2004
    Posts
    10
    Thanks for your quick Reply DSJ but it did not work. I changed all "AS Long" to "As Integer" and no dice.

  4. #4
    Join Date
    Dec 2002
    Posts
    305
    I don't see where you are attaching "myImage" to the desktop's screenshot.

  5. #5
    Join Date
    Apr 2004
    Posts
    10
    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.

  6. #6
    Join Date
    Dec 2002
    Posts
    305
    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.

  7. #7
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    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

  8. #8
    Join Date
    Apr 2004
    Posts
    10
    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.

  9. #9
    Join Date
    Dec 2002
    Posts
    305
    If you need to resize DSJ's image, let me know. I don't understand why you must use stretchblt.

  10. #10
    Join Date
    Apr 2004
    Posts
    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.

  11. #11
    Join Date
    Dec 2002
    Posts
    305
    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.

  12. #12
    Join Date
    Apr 2004
    Posts
    10
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center