Why can't I reload my PictureBox?

Posted on 2010-09-20
Last Modified: 2012-05-10
Hello Everyone

I have a routine whereby Excel creates a chart which is then loaded into the PictureBox of a Form.

The PictureBox.Image is firstly Disposed. Excel is called. Excel then Exports the image of the Chart to a .PNG File. And my  routine places this .PNG file into the PictureBox.


My problem is that this only works ONCE.

If I call the same routine again, the PictureBox displays not the .PNG file, but the ErrorImage.

Now, this worked fine using VB Express on a Vista machine.

But on a Win7 machine using Visual Studio 10, the routine can only be passed through successfully ONCE.

Here is a summary of my code ...


 If Me.PictureBox1.Image IsNot Nothing Then Me.PictureBox1.Image.Dispose()

        If My.Computer.FileSystem.FileExists("TheImage.png") Then
        End If


        xl.Visible = False
        XLWorkBooks = xl.Workbooks()
        XLWorkBook = XLWorkBooks.Open("C:\oldf\MYEXCEL\progresschart.xlsm")

        Dim XLChart = XLWorkBook.ActiveChart

        XLChart.Export("TheImage.png", "png")

        PictureBox1.Image = New System.Drawing.Bitmap("TheImage.png")


The file "TheImage.png" is successfully created by Excel every time.

But, as i mentioned above, the PictureBox just will not reload it on the second time round.

Thank you

Question by:KarlTheHopeless
  • 3
  • 3
LVL 18

Accepted Solution

John (Yiannis) Toutountzoglou earned 50 total points
ID: 33722588

Expert Comment

ID: 33723239
Can you please specify the path that excel saves the png?

When you say
 PictureBox1.Image = New System.Drawing.Bitmap("TheImage.png")
the code looks in the application startup path (the path of the .exe file) for this image. Are you sure that excel saves in the same location?

To test, change your code like this:

        XLChart.Export("c:\TheImage.png", "png")
        PictureBox1.Image = New System.Drawing.Bitmap("c:\TheImage.png")

Let me know with the results.


Author Comment

ID: 33740567

The paths are fine. And the image file is definitely being created.

 Also, remember that the routine does actually work. But only ONCE!


I can't see anything in your link that does anything really different to what I am trying to do.
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.


Assisted Solution

klakkas earned 450 total points
ID: 33741754
Goodmorning Karl.

Well, the only think I can think of is that the picturebox tries to load the image before Excel has finished saving it, or before Excel releases the file.

It is kind of a longshot, but try the following 2 approaches:

Approach 1: Give Excel 5 seconds to complete
        XLChart.Export("TheImage.png", "png")
        PictureBox1.Image = New System.Drawing.Bitmap("TheImage.png")

Approach 2: Dispose the Excel object, in order to remove all locks.
        XLChart.Export("TheImage.png", "png")


        PictureBox1.Image = New System.Drawing.Bitmap("TheImage.png")
Try these 2 and hopefully we will indentify the problem. Afterwards, we can discuss how to solve it in an optimal way.

Author Comment

ID: 33754444
OK Guys

I have solved the problem - as well as the breakpoint problem mentioned here ...

Both problems arose because of (yet another) Microsoft oversight, in my view.

When programming, I like my windows to appear instantly; not artistically sliding into view; with all the special effects - which is an irritating waste of time when debugging.

To this end, I had set up my Windows 7 using the 'classic' Windows Theme.

This was the problem.

By changing the theme to one of the more modern, artistic themes, both of the above problems went away.

(But now, of course, I have these irritatingly slow windows to contend with.)



Author Closing Comment

ID: 33754470
Thank you.

Expert Comment

ID: 33754505
Thanks for sharing Karl, you are not so hopeless after all! :-)

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now