Added picture is not appearing in spreadsheet

Hi,

I’m trying to add an image to a spreadsheet, I can see that there’s a new picture in the Selection pane in the spreadsheet but it’s not displayed.

My reading suggests it’s likely to be my handling of the MemoryStream but I’m not sure where I’m going wrong.

string docId = paramIn["cmapscLogo30"].ToString();
var cd = new System.Net.Mime.ContentDisposition();
string messageDetails = "";
string docContentType = "";
string fileName = "";
byte[] imageBytes = doc.pullDocument( docId, ref cd, ref docContentType, ref fileName, ref messageDetails, tenant );
MemoryStream ms = new MemoryStream(imageBytes);
System.Drawing.Image image;
image = System.Drawing.Image.FromStream(ms);
dataWorksheet.Pictures.Add(ms, ExcelPictureFormat.Png, 200,200, image.Width, image.Height, LengthUnit.Pixel);
configurationWorksheet.Cells[13, 0].Value = image.Width;
configurationWorksheet.Cells[14, 0].Value = image.Height;

I’ve written out the width and height of the image so I know the image has been retrieved successfully.

Thanks in advance.

Hi Peter,

I was unable to reproduce your issue.
Can you send us a small VS project that reproduces your issue so that I can investigate it?

Currently, I can only guess what the problem is.

Anyway, can you perhaps try removing these two lines:

System.Drawing.Image image;
image = System.Drawing.Image.FromStream(ms);

Also, can you tell me what file format you’re saving to?
Not sure if this is related to your case, but nevertheless, note that the older versions of GemBox.Spreadsheet didn’t support images in XLS format.
However, newer versions of GemBox.Spreadsheet do support images in all formats (including XLS).

Regards,
Mario

Hi Mario, I’ll see if I can reproduce in a small project, the file format is xlsm.

Something odd happened, I added the picture twice more, with different widths to make sure they were appearing in the Selection window in Excel (even if I couldn’t see them), bizarrely the 2nd and 3rd images appeared.

e.g.

	  dataWorksheet.Pictures.Add(ms, ExcelPictureFormat.Png, 200,200, 175, 50, LengthUnit.Pixel);
	  dataWorksheet.Pictures.Add(ms, ExcelPictureFormat.Png, 200,200, 275, 50, LengthUnit.Pixel);
	  dataWorksheet.Pictures.Add(ms, ExcelPictureFormat.Png, 200,200, 375, 50, LengthUnit.Pixel);

Then I tried duplicating the line and used the dimensions of the image and it worked, e.g.

	  dataWorksheet.Pictures.Add(ms, ExcelPictureFormat.Png, 200,200, image.Width, image.Height, LengthUnit.Pixel);
	  dataWorksheet.Pictures.Add(ms, ExcelPictureFormat.Png, 200,200, image.Width, image.Height, LengthUnit.Pixel);

So, I can work around the issue but it’s rather odd

Hopefully I can isolate the functionality in small project and send.

thanks
Peter

Hi Peter,

Yes, that is indeed very odd behavior.
I really hope you’ll manage to create some repro project for us so that we can resolve this problem for you.

Regards,
Mario

Hello again, so I tracked it down to an issue with the spreadsheet I was writing to. It turns out there were 2 buttons with the same name, when I deleted one of those it works as expected. I’ll email you an example but looks like this is a strange edge case,

Thanks for your help and being so responsive

Peter