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