Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8889623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:18:22+00:00 2026-06-14T22:18:22+00:00

When I run my application the following error get’s thrown in my face A

  • 0

When I run my application the following error get’s thrown in my face “A generic error occurred in GDI+.”
I’ve looked around and seen people have similar errors, but have found no real solution, or it is a real pain in the butt to implement. And those that have not gotten a solution have not posted their code.

So I thought I might as well give it a shot and throw another thread up about how to fix this error. This is my Code & Error

            Random r = new Random();
            DirectoryInfo di = new DirectoryInfo(folderbrowser.SelectedPath);
            FileInfo[] fi = di.GetFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();
            for (int i = 0; i < imageCount; i++)
            {
                int img1 = r.Next(imageCount);
                int img2 = r.Next(imageCount);
                while (img2 == img1)
                    img2 = r.Next(imageCount);
                pic1.Image = Image.FromFile(fi[img1].FullName);
                pic2.Image = Image.FromFile(fi[img2].FullName);
                Image i1 = pic1.Image;
                Image i2 = pic2.Image;
                Bitmap bitmap = new Bitmap(i1.Width + i2.Width, 1080);
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.DrawImage(i1, 0, 0);
                    g.DrawImage(i2, i2.Width, 0);
                }
                bitmap.Save(@"C:\TEST\Image_"+i.ToString());                   
            }

And here’s the Error code

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 System.Drawing.Image.Save(String filename, ImageFormat format)
       at System.Drawing.Image.Save(String filename)
       at WallpaperMerger.Form1.btn_merge_Click(Object sender, EventArgs e) in C:\Users\PeppeJ\documents\visual studio 2010\Projects\WallpaperMerger\WallpaperMerger\Form1.cs:line 113
       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.Control.ControlNativeWindow.OnMessage(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(IntPtr 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 System.Windows.Forms.Application.Run(Form mainForm)
       at WallpaperMerger.Program.Main() in C:\Users\PeppeJ\documents\visual studio 2010\Projects\WallpaperMerger\WallpaperMerger\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

So what do you guys here at StackOverflow think. Am I the same boat as the others and have to type a ridiculously complex code to solve this problem, or have I made a simple mistake and it’s easy to correct?

If you want to test it yourself if that might somehow help, here’s a link to the Debug Build

Simply tick “Use Multiple Files”, select a folder containing image files (C:\Users\Public\Pictures\Sample Pictures) for instance. Hit Merge.

I’d like to add that I use Bitmap.Save in this function, and it works flawlessly.

        Bitmap bitmap = new Bitmap(pic1.Image.Width + pic2.Image.Width, 1080);
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.DrawImage(pic1.Image, 0, 0);
            g.DrawImage(pic2.Image, pic1.Image.Width, 0);
        }
        if (savepicfile.ShowDialog() == DialogResult.OK)
            bitmap.Save(savepicfile.FileName);

In the above function files are loaded like this:

    DialogResult result = pic1file.ShowDialog();
    if (result == DialogResult.OK)
        pic1.Image = Image.FromFile(pic1file.FileName);

Something else worth mentioning is that “picX” is a “System.Windows.Forms.PictureBox” object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:18:24+00:00Added an answer on June 14, 2026 at 10:18 pm

    In my experience, 99% of the time this error is caused by an issue where either:

    1. You don’t have permission to write the file.
    2. The file you’re trying to write to is locked by another process.

    Occasionally I’ve run into it when:

    • I’m drawing GraphicPaths with zero points in them.
    • I’m not property disposing of my GDI+ objects. (possibly related to option 2 above)

    I’d check the first two things first. If that can be ruled out I can see where you’re not properly disposing of a few objects in the code you provided. I’d clean that up. GDI+ isn’t particularly cooperative with .NET’s GC if you aren’t calling Dispose() when you’re supposed to.

            var r = new Random();
            var di = new DirectoryInfo(folderbrowser.SelectedPath);
            var fi = di.GetFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();
            for (var i = 0; i < imageCount; i++)
            {
                var img1 = r.Next(imageCount);
                var img2 = r.Next(imageCount);
                while (img2 == img1) 
                {
                    img2 = r.Next(imageCount);
                }
                pic1.Image = Image.FromFile(fi[img1].FullName);
                pic2.Image = Image.FromFile(fi[img2].FullName);
                var i1 = pic1.Image;
                var i2 = pic2.Image;
                using (var bitmap = new Bitmap(i1.Width + i2.Width, 1080))
                using (var g = Graphics.FromImage(bitmap))
                {
                    g.DrawImage(i1, 0, 0);
                    g.DrawImage(i2, i2.Width, 0);
                    bitmap.Save(@"C:\TEST\Image_"+i.ToString());
                }                   
            }
    

    EDIT: More ideas (Well, really clarification of number 2 above)…

    • If you have a PictureBox or other control in your app referencing the images in your C:\TEST directory that you’re trying to write, you can see this error.
    • If you have other code reading the image you’re trying to write to you can see this error.
    • If an external process, such as your IDE, an image viewer/editor, etc. has the file open you can see this error.

    Debugging ideas:

    • Try setting a break point on the Save call. When you get to it, use the tips found here to figure out what process might be accessing the file: How do I find out which process is locking a file using .NET?
    • Try just deleting the files first to see if it allows you to save if they’re not their.
    • Try setting full permissions for “Everyone” on the directory you’re writing to (Don’t leave it like this, though)
    • Make sure the width of the image you’re trying to save is a number greater than zero. I noticed you’re setting that programatically.
    • Make sure the width of the image you’re trying to save isn’t ridiculously huge (an maybe you’re running into a memory issue?)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get the following error when trying to run my application. There are no
When I run an application that uses db40 I get the following error. Any
I get the following error when I try to run my application in Visual
I get the following error when trying to build/run my application: Error 30 -
I received the following error when I tried to run a C# WinForms application
I receive the following error when trying to run my ASP.NET MVC application: The
when i run my application, i get following exception. I have no problem with
I get an error when I run my application which utilizes the OpenSL ES
i am experiencing following error while trying to run application on heroku 2011-06-03T11:24:25-07:00 heroku[nginx]:
When I try to run my application within rubyMine I get an error about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.