473,327 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

GDI+ problem

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,0 x00CC0020 /*SRCCOPY*/);

bm.Save("C:\file.bmp",System.Drawing.Imaging.Image Format.Bmp);

ReleaseDC(System.IntPtr.Zero, desktopDC);

g.ReleaseHdc(bmDC);

g.Dispose();

}

catch(Exception er)

{

MessageBox.Show(er.ToString());

}

}

Nov 17 '05 #1
5 2660


"Pohihihi" wrote:
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,0 x00CC0020 /*SRCCOPY*/);

bm.Save("C:\file.bmp",System.Drawing.Imaging.Image Format.Bmp);

ReleaseDC(System.IntPtr.Zero, desktopDC);

g.ReleaseHdc(bmDC);

g.Dispose();

}

catch(Exception er)

{

MessageBox.Show(er.ToString());

}

}

Nov 17 '05 #2
Hi Pohihihi,
on the line:

bm.Save("C:\file.bmp",System.Drawing.Imaging.Image Format.Bmp);

I believe that having a single \ will cause problems because \<char> is
regarded as an escape character.

Try:
bm.Save(@"C:\file.bmp",System.Drawing.Imaging.Imag eFormat.Bmp);
which the @ means use the literal value of the string ignoring escape
characters

or
bm.Save("C:\\file.bmp",System.Drawing.Imaging.Imag eFormat.Bmp);
Hope that helps.
Mark

"Pohihihi" wrote:
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,0 x00CC0020 /*SRCCOPY*/);

bm.Save("C:\file.bmp",System.Drawing.Imaging.Image Format.Bmp);

ReleaseDC(System.IntPtr.Zero, desktopDC);

g.ReleaseHdc(bmDC);

g.Dispose();

}

catch(Exception er)

{

MessageBox.Show(er.ToString());

}

}

Nov 17 '05 #3
Thanks Mark that was the real cause. I was looking all around to find the
problem.
I am still interested in learning how to trap errors while using interops.
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:51**********************************@microsof t.com...
Hi Pohihihi,
on the line:

bm.Save("C:\file.bmp",System.Drawing.Imaging.Image Format.Bmp);

I believe that having a single \ will cause problems because \<char> is
regarded as an escape character.

Try:
bm.Save(@"C:\file.bmp",System.Drawing.Imaging.Imag eFormat.Bmp);
which the @ means use the literal value of the string ignoring escape
characters

or
bm.Save("C:\\file.bmp",System.Drawing.Imaging.Imag eFormat.Bmp);
Hope that helps.
Mark

"Pohihihi" wrote:
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,0 x00CC0020 /*SRCCOPY*/);

bm.Save("C:\file.bmp",System.Drawing.Imaging.Image Format.Bmp);

ReleaseDC(System.IntPtr.Zero, desktopDC);

g.ReleaseHdc(bmDC);

g.Dispose();

}

catch(Exception er)

{

MessageBox.Show(er.ToString());

}

}

Nov 17 '05 #4
For looking what the exact error was through the interop services you can get
the last error from the Win32 calls, although you cannot call the
GetLastError method directly, you must go through a method called
GetLastWin32Error.

Basically make sure you have imported the namespace
System.Runtime.InteropServices then when you import a DLL you need to set the
SetLastError property to true i.e.

[DllImport("gdi32.dll", SetLastError=true)]

Then when you have an exception you can do:

intError = Marshal.GetLastWin32Error();

which returns the error code.

I am not an expert in this, but this might be enough to get you pointed in
the right direction.

Hope that helps
Mark.

"Pohihihi" wrote:
Thanks Mark that was the real cause. I was looking all around to find the
problem.
I am still interested in learning how to trap errors while using interops.
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:51**********************************@microsof t.com...
Hi Pohihihi,
on the line:

bm.Save("C:\file.bmp",System.Drawing.Imaging.Image Format.Bmp);

I believe that having a single \ will cause problems because \<char> is
regarded as an escape character.

Try:
bm.Save(@"C:\file.bmp",System.Drawing.Imaging.Imag eFormat.Bmp);
which the @ means use the literal value of the string ignoring escape
characters

or
bm.Save("C:\\file.bmp",System.Drawing.Imaging.Imag eFormat.Bmp);
Hope that helps.
Mark

"Pohihihi" wrote:
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,0 x00CC0020 /*SRCCOPY*/);

