Put an image into a cell

Hello,

is it possible to put an image dynamically into a cell of an excel sheet which I want to convert later to a pdf file?

Like this:

Bitmap image = new Bitmap(“test.png”);
cells[1, 1].PutValue(image);

But the output in the cell is just the type string “System.Drawing.Bitmap”.

I use Aspose.Cells for .NET v7.2.0.1

Regards
Andreas

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

Please download and use the latest version:
Aspose.Cells
for .NET v7.2.2.3



First you will read bytes of your images, then you will create a memory stream object from bytes.

Then you will insert the image using the Pictures.Add() method.

Please see the following code. I have attached the output pdf and screenshot for your reference.

C#

string imgPath = @"D:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg";

byte[] imgBytes = File.ReadAllBytes(imgPath);

MemoryStream ms = new MemoryStream();
ms.Write(imgBytes, 0, imgBytes.Length);


Workbook workbook = new Workbook();

Worksheet worksheet = workbook.Worksheets[0];

worksheet.Pictures.Add(1, 1, ms);

PdfSaveOptions opts = new PdfSaveOptions();
opts.OnePagePerSheet = true;

workbook.Save("output.pdf");


Hopefully, it will be helpful for you. Let us know your feedback

Screenshot:

Thank you very much, it works.

Hi,


Moreover, we also recommend you to kindly see our documentation, especially the topic for your reference:
http://www.aspose.com/docs/display/cellsnet/Adding+Pictures


Thank you.

Hello,


I have Same issue of adding picture into cell dynamically. But in my case image path comes from server so I have dynamic path like
string path ="https://localhost/DCFileService/Library/Firm_1/Logo/Thumb/DealCloudPNG634608601930498149.png"

Now when I try to execute following statement

int pictureIndex = worksheet.Pictures.Add(0, 0, path);

it gives me error that "File does not exists."
What should be the issue? Any help would be appreciated.

Thanks & Regards
Ashish Rajguru

Hi,


You may try: ShapeCollection.AddLinkedPicture() for your requirements, see the document for your reference:
http://www.aspose.com/docs/display/cellsnet/Insert+a+Linked+Picture+from+Web+Address


Thank you.

Hi,


In addition, you can also read image data from its url and add it to a worksheet using memory stream method, as you can see below:
var webClient = new WebClient();
byte[] imgBytes = webClient.DownloadData("http://cdn.aspose.com/homepage/images/logo.png");
MemoryStream ms = new MemoryStream();
ms.Write(imgBytes, 0, imgBytes.Length);
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Pictures.Add(1, 1, ms);
workbook.Save("K://pic.xlsx");

Hello,


Thank you very much. it is working perfectly. I was thinking of downloading that image first but I was avoiding to do so. So I was looking for solution to give direct path at the time of adding picture or any other aspose’s method. Now I am able to render logo with dynamic path of my server.

Thank you once again.

Thanks & Regards
Ashish Rajguru

Hi Ashish,


Good to know that your issue resolved now.

Feel free to contact us any time if you have further queries or issue, we will be happy to assist you.

Thank you.

Hello,


Adding image into cell dynamically working fine but i have problem with fitting that image to top left cell.
it is not being fit perfectly into top left cell. my code for adding image dynamically in top left cell is as follows.


//Add Image to first left top cell using memory stream
bool exist = false;
try
{
HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(logoPath);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
exist = response.StatusCode == HttpStatusCode.OK;
}
}
catch
{
}
if (exist)
{
var webClient = new WebClient();
byte[] imgBytes = webClient.DownloadData(logoPath);
MemoryStream ms = new MemoryStream();
ms.Write(imgBytes, 0, imgBytes.Length);

MakePageSettings(worksheet);

int pictureIndex = worksheet.Pictures.Add(0, 0, ms);

//Accessing the newly added picture
Picture picture = worksheet.Pictures[pictureIndex];

//Setting height-width of top left cell as per picture hight-width
cell.SetColumnWidthInch(0, picture.WidthInch);
cell.SetRowHeightInch(0, picture.HeightInch);

picture.RelativeToOriginalPictureSize = true;
picture.HeightCM = picture.OriginalHeightCM;
picture.WidthCM = picture.OriginalWidthCM;
}

Image is being fit in stretch mode. it is not being fit as per image height width. I have also attached current output file and expected output file. Any help would be appriciated

Hi,


Thank you for using Aspose.Cells.

I could not reproduce the issue at my end, as your code gives compilation errors. You can use the AddPicture method of the shapes collection for scaling the picture according to your needs. Please see the below code for your reference. i have also attached screenshot for your reference.

Also, we recommend you to download and use this latest version of Aspose.Cells: http://www.aspose.com/community/files/51/.net-components/aspose.cells-for-.net/entry396793.aspx

