Capture screen and save image.

M

Mahesh

Hello All,
I'm fairly new to System.Windows.Forms namespace.
I want to capture a screenshot of a form and want to save
it. I got a rough idea as to how to do it thru this link:
http://www.c-
sharpcorner.com/Code/2002/April/ScreenCaptureUtility.asp

But the developer has used Win32 API. I wanted to write a
pure .NET code. I tried to modify this code like this.

public void saveImage(string strFileName)
{
Graphics formGraphics = this.CreateGraphics();
Graphics fileGraphics = null;
try
{
Image objImage
= new Bitmap((int)formGraphics.VisibleClipBounds.Width,
(int)formGraphics.VisibleClipBounds.Height,formGraphics);
fileGraphics = Graphics.FromImage(objImage);
fileGraphics.DrawImage
(objImage,0,0,formGraphics.VisibleClipBounds.Width,formGrap
hics.VisibleClipBounds.Height);
objImage.Save("MyForm.jpg",ImageFormat.Jpeg);
}
finally
{
graphics.Dispose();
if(fileGraphics != null)
fileGraphics.Dispose();
}
}

Though this code is not croaking, but the image is not
saved. There is just a blank (black)image in place which
is saved. I don't know what I'm missing.
Is there some raster operation to be performed?

Please do let me know where my mistake is.

Thanx in advance.
Mahesh.
 
H

Herfried K. Wagner [MVP]

* "Mahesh said:
I'm fairly new to System.Windows.Forms namespace.
I want to capture a screenshot of a form and want to save
it. I got a rough idea as to how to do it thru this link:
http://www.c-
sharpcorner.com/Code/2002/April/ScreenCaptureUtility.asp

But the developer has used Win32 API. I wanted to write a
pure .NET code. I tried to modify this code like this.

<http://www.google.com/[email protected]>
 
R

Ron Allen

Mahesh,
While this type of code is a nice exercise, I think you would be better
off using a utility program to do this. I use HyperSnapDX from
http://www.hyperionics.com/ to produce documentation as it allows me to
capture regions of the screen and show forms with menus dropped down. The
small shareware price should be a lot less than the time/effort spent
writing something similar unless you just want to do this to learn how or
have to provide users a way to get a window snapshot without using the built
in Windows version.

Ron Allen
 
J

james

Here you go, copied from a post by Armin Zingler. And it works very good
too.
james



Meanwhile I'd declare some values as IntPtr instead of Integer (read it
somewhere):


Private Declare Function BitBlt Lib "gdi32" Alias "BitBlt" _
(ByVal hDestDC As IntPtr, ByVal x As Integer, _
ByVal y As Integer, ByVal nWidth As Integer, _
ByVal nHeight As Integer, ByVal hSrcDC As IntPtr, _
ByVal xSrc As Integer, ByVal ySrc As Integer, _
ByVal dwRop As Integer) As Integer

Private Declare Function GetDC Lib "user32" Alias "GetDC" _
(ByVal hwnd As IntPtr) As IntPtr
Private Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" _
(ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As Integer

Private Const SRCCOPY As Integer = &HCC0020

Function CreateScreenshot() As Bitmap

Dim Rect As Rectangle = Screen.PrimaryScreen.Bounds
Dim gDest As Graphics
Dim hdcDest, hdcSrc As IntPtr

CreateScreenshot = New Bitmap(Rect.Right, Rect.Bottom)
gDest = gDest.FromImage(CreateScreenshot)

hdcSrc = GetDC(IntPtr.Zero)
hdcDest = gDest.GetHdc
BitBlt(hdcDest, 0, 0, _
Rect.Right, Rect.Bottom, hdcSrc, 0, 0, SRCCOPY)
gDest.ReleaseHdc(hdcDest)
ReleaseDC(IntPtr.Zero, hdcSrc)

End Function


Private Sub Button1_Click( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click

Dim bmp As Bitmap = CreateScreenshot()
bmp.Save("c:\shot.bmp")
End Sub

Armin
 
M

Mahesh

Thanx for a readymade example ! It is working fine. But it
a'ain uses Win32 API and I wanted to used DrawImage method
of Graphics object.
Anyway, one more thing I wanted to ask is about
ClientRectangle property of any control. It gives the
visible client rectangle of the control. I wanted to have
an image of a form which has a scrollbar. The
ClientRectangle and ClientSize gives me only the visisble
area's size and not of the full control. Is there any such
property which gives me the size of the entire document..?

Thanx once a'ain.
Mahesh
 
M

Mahesh

I was making some mistakes in releasing dc handles. Now,
it is working fine. But it a'ain uses Win32 API and I
wanted to used DrawImage method of Graphics object.

Anyway, one more thing I wanted to ask is about
ClientRectangle property of any control. It gives the
visible client rectangle of the control. I wanted to have
an image of a form which has a scrollbar. The
ClientRectangle and ClientSize gives me only the visisble
area's size and not of the full control. Is there any such
property which gives me the size of the entire document..?

Thanx once a'ain.
Mahesh
 
H

Herfried K. Wagner [MVP]

* "Mahesh said:
I was making some mistakes in releasing dc handles. Now,
it is working fine. But it a'ain uses Win32 API and I
wanted to used DrawImage method of Graphics object.

Just FYI: This method AFAIS doesn't allow you to capture the visible
areas of the control.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top