Solved

Why can't I reload my PictureBox?

Posted on 2010-09-20
7
399 Views
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.

Simple.

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
            My.Computer.FileSystem.DeleteFile("TheImage.png")
        End If

       
       
        Application.DoEvents()

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

        Dim XLChart = XLWorkBook.ActiveChart

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

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

==============================

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

Karl
0
Comment
Question by:KarlTheHopeless
  • 3
  • 3
7 Comments
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 50 total points
ID: 33722588
0
 
LVL 7

Expert Comment

by:klakkas
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")
        Application.DoEvents()
        PictureBox1.Image = New System.Drawing.Bitmap("c:\TheImage.png")

Let me know with the results.

0
 

Author Comment

by:KarlTheHopeless
ID: 33740567
@klakkas

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

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

@jtoutou

I can't see anything in your link that does anything really different to what I am trying to do.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 7

Assisted Solution

by:klakkas
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")
        Application.DoEvents()
        System.Threading.Thread.Sleep(5000)
        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")
        XLWorkBook.Close()

        Application.DoEvents()

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

Author Comment

by:KarlTheHopeless
ID: 33754444
OK Guys

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

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_26496341.html

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

Karl

0
 

Author Closing Comment

by:KarlTheHopeless
ID: 33754470
Thank you.
0
 
LVL 7

Expert Comment

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

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

820 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