<span style=“font-size:10.0pt;font-family:“Courier New”;
color:blue;mso-no-proof:yes”>string logoPath = http://www.aspose.com/Images/aspose-logo.jpg;

<span style=“font-size:10.0pt;font-family:“Courier New”;
color:blue;mso-no-proof:yes”>

<span style=“font-size:10.0pt;font-family:“Courier New”;
color:blue;mso-no-proof:yes”>var webClient = new WebClient();

<span style=“font-size:10.0pt;font-family:“Courier New”;
color:blue;mso-no-proof:yes”>

<span style=“font-size:10.0pt;font-family:“Courier New”;
color:blue;mso-no-proof:yes”>byte[] imgBytes =
webClient.DownloadData(logoPath);

<span style=“font-size:10.0pt;font-family:“Courier New”;
color:#2B91AF;mso-no-proof:yes”>

<span style=“font-size:10.0pt;font-family:“Courier New”;
color:#2B91AF;mso-no-proof:yes”>MemoryStream ms = new
MemoryStream();

<span style=“font-size:10.0pt;font-family:“Courier New”;
mso-no-proof:yes”>

<span style=“font-size:10.0pt;font-family:“Courier New”;
mso-no-proof:yes”>ms.Write(imgBytes, 0, imgBytes.Length);

<span style=“font-size:10.0pt;font-family:“Courier New”;
color:#2B91AF;mso-no-proof:yes”>

<span style=“font-size:10.0pt;font-family:“Courier New”;
color:#2B91AF;mso-no-proof:yes”>Workbook workbook = new Workbook();

<span style=“font-size:10.0pt;font-family:“Courier New”;
color:#2B91AF;mso-no-proof:yes”>

<span style=“font-size:10.0pt;font-family:“Courier New”;
color:#2B91AF;mso-no-proof:yes”>Worksheet worksheet =
workbook.Worksheets[0];

<span style=“font-size:10.0pt;font-family:“Courier New”;
mso-no-proof:yes”>

<span style=“font-size:10.0pt;font-family:“Courier New”;
mso-no-proof:yes”>Aspose.Cells.Drawing.Picture
picture =

<span style=“font-size:10.0pt;font-family:“Courier New”;
mso-no-proof:yes”>

<span style=“font-size:10.0pt;font-family:“Courier New”;
mso-no-proof:yes”>workbook.Worksheets[0].Shapes.AddPicture(0, 0, ms, 100, 100);


workbook.Save(@“e:\outLinkedPicture.xlsx”);

I have tried adding picture as per your mentioned method like below.



var webClient = new WebClient();
byte[] imgBytes = webClient.DownloadData(logoPath);
MemoryStream ms = new MemoryStream();
ms.Write(imgBytes, 0, imgBytes.Length);

Picture picture = worksheet.Shapes.AddPicture(0, 0, ms, 100, 100);


Please find output file. logo is not properly rendered/added to cell[0,0]. it must be fit to cell[0,0]. cell[0,0]'s height and width must be set according to logo height - width.
In my case everything is being done dynamically. logo path coming dynamically and i want to render/add that logo to cell[0,0].

Thanks & Regards
Ashish Rajguru

Hi Ashish,


Thank you for the feedback.

With your earlier code shared, I tried to reproduce the issue at my end, but could not get success. You can see the attached file here for your reference. It loads the image in Cell [0,0] without changing its size.

My Issue has been resolved. I bind image first into top left cell and then I called worksheet.AutoFitColumns at end. that was the only reason. Now I have made columns AutoFit except the column in which image is being rendered.



Thanks & Regards
Ashish Rajguru

Hi,

Thanks for sharing your workaround.

You can also Autofit all columns first and then reset the width and height of the column and row of the cell where you placed the image.

The code below inserts a picture inside some cell and adjust the cell’s height and width accordingly.

I have attached the input image, output xls file and screenshot for your reference.

C#


public static void AddPicture(Worksheet Sheet, int Row, int Column, int Width, int Height, string PicPath)

{

//Set the height of the first row

Sheet.Cells.SetRowHeightPixel(Row, Height); //height equals to picture height


//Set the width of the first and second column

Sheet.Cells.SetColumnWidthPixel(Column, Width);


//Add a picture inside cell at this row and column

int picId = Sheet.Pictures.Add(Row, Column, PicPath);

  Picture pic = Sheet.Pictures[picId];<br>


//Set the height and width of picture

pic.Height = Height;

pic.Width = Width;

}


public static void Run()

{

//Create a workbook

Workbook workbook = new Workbook();


//Access first sheet

Worksheet worksheet = workbook.Worksheets[0];


//Add a picture inside a cell

  AddPicture(worksheet, 4, 4, 300, 200, @"f:\downloads\image-koala.png");<br>


//Write the output

workbook.Save(@“f:\downloads\output.xls”);

}


Screenshot: