We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose.Cells Insert EMF image using MemoryStream fails

Using Aspose.Cells 4.9 I am unable to add an EMF image to my excel worksheet using a MemoryStream. I get the Unsupported file format exception.

However, if I save the EMF to a file and add it to excel worksheet, it works fine.

MemoryStream ms = CreateImage(); //the memory stream return is open

worksheet.Pictures.Add(row,col,ms); //throws an exception

//but if I save the memory stream to a file and add the image by using the Add method that takes the file name it works fine (below).

worksheet.Pictures.Add(row, col, @"c:\temp\uv.emf");

Obviously, I don't want to write to disk just to read it back in when I shold be able to use the MemoryStream directly.

Can you please help me understand what I can or can't do with EMF images in Aspose.Cells.

Thanks

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Well, I tested your scenario and it works fine with the attached latest version of Aspose.Cells. Please try the attached latest version of Aspose.Cells and if you still face any issue, please share your sample code and image file. We will check it soon.

Thank You & Best Regards,

Thanks. The latest version works!

Now I have a problem with not knowing how to embed the image into a cell. Currently I am only able to size the cell the size of the image, add the image to the worksheet in the position that overlaps the cell rectangle exactly but it is not embedded inside the cell. Is this possible?

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Please create your desired workbook in MS Excel manually to demonstrate your requirement and post it here. We will check it and get back to you soon.

Thank You & Best Regards,

First request: The image over C2 I would like bound to the cell C2 so that when the cell is resized (col or row) it will be resized with the cell.

Second request: The image was added using EMF format. This should produce a nice looking image when resized but when it is enlarged it looks terrible. The lines don't look goood, etc... Is that a formatting issue when image is added to Excel?

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

jsimpkins@virscidian.com:
First request: The image over C2 I would like bound to the cell C2 so that when the cell is resized (col or row) it will be resized with the cell.

Please use Picture.Placement = PlacementType.MoveAndSize; to get your desired result.

jsimpkins@virscidian.com:
Second request: The image was added using EMF format. This should produce a nice looking image when resized but when it is enlarged it looks terrible. The lines don't look good, etc... Is that a formatting issue when image is added to Excel?

Please try adding the picture manually in MS Excel and see if it behaves the same way as by adding picture using Aspose.Cells APIs. If you find any difference in the behaviors, please share your image file and files generated by MS Excel and Aspose.Cells. We will check them and get back to you soon.

Thank You & Best Regards,

Is there a way to make the image size the same size as the cell so when it is placed in the cell it fits perfectly to the size fo the cell? Or another way to ask, is there way to make the cell size the exact size of the image?

Hi,

I think you may make use of UpperLeftRow, UpperLeftColumn, LowerRightRow and LowerRightColumn attributes/parameters for your requirement. See the sample code below:

Workbook workbook = new Workbook(“e:\test\Book1.xlsx”);
Worksheet worksheet = workbook.Worksheets[0];

FileStream fs = File.Open(@“e:\test\school.jpg”, FileMode.Open);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
MemoryStream ms = new MemoryStream(buffer);

//Place the picture to A1 cell.
//The Picture width and height would be same A1 cell’s height/width
int index = worksheet.Pictures.Add(0, 0, 1, 1,ms);

Aspose.Cells.Drawing.Picture pic = worksheet.Pictures[index];
pic.Placement = PlacementType.MoveAndSize;

workbook.Save(“e:\test\outBook1.xlsx”);


Thank you.

That works! I spent hours trying to figure out how to get the image and the cell size to be the same. THanks