Tutorial :How can I load an image saved in database to Visual Studio ReportViewer 2008?


Does anyone know how can I get ai image saved in database and show it on ReportViewer 2008?



I am currently doing this, however, it was not easy to achieve.

I created a class that generates RDLC files in a MemoryStream. The RDLC memory stream is sent to the reportViewer control, which in turn displays the report.

During the generation of the RDLC file, you can create an embedded image. In order to do this, I retrieve the image into a Byte array, then I convert the Byte array into Base64 Encoding. The RDLC files require Base64 encoding for images to display properly.

Again, it was not easy to achieve, so you will really need to want to do this in order to pull it off. If you want to proceed with this method, I can give you some more details.

EDIT (More details)

You can get some sample code on how to dynamically create RDLC files at GotReportViewer. Direct link to code for VB.NET and C#.

If you can manage to figure out how that code works, you will be able to generate your RDLC files, and won't need to manually create the files yourself. Again, I need to stress the fact that you will be tinkering a lot to get to where you want to be with this.

Now, in order to generate images, retrieve it from the database. When you get it back from the database (I use BLOBs), you will get a Byte array. The Byte array holds the data for the image, but I cannot be used in its current state. To convert it, you can do something like this:

Dim output As String = ""  output = Convert.ToBase64String(imgByteArray)  

The resulting output string will be compatible with ReportViewer. Now you can dynamically add this string into the ReportViewer's RDLC file (XML file). Using the code examples from GotReportViewer, you could do the following:

  'Inserts embedded images into the report    Overridable Function CreateEmbeddedImages() As Rdl.EmbeddedImagesType      Dim bgCell As New Rdl.EmbeddedImageType      Dim images As New Rdl.EmbeddedImagesType        bgCell.Name = "bgTableHeader"      bgCell.Items = New Object() {"image/jpeg", output}      bgCell.ItemsElementName = New Rdl.ItemsChoiceType35() {Rdl.ItemsChoiceType35.MIMEType, Rdl.ItemsChoiceType35.ImageData}        images.EmbeddedImage = New Rdl.EmbeddedImageType() {bgCell}      Return images      End Function  

The RDL type is the Report Definition Language Class file. Basically, its a reverse engineered RDLC XSD Schema. It contains classes that will be used to generate a valid XML file for your reports. It is included in the GotReportViewer link up top, but you can generate one yourself using the XSD.EXE tool provided with Visual Studio.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »