Free Support Forum - aspose.com

How do I resize a picture added using a stream?

I am attempting to add a picture to a cell using a stream but cannot seem to resize it correctly. When using public int Add(int,int,Stream,int,int); the ratios seem to be ignored andit always inserts a picture of width and height 0.. The following code creates a picture of the corerect height but a width of 'zero'. Inserting the same picture directly from a file correctly creates the picture with correct sizing

MemoryStream imgStrm = new MemoryStream();

imgStrm.Write(imgBytes, 0, imgBytes.Length);

try

{

Image infoImage = Image.FromStream(imgStrm);

int picIdx = excelDoc.Worksheets[0].Pictures.Add(rowNo, colCnt, imgStrm, 100, 100);
excelDoc.Worksheets[0].Pictures[picIdx].Top = 1;
excelDoc.Worksheets[0].Pictures[picIdx].Left = 0;
sheetCells.SetRowHeight(rowNo, infoImage.Height+2);

}catch (Exception)

{.....

TIA

Alan

Which version of Aspose.Cells are you using? I don't find the problem. Following is my sample code:

Workbook excel = new Workbook();

FileStream fs = File.OpenRead("d:\\logo.jpg");

byte[] data = new Byte[fs.Length];
fs.Read(data, 0, data.Length);
fs.Close();


MemoryStream stream = new MemoryStream();
stream.Write(data, 0, data.Length);


Image infoImage = Image.FromStream(stream);
excel.Worksheets[0].Pictures.Add(2, 2, stream, 100, 100);
excel.Worksheets[0].Pictures[0].Top = 1;
excel.Worksheets[0].Pictures[0].Left = 0;

excel.Save("d:\\test\\abc.xls");

What's the format of your image file? Is it a gif, jpg or other format image file?

Sorry for the lack of information. I am using version 3.9.1.2 and the image is a png which is held in a database. I will try your example to see if it works as exepected

Thanks

The problem appears to be with the originating images as they have zero resolution in their properties. IE and Microsoft picture manager seem to cater for this somehow, presumably autocorrecting as necessary. I dont suppose you have plans to do something similar? I can send you a sample file if you like.

Thanks

You can attach your image file here or send it to nanjing@aspose.com .

If it's not very difficult to solve it, I will try to make it in Aspose.Cells. Otherwise, it's better to change your image file to a "normal" image file. Thanks.

I have emailed some images

Thanks

Please try this version.

Perfect! Thanks for your quick response