Results 1 to 8 of 8

Thread: create a bitmap from a rectangle of the screen (resolved)

  1. #1

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Resolved create a bitmap from a rectangle of the screen (resolved)

    Hi all,

    how do I define a rectangle of a portion of that sceen and create a bitmap from it?
    kevin
    Last edited by kebo; May 24th, 2005 at 08:40 AM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: create a bitmap from a rectangle of the screen

    This code might be helpful to you...

    VB Code:
    1. Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) As Integer
    2.     Declare Function BitBlt Lib "gdi32" Alias "BitBlt" (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
    3.  
    4.     Public Function ScreenGrab(ByVal SourceRect As Rectangle) As Bitmap
    5.         'returns a screengrab of a known area of the desktop
    6.  
    7.         Dim destBMP As Bitmap = New Bitmap(SourceRect.Width, SourceRect.Height)
    8.         Dim gDest As Graphics = Graphics.FromImage(destBMP)
    9.         Dim DestHDC As IntPtr = gDest.GetHdc()
    10.  
    11.         'copy the pixel data
    12.         BitBlt(DestHDC.ToInt32, 0, 0, SourceRect.Width, SourceRect.Height, GetDC(0), SourceRect.Left, SourceRect.Top, &HCC0020)
    13.  
    14.         gDest.ReleaseHdc(DestHDC)
    15.         gDest.Dispose()
    16.  
    17.         Return destBMP
    18.  
    19.     End Function

    and here is a demo of how to use it...

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.  
    3.         Dim MyRect As Rectangle = New Rectangle(0, Screen.PrimaryScreen.Bounds.Bottom - 100, 100, 100)
    4.         Dim MyPic As Bitmap
    5.  
    6.         'take the snapshot and store it in a bitmap object
    7.         MyPic = ScreenGrab(MyRect)
    8.  
    9.         'save it to disc
    10.         MyPic.Save("C:\TestScreenGrabImage.png", Drawing.Imaging.ImageFormat.Png)
    11.  
    12.         MessageBox.Show("Screenshot saved!")
    13.  
    14.     End Sub

    On my machine I get this image...
    Attached Images Attached Images  
    Last edited by wossname; May 24th, 2005 at 08:35 AM.
    I don't live here any more.

  3. #3

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: create a bitmap from a rectangle of the screen

    cool beans wossy!!! I had been working with your BlitterChip class, but couldn't figure it out. Your code does it perfectly!
    thanks
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: create a bitmap from a rectangle of the screen (resolved)

    Glad to help

    Blitterchip was intended to be used for copying image data between 2 controls on the same form, rather than what you wanted but now you have mentioned it I'll add this sub to that class and revise the codebank thread
    Last edited by wossname; May 24th, 2005 at 08:52 AM.
    I don't live here any more.

  5. #5

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: create a bitmap from a rectangle of the screen (resolved)

    well, none really, I just could figure out how to make it do what I needed. I think it is more about not understanding how Bitblt works. It makes a bit more sense now though.
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  6. #6

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: create a bitmap from a rectangle of the screen (resolved)

    Quote Originally Posted by wossname
    Glad to help

    Blitterchip was intended to be used for copying image data between 2 controls on the same form, rather than what you wanted but now you have mentioned it I'll add this sub to that class and revise the codebank thread
    cool
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  7. #7
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: create a bitmap from a rectangle of the screen (resolved)

    Done.

    http://www.vbforums.com/showthread.p...=1#post2023863

    This must be worth a rating
    I don't live here any more.

  8. #8

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: create a bitmap from a rectangle of the screen (resolved)

    Quote Originally Posted by wossname
    Done.
    ...
    This must be worth a rating
    It asolutely is..... I need to spread some around first though. I'll be back.
    Thanks again. With your help I was able to taken dozens of screen shot of my app in only a few minutes. Way kick @ss cool I say!
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

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