Hi,
I was wondering if it would be possible to add an image to a table cell through code?
If so do you have an example?
Kind regards,
Nathan
Hi,
I was wondering if it would be possible to add an image to a table cell through code?
If so do you have an example?
Kind regards,
Nathan
Hi Nathan,
The way to do this in presentations is to add a background image to the table cell.
For example, try something like this:
var cell = table.Rows[0].Cells[0];
using (var imageStream = File.OpenRead("image.png"))
cell.Format.Fill.SetPicture(PictureContentType.Png, imageStream);
Does this solve your issue?
Regards,
Mario
That stretches the image. Is there anyway to maintain the size?
Hi Nathan,
Try this:
using (var imageStream = File.OpenRead("image.png"))
using (var image = System.Drawing.Image.FromStream(imageStream))
{
// The values are in points.
double imageWidth = image.Width * 0.75;
double imageHeight = image.Height * 0.75;
double cellWidth = table.Columns[0].Width;
double cellHeight = table.Rows[0].Height;
var pictureFill = cell.Format.Fill.SetPicture(PictureContentType.Png, imageStream);
pictureFill.StretchRight = 1 - imageWidth / cellWidth;
pictureFill.StretchBottom = 1 - imageHeight / cellHeight;
}
This should place the image in the cell’s top-left corner.
If you want to place it on the right, change the StretchRight to StretchLeft, and if you want to place it on the bottom, change the StretchBottom to StretchTop.
I hope this helps.
Regards,
Mario
Hi Nathan,
Try this:
var pictureFill = cell.Format.Fill.SetPicture(PictureContentType.Png, imageStream);
double stretchHorizontal = 1 - imageWidth / cellWidth;
pictureFill.StretchLeft = stretchHorizontal / 2;
pictureFill.StretchRight = stretchHorizontal / 2;
pictureFill.StretchBottom = 1 - imageHeight / cellHeight;
Does this solve your issue?
Regards,
Mario
Thanks Mario,
That has solved that issue.
I have another issue (sorry). Is there a way to persist the background of the cell as i want the same colour background for the full row? The image background is transparent.
If i dont add the image the back colour is the gray.

Kind regards,
Nathan
Hi Nathan,
I’m afraid that’s not possible with a picture fill.
The only workaround that comes to my mind is to add Picture elements and position them above the desired cells.
For example, see:
However, finding the picture’s position could be tricky (depending on the presentation).
Nevertheless, try using the Table.Frame.Layout.Left and Table.Frame.Layout.Top as a start position and then move the position based on the row heights and column widths.
Regards,
Mario
Thanks Mario. I will try that. I also tried making an image that filled the whole area in code. Will work out which will work more reliably.
Thanks for all your help.