Quick way to perform screen region capture in C# 
Author Message
 Quick way to perform screen region capture in C#

All,

I am looking for a quick way to capture a portion of a
screen in C# and then redisplay that portion.  I have
looked through the Graphics class and do not see a simple
mechanism for performing this, presumably, basic task.  

Just to be perfectly clear, I want to:
1.  Capture a small portion of the screen by marking a
block with the cursor.
2.  Save the captured portion to some an internal image
object (Image class leaps to mind).
3.  Display the captured region wherever I wish (like
simple Paint program block moves).

Any simple way to do this?  Thanks in advance.



Sun, 28 Nov 2004 04:18:32 GMT  
 Quick way to perform screen region capture in C#
Hello Amram,

You may implement this function with the steps below:

1. Import the BitBlt and GetDesktopWindow API functions
2. Get the handle of desktop
3. Calculate the region to capture
4. Capture the image of the form save it to an image

This is a snip of code:

[DllImport("user32.dll")]
private static extern IntPtr GetDesktopWindow();

[DllImport("gdi32.dll")]
private static extern bool BitBlt(
        IntPtr hdcDest, // handle to destination DC
        int nXDest, // x-coord of destination upper-left corner
        int nYDest, // y-coord of destination upper-left corner
        int nWidth, // width of destination rectangle
        int nHeight, // height of destination rectangle
        IntPtr hdcSrc, // handle to source DC
        int nXSrc, // x-coordinate of source upper-left corner
        int nYSrc, // y-coordinate of source upper-left corner
        System.Int32 dwRop // raster operation code
        );

private const Int32 SRCCOPY = 0xCC0020;

private Bitmap memImage;

private void Capture ()
{
        Graphics graphic = this.CreateGraphics();
        Size s = this.Size;
        memImage = new Bitmap(s.Width, s.Height, graphic);
        Graphics memGraphic = Graphics.FromImage(memImage);
        IntPtr dc1 = graphic.GetHdc();
        IntPtr dc2 = memGraphic.GetHdc();
        //You should specify the region to capture in this function
        BitBlt(dc2, 0, 0, this.ClientRectangle.Width,
this.ClientRectangle.Height,dc1, 0, 0, SRCCOPY);
        graphic.ReleaseHdc(dc1);
        memGraphic.ReleaseHdc(dc2);

Quote:
}

I hope this is helpful.

Best regards,

Lion Shi, MCSE, MCSD
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.  2001 Microsoft Corporation. All rights
reserved.
--------------------
    Content-Class: urn:content-classes:message


    Subject: Quick way to perform screen region capture in C#
    Date: Tue, 11 Jun 2002 13:18:32 -0700
    Lines: 16

    MIME-Version: 1.0
    Content-Type: text/plain;
        charset="iso-8859-1"
    Content-Transfer-Encoding: 7bit
    X-Newsreader: Microsoft CDO for Windows 2000
    X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
    Thread-Index: AcIRhSg2SCKUItABSem/Dud7e69HuA==
    Newsgroups: microsoft.public.dotnet.languages.csharp
    NNTP-Posting-Host: TKMSFTNGXA06 10.201.232.165
    Path: cpmsftngxa08!cpmsftngxa07
    Xref: cpmsftngxa08 microsoft.public.dotnet.languages.csharp:67182
    X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

    All,

    I am looking for a quick way to capture a portion of a
    screen in C# and then redisplay that portion.  I have
    looked through the Graphics class and do not see a simple
    mechanism for performing this, presumably, basic task.  

    Just to be perfectly clear, I want to:
    1.  Capture a small portion of the screen by marking a
    block with the cursor.
    2.  Save the captured portion to some an internal image
    object (Image class leaps to mind).
    3.  Display the captured region wherever I wish (like
    simple Paint program block moves).

    Any simple way to do this?  Thanks in advance.



Mon, 29 Nov 2004 15:44:49 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. does the LineTo perform a quick clip arithmetic?

2. client region, frame region, windows region?

3. needed: a good quick .CHM quick ref of C#

4. Perform a screen grab of a view

5. direct access to memory region where console-screen (win32) is located

6. Print a screen region

7. How to move a region of Screen ?

8. Need to perform CD in XP burning using C#

9. Windows Forms performance in C# with irregular edges and regions

10. Anything resembling #Region in C# ?

11. Quick C with RS232 and Direct Screen I/O

12. Capturing a screen.

 

 
Powered by phpBB® Forum Software