P
Pohihihi
I am getting a generic GDI+ error on following code. Basically it is trying
to capture screen image and save in a file.
Is there a way to find more details on this error? Thanks for the help.
---------------------------
ERROR MSG
---------------------------
System.Runtime.InteropServices.ExternalException: A generic error occurred
in GDI+.
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder,
EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at ScreenCapture.Form1.TakeSnapNow() in c:\visual studio
projects\screencapture\form1.cs:line 102
Sorry for the formating below.
//First, you need to import the BitBlt, GetDC and ReleaseDC functions.
//imports the GDI BitBlt function that enables the background of the window
//to be captured
[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
);
[DllImport("User32.dll")]
public extern static System.IntPtr GetDC(System.IntPtr hWnd);
[DllImport("User32.dll")]
public extern static int ReleaseDC(System.IntPtr hWnd, System.IntPtr hDC);
//modified to include hWnd
//Now, to capture the screen in it's entirety you can do the following.
public void TakeSnapNow()
{
System.IntPtr desktopDC=GetDC(System.IntPtr.Zero);
Bitmap bm=new Bitmap(SystemInformation.VirtualScreen.Width,
SystemInformation.VirtualScreen.Height);
try
{
Graphics g=Graphics.FromImage(bm);
System.IntPtr bmDC=g.GetHdc();
BitBlt(bmDC,0,0,bm.Width,bm.Height,desktopDC,0,0,0x00CC0020 /*SRCCOPY*/);
bm.Save("C:\file.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
ReleaseDC(System.IntPtr.Zero, desktopDC);
g.ReleaseHdc(bmDC);
g.Dispose();
}
catch(Exception er)
{
MessageBox.Show(er.ToString());
}
}
to capture screen image and save in a file.
Is there a way to find more details on this error? Thanks for the help.
---------------------------
ERROR MSG
---------------------------
System.Runtime.InteropServices.ExternalException: A generic error occurred
in GDI+.
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder,
EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at ScreenCapture.Form1.TakeSnapNow() in c:\visual studio
projects\screencapture\form1.cs:line 102
Sorry for the formating below.
//First, you need to import the BitBlt, GetDC and ReleaseDC functions.
//imports the GDI BitBlt function that enables the background of the window
//to be captured
[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
);
[DllImport("User32.dll")]
public extern static System.IntPtr GetDC(System.IntPtr hWnd);
[DllImport("User32.dll")]
public extern static int ReleaseDC(System.IntPtr hWnd, System.IntPtr hDC);
//modified to include hWnd
//Now, to capture the screen in it's entirety you can do the following.
public void TakeSnapNow()
{
System.IntPtr desktopDC=GetDC(System.IntPtr.Zero);
Bitmap bm=new Bitmap(SystemInformation.VirtualScreen.Width,
SystemInformation.VirtualScreen.Height);
try
{
Graphics g=Graphics.FromImage(bm);
System.IntPtr bmDC=g.GetHdc();
BitBlt(bmDC,0,0,bm.Width,bm.Height,desktopDC,0,0,0x00CC0020 /*SRCCOPY*/);
bm.Save("C:\file.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
ReleaseDC(System.IntPtr.Zero, desktopDC);
g.ReleaseHdc(bmDC);
g.Dispose();
}
catch(Exception er)
{
MessageBox.Show(er.ToString());
}
}