KarlTheHopeless
asked on
Why can't I reload my PictureBox?
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.Dispo se()
If My.Computer.FileSystem.Fil eExists("T heImage.pn g") Then
My.Computer.FileSystem.Del eteFile("T heImage.pn g")
End If
Application.DoEvents()
xl.Visible = False
XLWorkBooks = xl.Workbooks()
XLWorkBook = XLWorkBooks.Open("C:\oldf\ MYEXCEL\pr ogresschar t.xlsm")
Dim XLChart = XLWorkBook.ActiveChart
XLChart.Export("TheImage.p ng", "png")
Application.DoEvents()
PictureBox1.Image = New System.Drawing.Bitmap("The Image.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
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.Dispo
If My.Computer.FileSystem.Fil
My.Computer.FileSystem.Del
End If
Application.DoEvents()
xl.Visible = False
XLWorkBooks = xl.Workbooks()
XLWorkBook = XLWorkBooks.Open("C:\oldf\
Dim XLChart = XLWorkBook.ActiveChart
XLChart.Export("TheImage.p
Application.DoEvents()
PictureBox1.Image = New System.Drawing.Bitmap("The
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@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.
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK Guys
I have solved the problem - as well as the breakpoint problem mentioned here ...
https://www.experts-exchange.com/questions/26496341/Form-disappears-when-stopping-at-breakpoint.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
I have solved the problem - as well as the breakpoint problem mentioned here ...
https://www.experts-exchange.com/questions/26496341/Form-disappears-when-stopping-at-breakpoint.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
ASKER
Thank you.
Thanks for sharing Karl, you are not so hopeless after all! :-)
When you say
PictureBox1.Image = New System.Drawing.Bitmap("The
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:\TheImag
Application.DoEvents()
PictureBox1.Image = New System.Drawing.Bitmap("c:\
Let me know with the results.