In this example, I will demonstrate how to convert an image in an Windows Form Application to a Base64 String that can be used to populate a report parameter that will be received into a SQL Server Reporting Services report as a report parameter.
For example, in my Windows Forms Application, I take an image that is already stored in an image variable called mImage, and convert it to a base64string with the following code:
Private mSignatureString As String = String.Empty mSignatureString = ImageToBase64(mImage , System.Drawing.Imaging.ImageFormat.Jpeg
Public Function ImageToBase64(ByVal mImage As Image, ByVal format As System.Drawing.Imaging.ImageFormat) As String
Using ms As New MemoryStream()
' Convert Image to byte[]
mImage.Save(ms, format)
Dim imageBytes As Byte() = ms.ToArray()
' Convert byte[] to Base64 String
Dim base64String As String = Convert.ToBase64String(imageBytes)
Return base64String
End Using
End Function
I then pass the variable mSignatureString as a parameter called ‘Signature‘ to the process that calls my SSRS report.
My code to that looks like this:
Dim myparams As New List(Of ReportParameter) Dim myparam As New ReportParameter("some_guid", mSomeObject.some_guid.ToString) myparams.Add(myparam) Dim myparam1 As New ReportParameter("Signature", mSignatureString) myparams.Add(myparam1) Dim mNewReport As New frmReportViewer(e.ClickedItem.Text, e.ClickedItem.Tag, True, False, myparams)
NOTE: I don’t cover the reportviewer control that is placed on a windows form and the code behind in this post.
NOTE: If no signature is required on the report, just pass something like ‘NOSIG‘ into the parameter instead so the report can make the image control invisible so not to produce the ‘red X’ box. You will see how I use the text value of ‘NOSIG‘ when building the report below.
Define a parmater in SSRS and call it Signature
Add an Image control to the report.
Open up the Image Properties. Set the ‘image source’ = “Database” and MIME type = “image/bmp”.
In Image Properties, click the ‘Fx’ button under “Use this field:” and type the following expression:
If the parameter value passed from visual basic = “NOSIG“, then nothing will appear in the signature line else the image will appear.