trouble saving a jpg out of a picturebox....


C

cdj

Hi all,

I've got a picturebox on a form, and a save button. When I go to save,
the app craps out with the following error:

==================
An unhandled exception of type
'System.Runtime.InteropServices.ExternalException' occurred in
system.drawing.dll

Additional information: A generic error occurred in GDI+.
==================


And here's the additional GDI+ error info:

==================
Unhandled Exception: System.Runtime.InteropServices.ExternalException:
A generic error occurred in GDI+.
at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder,
EncoderParameters encoderParams)
at System.Drawing.Image.Save(Stream stream, ImageFormat format)
at _2DFunctionPlot.Form1.saveGraphicClick(Object sender, EventArgs
e) in c:\documents and settings\cdj.mobile-frege.000\stuff\c#
stuff\2dfunctionplot\form1.cs:line 1005
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at _2DFunctionPlot.Form1.Main() in c:\documents and
settings\cdj.mobile-frege.000\stuff\c#
stuff\2dfunctionplot\form1.cs:line 663The program '[5460]
2DFunctionPlot.exe' has exited with code 0 (0x0).
===============

I'm new to .net programming - this lil app is meant to be a learning
experience, in fact. Here's the code for my save routing, which is
"simply" a trivial variant of what's on MSDN:

===========================
private void saveGraphicClick(object sender, System.EventArgs e)
{
// Displays a SaveFileDialog so the user can save the Image
// assigned to Button2.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif
Image|*.gif";
saveFileDialog1.Title = "Save an Image File";
saveFileDialog1.ShowDialog();

// If the file name is not an empty string open it for saving.
if(saveFileDialog1.FileName != "")
{
// Saves the Image via a FileStream created by the OpenFile
method.
System.IO.FileStream fs =
(System.IO.FileStream)saveFileDialog1.OpenFile();
// Saves the Image in the appropriate ImageFormat based upon the
// File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch(saveFileDialog1.FilterIndex)
{
case 1 :
this.graphicsBox.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Jpeg);
break;

case 2 :
this.graphicsBox.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Bmp);
break;

case 3 :
this.graphicsBox.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Gif);
break;
}

fs.Close();
}
}
=============

The graphic I'm trying to save is always a jpg, and thus the error
always occurs in the Case1 save statement.

Does anyone know what I'm doing wrong?

(please let me know if I haven't supplied enuff/relevant info, i'll be
glad to provide it...)

thx for any insights,

cdj
 
Ad

Advertisements

A

AlexS

Can you do image.Load for same jpeg w/o error? Are you creating image
yourself or using precereated one?

I've seen cases when jpeg engine in .Net was unable to display image, which
IE or PhotoEditor were able to show just fine. With similar internal GDI
errors. I think this issue exists since Beta.

You might try to find some special way to save such problematic images. E.g.
like bitmaps, which you convert later using some other tools.

HTH
Alex

cdj said:
Hi all,

I've got a picturebox on a form, and a save button. When I go to save,
the app craps out with the following error:

==================
An unhandled exception of type
'System.Runtime.InteropServices.ExternalException' occurred in
system.drawing.dll

Additional information: A generic error occurred in GDI+.
==================


And here's the additional GDI+ error info:

==================
Unhandled Exception: System.Runtime.InteropServices.ExternalException:
A generic error occurred in GDI+.
at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder,
EncoderParameters encoderParams)
at System.Drawing.Image.Save(Stream stream, ImageFormat format)
at _2DFunctionPlot.Form1.saveGraphicClick(Object sender, EventArgs
e) in c:\documents and settings\cdj.mobile-frege.000\stuff\c#
stuff\2dfunctionplot\form1.cs:line 1005
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMetho
ds+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at _2DFunctionPlot.Form1.Main() in c:\documents and
settings\cdj.mobile-frege.000\stuff\c#
stuff\2dfunctionplot\form1.cs:line 663The program '[5460]
2DFunctionPlot.exe' has exited with code 0 (0x0).
===============

I'm new to .net programming - this lil app is meant to be a learning
experience, in fact. Here's the code for my save routing, which is
"simply" a trivial variant of what's on MSDN:

===========================
private void saveGraphicClick(object sender, System.EventArgs e)
{
// Displays a SaveFileDialog so the user can save the Image
// assigned to Button2.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif
Image|*.gif";
saveFileDialog1.Title = "Save an Image File";
saveFileDialog1.ShowDialog();

// If the file name is not an empty string open it for saving.
if(saveFileDialog1.FileName != "")
{
// Saves the Image via a FileStream created by the OpenFile
method.
System.IO.FileStream fs =
(System.IO.FileStream)saveFileDialog1.OpenFile();
// Saves the Image in the appropriate ImageFormat based upon the
// File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch(saveFileDialog1.FilterIndex)
{
case 1 :
this.graphicsBox.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Jpeg);
break;

case 2 :
this.graphicsBox.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Bmp);
break;

case 3 :
this.graphicsBox.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Gif);
break;
}

fs.Close();
}
}
=============

The graphic I'm trying to save is always a jpg, and thus the error
always occurs in the Case1 save statement.

Does anyone know what I'm doing wrong?

(please let me know if I haven't supplied enuff/relevant info, i'll be
glad to provide it...)

thx for any insights,

cdj
 
Ad

Advertisements

C

cdj

AlexS said:
Can you do image.Load for same jpeg w/o error? Are you creating image
yourself or using precereated one?

Hm. Never trid to do an image.load..... that's because the image is
being generated by another program (Mathematica), and being fed into
the picturebox. The image displays perfectly in the picturebox
everytime without fail.
You might try to find some special way to save such problematic images. E.g.
like bitmaps, which you convert later using some other tools.

o - so it might just be a problem with jps? i'll give gif and bmp a
try i guess.... i just figured i was making some kinda stupid coding
mistake.....

we'll see.... but i really appreciate your taking the time to
answer...

cdj
 

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

Similar Threads


Top