reporting-servicessystem.drawingimage-size

SSRS - Accessing System.Drawing.Image.FromStream


I am currently trying to center an image on screen in SSRS. While this functionality is not directly support it is possible to provide custom padding based on the image size. This is an external image provided by a database, so the image changes repeatedly and I cannot hard code a padding. I am trying to retrieve the width of the image using:

=System.Drawing.Image.FromStream(new System.IO.MemoryStream(CType(System.Convert.FromBase64String(Fields!HomeLogoImage.Value), Byte()))).Width

However when this is entered into an expression place holder, "FromStream" is not recognised. enter image description here

I can confirm that I have added the reference to the system.drawing assembly and I am using version 4.0.0.0.

enter image description here

I am returning to SSRS for the first time in quite a while so any advice on this would be greatfully received. Or even if there is a way to center images without resorting to the System.Drawing method, I would love to hear.

Many thanks.


Solution

  • Try the following. I've tested this against some .png database images I had lying around and it seemed to work.

    =System.Drawing.Image.FromStream
                        (new System.IO.MemoryStream(CType(Fields!Image.Value,Byte())))
                        .Width
    

    EDIT after OP update

    To do the same with an external image you can use the following

    =System.Drawing.Image.FromStream
                        (new System.IO.FileStream(Fields!HomeLogoImage.Value, IO.FileMode.Open))
                        .Width
    

    This assumes that the HomeLogoImage field contains a valid path and filename to the image.

    However You will probably encounter a permissions error. I have not done this myself so I can only point you to a link that discusses the issue with possible solutions. I've not done anything other than a quick search to find this so better solutions may be available

    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/31490332-c2f7-48a4-9928-29916ce002b4/request-for-the-permission-of-type-systemsecuritypermissionssecuritypermission-mscorlib?forum=sqlreportingservices