SSRS: Right and Centre Align Images in a Table

Nathan Eccles, 15 April 2014

SSRS is a great tool for creating reports, but it has some limitations. One such limitation is right or centre aligning images. By default images are placed at the top left of a cell, and will expand from there based on the Display properties you set. In order to position a static image we can either resize the cell the image is in, or modify the padding settings.

But what happens when you want to right or centre align a dynamic image?

Following is a method which will help you to manage your image positioning.

Please note that unfortunately this does not appear to work for CRM online.

The idea we are implementing here is that if we know how wide the image is, we can add padding to the left of the image to space it out across the cell. So if our cell is 5cm wide, and our image is 2cm wide, we want to add 3cm of padding to the left of the image to right align it.

In my scenario I am also using the “Fit Proportional” display property, which means my image will have been scaled to fit the space. Therefore in order to work out its current width, I need to work out its proportions, determine how much the image has been scaled down by, and then get the width.

Please note that in order for this code to working you must add a reference to System.Drawing within the report.

To do this, open Report Properties, then click References, then Add. Click the ellipses and under the .NET tab, find System.Drawing and click OK twice.

Below is the code I am using to achieve the desired result. I’ll break it all down for you so you can see what each part is doing.

=CSTR(255 - (round((System.Drawing.Image.FromStream(new System.IO.MemoryStream(CType(System.Convert.FromBase64String(Fields!Notes_documentbody.Value), Byte()))).Width*0.75) / ((System.Drawing.Image.FromStream(new System.IO.MemoryStream(CType(System.Convert.FromBase64String(Fields!Notes_documentbody.Value), Byte()))).Height*0.75)/62),0))) + "pt"

Fields!Notes_documentbody.Value – This indicates the image you are referring to. In my case that is a file stored in a Note record within Dynamics CRM 2013.

– This is used to parse the image from a string into a useable format. This may not be necessary depending on your data source, however due to how Dynamics CRM 2013 stores images it is necessary for my example.

new System.IO.MemoryStream(CType(<>,  Byte()))
– This is used to put the Image string into a Memory Stream which can be used to create a .NET Image.

– This converts the Memory Stream into a .NET Image type which we can get properties from.

<>.Width / <>.Height
– This gets the Width or Height of the created .NET Image. Note that this is the Width or Height of the full size image in pixels.

– The padding of an image in SSRS is done using “pt”s. 1 pixel is 0.75 pts, so to get the width and height in pts we must multiply by 0.75.

– My cell height is 2.2cm, but I need this in pts in order to calculate the scale. 62 is the height of the cell I am adding the image to in pts (rounded to the nearest whole number). You can use the online converter for CMs to Pts or Inches to Pts depending on what you need.

256 - round(<>)
– My cell width is 9cm. 255 is this width converted to pts (rounded to the nearest whole number). Rounding is used as the final answer needs to be a whole number.

CSTR(<>) + "pt"
– Our final answer needs to be in string format followed by “pt” in order to be recognised as a valid padding value.

With a slight tweak to this code we can centre the image.

=CSTR(round((256 - (round((System.Drawing.Image.FromStream(new System.IO.MemoryStream(CType(System.Convert.FromBase64String(Fields!Notes_documentbody.Value), Byte()))).Width*0.75) / ((System.Drawing.Image.FromStream(new System.IO.MemoryStream(CType(System.Convert.FromBase64String(Fields!Notes_documentbody.Value), Byte()))).Height*0.75)/62),0))) / 2, 0)) + "pt"

The only difference here is that we divide the final result by 2 so that it only pads the image to the centre instead of the right.

Below we can see the result of using no padding, padding to the centre, and padding to the right for dynamics images.


This concept could be used in a range of different situations to help manage dynamic images within SSRS reports and provide a more aesthetically pleasing interface for the users of your report.