Adding Picture to Cell and setting transparency Color in Excel worksheet in .NET

Hi, I´am adding a Picture to a Cell but I also want to set the transparency Color of the Picture.


I was able to do this with Excel Automation in the past and now Iam migrating to Aspose but I dont now how to accomplish this.


Workbook workbook = new Workbook(“PathToWorkbook”);
var _worksheet = workbook.Worksheets[0];
int picIdx = _worksheet.Pictures.Add(4, 57, “PathtoImage”);
Picture newPic = _worksheet.Pictures[picIdx];

workbook.Save(“PathToNewWorkbook”);

Any Idea how to do that?

Hi Gonzalo,

Thanks for your posting and considering Aspose.Cells.

Please provide us some Excel file containing your picture with transparency set. Also, let us know what steps you took to set the transparency of the picture in Microsoft Excel.

We will look into your issue and help you asap.

Hi, i attached 2 files. The Excel where I want to insert the Picture and a .gif with white background. Previusly using Excel Automation i did something like this:




Microsoft.Office.Interop.Excel.Shape shape = _worksheet.Shapes.AddPicture(
pictureFilePath,
MsoTriState.msoFalse,
MsoTriState.msoTrue,
(float)left,
(float)top,
width,
height);

SetTransparentColor(
shape,
transparentColor);

And SetTranparentColor() do this:

private void SetTransparentColor(
Microsoft.Office.Interop.Excel.Shape shape,
Color transparentColor)
{
if (!transparentColor.IsEmpty)
{
shape.PictureFormat.TransparentBackground = MsoTriState.msoTrue;
shape.PictureFormat.TransparencyColor = transparentColor.ToArgb();
shape.Fill.Visible = MsoTriState.msoFalse;
}
}


transparentColor is of type System.Drawing.Color


Thanks a lot for your help.

Hi Gonzalo,

Thanks for your posting and considering Aspose.Cells.

Aspose.Cells supports this feature. Please see the following sample code. It adds your picture and make the white color transparent.

I have attached the output Excel file generated by the code and screenshot showing the output picture as it looks in MS-Excel for your reference. As you can see the picture is transparent, you can see grid lines behind the picture because of its transparency.

C#


string filePath = @“F:\Shak-Data-RW\Downloads\book1.xlsx”;


//Create workbook object

Workbook workbook = new Workbook();


//Access the first worksheet

Worksheet worksheet = workbook.Worksheets[0];


//Add your picture inside the worksheet

int pid = worksheet.Pictures.Add(5, 5, “TAMSA_11919191.gif”);

Picture pic = worksheet.Pictures[pid];


//Create cells color

CellsColor clr = workbook.CreateCellsColor();

clr.Color = Color.FromArgb(255, 255, 255);

clr.IsShapeColor = true;


//Make this color transparent for this picture

pic.FormatPicture.TransparentColor = clr;


//Save the workbook

workbook.Save(“output.xlsx”);



Thanks a lot!

Can you please send me the Excel file you used to make the example?


Thanks!

Nevermind. You attached to your last reply. Sorry about that. :slight_smile:

Hi Gonzalo,

Thanks for your posting and considering Aspose.Cells.

It is good to know that you have received the output Excel file. Hopefully, it will fulfill your needs. Let us know if you have any other questions, we will be glad to look into it and help you further.

Sorry for coming back with this but I`am having problems to make this works within the Excel I sended you before (Adding Picture to Cell and setting transparency Color in Excel worksheet in .NET)


Any idea?

Hi Gonzalo,

Thanks for your posting and using Aspose.Cells.

Actually, there is a bug in Aspose.Cells. It works for XLSX format but does not work for XLS format.

So, please save your file in XLSX format to get rid of this issue. Please see the following code. It generates files in XLSX and XLS format. I have attached both of them for your reference. As you can see, XLSX output is fine but XLS output is not working.

We have logged this issue in our database for investigation. We will look into it and fix this issue. Once, the issue is resolved or we have some other update for you, we will let you know asap.

This issue has been logged as

  • CELLSNET-43131 - Setting transparent color of picture works for XLSX format but not for XLS format

C#

//Create workbook object

Workbook workbook = new Workbook();


//Access the first worksheet

Worksheet worksheet = workbook.Worksheets[0];


//Add your picture inside the worksheet

int pid = worksheet.Pictures.Add(5, 5, “TAMSA_11919191.gif”);

Picture pic = worksheet.Pictures[pid];


//Create cells color

CellsColor clr = workbook.CreateCellsColor();

clr.Color = Color.FromArgb(255, 255, 255);

clr.IsShapeColor = true;


//Make this color transparent for this picture

pic.FormatPicture.TransparentColor = clr;


//Save the workbook

workbook.Save(“output.xls”);

workbook.Save(“output.xlsx”);

Hi Gonzalo,

Thanks for your using Aspose.Cells.

Please download and try the latest fix: Aspose.Cells for .NET v8.2.2.2 and let us know your feedback.

It´s working fine now with XLS!


Question: Does Aspose supports Excel files in XML format for this kind of operation?

We also work sometimes with this type of Excel files and it seams isn´t working.

I attached one for reference.

Thanks a lot for all you help

Hi Gonzalo,

Thanks for your feedback and using Aspose.Cells.

It is good to know that your original issue is resolved now.

For your current issue, please let us know what problem you are facing. Please note, .xml only saves xml data, it does not save images inside it. Please open your spreadsheetML.xml file in Excel and then insert picture inside it, then click save, you will get a warning that this feature is not supported in this format, do you still want to save it, press yes, then open it again in Excel and you will see picture is not present inside.

So, this feature is not even supported in Excel which means, it can also not be done with Aspose.Cells or with any other software because this is the problem of format, this format does not support saving pictures.

If you are facing some other issue, please explain it with more detail, we will look into it and help you asap.

Hi,


Thanks for your explanation. I got it.

The solution is open an Excel XML, insert the picture and then save into another Excel format (a binary one) using SaveFormat. It works!

Thanks a lot for all your help!

Hi Gonzalo,

Thanks for your feedback and using Aspose.Cells.

It is good to know you were able to sort out this issue. Let us know if you encounter any other issue, we will be glad to look into it and help you further.

The issues you have found earlier (filed as CELLSNET-43131) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.