bm.Save("C:\file.bmp",System.Drawing.Imaging.Image Format.Bmp);

ReleaseDC(System.IntPtr.Zero, desktopDC);

g.ReleaseHdc(bmDC);

g.Dispose();

}

catch(Exception er)

{

MessageBox.Show(er.ToString());

}

}


Nov 17 '05 #5
Thanks Mark, it is a nice start for me, I will dig more in interops on MSDN.
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:EF**********************************@microsof t.com...
For looking what the exact error was through the interop services you can
get
the last error from the Win32 calls, although you cannot call the
GetLastError method directly, you must go through a method called
GetLastWin32Error.

Basically make sure you have imported the namespace
System.Runtime.InteropServices then when you import a DLL you need to set
the
SetLastError property to true i.e.

[DllImport("gdi32.dll", SetLastError=true)]

Then when you have an exception you can do:

intError = Marshal.GetLastWin32Error();

which returns the error code.

I am not an expert in this, but this might be enough to get you pointed in
the right direction.

Hope that helps
Mark.

"Pohihihi" wrote:
Thanks Mark that was the real cause. I was looking all around to find the
problem.
I am still interested in learning how to trap errors while using
interops.
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:51**********************************@microsof t.com...
> Hi Pohihihi,
> on the line:
>
> bm.Save("C:\file.bmp",System.Drawing.Imaging.Image Format.Bmp);
>
> I believe that having a single \ will cause problems because \<char> is
> regarded as an escape character.
>
> Try:
> bm.Save(@"C:\file.bmp",System.Drawing.Imaging.Imag eFormat.Bmp);
> which the @ means use the literal value of the string ignoring escape
> characters
>
> or
> bm.Save("C:\\file.bmp",System.Drawing.Imaging.Imag eFormat.Bmp);
>
>
> Hope that helps.
> Mark
>
> "Pohihihi" wrote:
>
>> 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,0 x00CC0020
>> /*SRCCOPY*/);
>>
>> bm.Save("C:\file.bmp",System.Drawing.Imaging.Image Format.Bmp);
>>
>> ReleaseDC(System.IntPtr.Zero, desktopDC);
>>
>> g.ReleaseHdc(bmDC);
>>
>> g.Dispose();
>>
>> }
>>
>> catch(Exception er)
>>
>> {
>>
>> MessageBox.Show(er.ToString());
>>
>> }
>>
>> }
>>
>>
>>
>>


Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Kennedy [UB] | last post by:
Hi Everyone, I have this multithreaded C# windows forms application which does a lot of image processing. Occasionally, I get the following error: A generic error occurred in GDI+....
10
by: **ham | last post by:
I know that's an old dirty issue; GDI+ almost -the slowest part of the framework - has bothered many developers using it in animations. Even in managed C++ the performance is awful. Now, any dude...
15
by: Wiktor Zychla | last post by:
today we've found a critical issue regarding the ListView from Windows.Forms. it was confirmed on several machines with Win2K and XP. here's the problem: create a ListView with about 50000 rows....
1
by: pei_world | last post by:
Hi there, I have a problem about how to keep my draw graphics stay on my control unless I delete them. that means how to resist my graphics. also how to partially remove some graphics from my...
7
by: news | last post by:
This may be a stupid question, but if I don't ask I'll never know ;) Ok, here it goes.... I am writing an application that renders an image in one picturebox and a graph in another. The image...
13
by: lgbjr | last post by:
Hello All, I have some pictureboxes on a VB.NET form that are linked to an AccessDB. If the user wishes to open or edit an image, I need to save the image in the picturebox to a temp file, then...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
7
by: Marcin Rzeznicki | last post by:
Hello, Do you think it is legitimate practice to mix GDI+ and GDI calls (via Get/ReleaseHDC()) in paint event of a control? I've heard there is possibility of performance loss while "locking"...
5
by: Jonathan Boivin | last post by:
Hi, I've got some problems with loading bills using a bill usercontrol I built. If I load all current bills in my test environment (156) everything is fine once, but repeating the operation...
7
by: =?Utf-8?B?Um9oaXQ=?= | last post by:
I have a timer object that calls UpdateWindow(). The WM_TIMER message is processed in the main Win32 message loop for the window. However, when I run the app, the image doesn't get updated (there...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.