Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My application creates a chart in Excel, then exports the chart as a .png file.

It works fine the first time i generate a chart, But when i do it a second time i get this error.

An unhandled exception of type System.Runtime.InteropServices.ExternalException; occurred in System.Drawing.dll
Additional information: A generic error occurred in GDI+

Code where i get an error.

Dim workbook As New Workbook()
       Dim sheet As Worksheet = workbook.Worksheets(0)
       Dim imgs As Image() = workbook.SaveChartAsImage(sheet)
        imgs(0).Save(String.Format("img-0.png", ImageFormat.Png)) 'Gets first chart from excel (Error on this line)

       Me.PictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
       Me.PictureBox2.TabIndex = 1
       Me.PictureBox2.TabStop = False

       PictureBox2.Image = Image.FromFile("img-0.png")


What I have tried:

I tried to dispose the bitmap/image, But i have no clue how to successfully do it. I Also tried to copy the image and use the copied image in my picturebox. This allows me to generate charts a second time before the error occurs.

I think i know what the problem is (Bitmap is busy somewhere), but i do not understand to fully resolve it.
Updated 5-Jul-22 1:25am

1 solution

Hi, very simple solution: delete the old file with same name before save again. It solved the same issue in my case
Share this answer

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

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