VSTO 2005 Excel picturebox control question

D

Doug

I’m using VSTO 2005 to add some code to an Excel worksheet that allows the
user to click a button and select an image file to display in a picturebox
control. It seems successful, but when I save/close/reopen the worksheet,
the image displays briefly, then goes blank. Also, after saving the file
with an image it does increase the file size, so it appears that it is
embedded in the file.

Can someone explain to me why the image disappears and/or what I need to do
to
make the image persist???

Thanks.

I added a picturebox control (pictureBox1) and a button (cmdInsertPicture)
to a worksheet with the following code behind:

private void cmdInsertPicture_Click(object sender, EventArgs e)
{
string strImgFileFormat = "Image Files (*.bmp; *.jpg)|*.bmp;
*.jpg|Windows Bitmap (*.bmp)|*.bmp|JPEG File Interchange Format
(*.jpg)|*.jpg|All files (*.*)|*.*";
this.openFileDialog1.Filter = strImgFileFormat;
this.openFileDialog1.FileName = "";
this.openFileDialog1.InitialDirectory = Globals.ThisWorkbook.Path;
this.openFileDialog1.ShowDialog();
try
{
this.pictureBox1.Locked = false;
this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
// this.pictureBox1.Load(openFileDialog1.FileName); //both
methods work…
this.pictureBox1.Height = 275;
this.pictureBox1.Width = 275;
this.pictureBox1.Refresh();
}
catch (Exception ex)
{
MessageBox.Show("An image must be selected");
}
}

Sorry if some of you notice this is a duplicate post, I had an MSDN managed
newsgroups profile configuration problem which I believe I've resolved.
Hopefully now I can get expert answers to my technical questions within two
business days. :)
 
P

Peter Huang [MSFT]

Hi,

I am researching the issue, and I will update you with new information ASAP.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Hi,

Based on my research, this is because the different mechanism about .NET
and Excel itself.
Since you are using the PictureBox control of Winform, for Excel side, this
is just a Common ActiveX Control, it did not know how to save the content
of ActiveX, and for the .NET side, it is a picturebox, but all the .NETcode
related with picturebox will not load a picture when we open the workbook
second time.

For your scenario, I think a simple method was to use Excel approach to
handle picture.
e.g.
string strImgFileFormat = "Image Files (*.bmp; *.jpg)|*.bmp;
*.jpg|Windows Bitmap (*.bmp)|*.bmp|JPEG File Interchange Format
(*.jpg)|*.jpg|All files (*.*)|*.*";
this.openFileDialog1.Filter = strImgFileFormat;
this.openFileDialog1.FileName = "";
this.openFileDialog1.InitialDirectory =
Globals.ThisWorkbook.Path;
this.openFileDialog1.ShowDialog();
this.Shapes.AddPicture(openFileDialog1.FileName,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoTrue, 0, 0, 275, 275);

It will be saved when Excel Workbook is saved.

If you have any concern, please feel free to post here.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

Doug

Peter,

Thank you for your research and your response.

When I add the code to a project as in my original post, I am able to add a
picture to a document, save and close that document. The size of that
document in Windows Explorer has increased I believe because I added the
image. When I reopen it I BRIEFLY see the image I had added fully rendered
within the control, and then just prior the application releasing control to
the user, the image disappears. This leads me to believe that the document
does in fact know something about the image. I am not executing any code on
load of the document to affect the picturebox. This behavior goes against
your explanation. Can you explain why I can briefly see the image on reopen?

I will try your suggestion of adding the picture to the shapes collection
which appears to be a valid approach.

Thanks.
 
D

Doug

The this.Shapes.AddPicture method worked fine and persisted the image in the
document upon reopening. Thank you.
 
P

Peter Huang [MSFT]

Hi

I understand that the scenario that the picture is flash at start.
But as I said before, here are two kinds of persistence, one by excel, the
other by .NET.
To the Excel, the picturebox in .net is just an ActiveX Control.
I believe this is not the correct approach to persist the data in the
ActiveX Control, because the Excel did not understand .NET.

If you do want to know the root cause about how the Excel working, you may
want to work with Microsoft Customer Service and Support (CSS) for a faster
resolution. Once you open a Support incident with Microsoft CSS, a
dedicated Support Professional can work with you in a more efficient manner.

Thanks for your understanding!

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Hi,

I am wrting to check if you have still any concern with this issue.
If so, please feel free to post here and I would be happy to keep work with
you on it.

Thanks!

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top