Click here to Skip to main content
14,664,980 members
Rate this:
Please Sign up or sign in to vote.
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()
       workbook.LoadFromFile("Test.xls")
       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")


Thanks

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.
Posted

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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100