Click here to Skip to main content
15,922,007 members
Please Sign up or sign in to vote.
4.50/5 (3 votes)
See more:
I have looked around but nothing seemed to really match my problem.
I am trying to save a picture from a direct link. Either as a gif or png depending on what is needed. Every time I try to save the file I get said error.
C#
public void SaveImage(String ID, string pokemonName, bool isShiny, ImageFormat imageFormat, bool isSprite = false)
{
    Directory.CreateDirectory(@"C:\Users\Cardon\Documents\Pokemon\" + ID + " - " + pokemonName);
    var tmpImageFormat = (imageFormat == ImageFormat.Gif) ? "gif" : "png";
    var url = (isSprite)
                  ? "http://www.serebii.net/pokedex-bw/icon/" + ID + "." + tmpImageFormat
                  : (isShiny) ? "http://www.serebii.net/Shiny/BW/" + ID + "." + tmpImageFormat
                  : "http://www.serebii.net/blackwhite/pokemon/" + ID + "." + tmpImageFormat;
    var pictureBox = new PictureBox();
    pictureBox.Load(url);
    if (pictureBox.ImageLocation == null) return;
    pictureBox.Image.Save(@"C:\Users\Cardon\Documents\Pokemon\" + ID + " - " + pokemonName, imageFormat);
}

System.Runtime.InteropServices.ExternalException was unhandled
  Message=A generic error occurred in GDI+.
  Source=System.Drawing
  ErrorCode=-2147467259
  StackTrace:
       at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
       at PokemonDataExtractor.frmMain.SaveImage(String ID, String pokemonName, Boolean isShiny, ImageFormat imageFormat, Boolean isSprite) in C:\Users\Cardon\Desktop\PokemonDataExtractor\PokemonDataExtractor\frmMain.cs:line 297
       at PokemonDataExtractor.frmMain.btnExtract_Click(Object sender, EventArgs e) in C:\Users\Cardon\Desktop\PokemonDataExtractor\PokemonDataExtractor\frmMain.cs:line 79
       at System.Windows.Forms.Control.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.Control.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.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at PokemonDataExtractor.Program.Main() in C:\Users\Cardon\Desktop\PokemonDataExtractor\PokemonDataExtractor\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

The error only occurres when I try to save the image, some say it is because of write access but I am positive that my account has write has write access to those files. Even though I am positive I still may be wrong due to wrong thinking.
Any solution would be greatly appreciated.

Thanks in Advance.
Posted
Updated 5-Jun-11 19:53pm
v3
Comments
Member 12408392 11-Jul-17 5:29am    
this is my code


protected void Button1_Click1(object sender, EventArgs e)
{
Capture(@"C:/ScreenShot.Bmp");//path to Save Captured files
}
public static void Capture(string CapturedFilePath)
{
try
{
Bitmap bitmap = new Bitmap
(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

Graphics graphics = Graphics.FromImage(bitmap as System.Drawing.Image);
graphics.CopyFromScreen(25, 25, 25, 25, bitmap.Size);

bitmap.Save(CapturedFilePath, ImageFormat.Bmp);
}
catch (Exception ex)
{
throw ex;
}
}

am facing the A generic error occurred in GDI+


can u please sort out this....

1 solution

How come you did not report the problem properly? Run it under debugger and see in what line you got an exception. Catch all exceptions and look at exception's stack and all inner exceptions. Better yet, dump all exception information to the file. If it does not help you to find out the solution, ask the question. Use "Improve question", see above.

I hope you write hard-coded directory path names and URLs just for testing. There are no situations where hard-coded path names can work in applications. All path names should be calculated during run-time based on data input from configuration files or taken from the users via file dialogs. Naturally, you need to present exception information to the user.

If this is UI application, you need to catch all the exception of the main UI cycle.
You will find out how to do it here:
When i run an application an exception is caught how to handle this?[^]. See my solution.

To find out more info on how to get all exception information and report the issue properly, see my other past answer:
How do i make a loop that will stop when a scrollbar reaches the bottom[^]. See also general direction on exception use.

—SA
 
Share this answer
 
Comments
Cardon Fry 6-Jun-11 1:52am    
The reason that it is hard-coded is because it is a personal application that I am using for a one time scrape of data, it will not be sent to users as it is really only meant for me to use once to grab the data off of 649 pages.
I already know what line it is, as I said "when I try to save the image", it should be obvious that is is pictureBox.Image.Save, which is the last line in the method. I did go through debugging as I always do when I run into problems, but the error is something that is so general it doesn't give any real information as to the problem.
Anyways I have updated the question with the exceptions details.
Sergey Alexandrovich Kryukov 6-Jun-11 11:29am    
Your code is different from what is shown in exception stack (line 297), as PokemonDataExtractor.frmMain.SaveImage has different signature. Where is this code line? Also, did you check inner exceptions?
Yes, it is not I/O exception, so it should not be a problem of permission. (You can always check up if you have required permission for the directory if you create a file manually.)
--SA
Cardon Fry 6-Jun-11 16:23pm    
I got rid of some code I commented from when I tried a way to get around the exception. But it is that very last line of code every time.
Sergey Alexandrovich Kryukov 6-Jun-11 21:41pm    
I don't understand. Look at my comment above. In exception stack there is one method, in your code fragment it's another one. How can you explain that? You don't need to get around exception, you need to get exception and handle it, to see what's going on. No, please show the code as is with matching stack, otherwise what the use? Also, I don't see InnerException. The problem must be described in InnerException, I guess.
"Very last line" is interesting fact.
--SA
Cardon Fry 6-Jun-11 22:28pm    
Thank you SO much, I went back and looked at your comment carefully rereading it several times, taking a look at the exception stack and my code then I noticed the problem. I was trying to save the image as if it was the folder I just created causing it to throw the exception. I have fixed this and have given your solution a 5. I am sorry, sometimes I can just be so blind.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900