Mark Cortejo
Mark Cortejo

Reputation: 109

How to load image from database that uses text format to store file path to crystal reports?

I stored the image path file as text into the database instead of using OLE Object. Now I want to know how to retrieve the image path from the database and load the image into the Crystal Report.

The image path is stored something like this C:\Users\HPLaptop\Desktop\Folder\ImageFile.jpg and I want the crystal report to load the image using this path file.

Upvotes: 0

Views: 3075

Answers (1)

Kvadiyatar
Kvadiyatar

Reputation: 964

Report Schema:

We all know that we need to provide a schema for the crystal repot. Here is an XML view of the report schema that we are going to supply for the report.

BLOB field:

In order to add an image to the crystal report (considering the image field is coming from the database) we need to have a BLOB field in the schema. When you want to store images, documents or different custom types in the database you use a BLOB field. BLOB stands for Binary Large Object.

If you inspect this schema we can see that we have a table called "Images" and two columns "path" and "image". "image" column is type Base64 Binary. This is our BLOB field.What we are going to do in our program is when the user selects the image to upload we store that image in a BLOB field as a stream of bytes and then supply that dataset to the report.

CreateTable() method will illustrate how to generate this schema in the program.

Generating the schema for the report:

There are other ways you can create the schema but this will give you a clearer idea about the column fields.

Creating the table:

private void CreateTable()

{

      //create a new data set.

      this.DsImages = new DataSet();

      //create a new table with two columns and add the table to the
  dataset

DataTable ImageTable = new DataTable("Images");

      //in here the "path" column is not really needed. Image column is
  just enough.

ImageTable.Columns.Add(new DataColumn("path",typeof(string)));

      //Important note

      //Note the type of the image column. You want to give this column
  as a blob field to the crystal report.

      //therefore define the column type as System.Byte[]

      ImageTable.Columns.Add(new DataColumn("image",typeof(System.Byte[])));

      this.DsImages.Tables.Add(ImageTable);

}

If you notice the "image" column you can see that the type of that column is System.Byte[]. This is pretty straight forward. Just create a table with two columns. Then add it to the dataset. Now you can create the schema using this line:

this.DsImages.WriteXmlSchema(@"c:\temp\ImagesSchema.xsd");

Once we have the schema ready we can provide it to the crystal report.

Image 1:

enter image description here

Inspect the Image 1, in the field explorer we can see Images table and the two columns path and image. When you drag the image column onto the report you can see the type of that field is IBlobFieldObject. These fields will read a stream of bytes and convert it back to an image. So our task is pretty much done. The code below shows you how it can save the image as a stream of bytes in the BLOB field.

private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)

{

      try

      {

               //get the image file into a stream reader.

               FileStream FilStr = new FileStream(this.openFileDialog1.FileName, FileMode.Open);

                BinaryReader BinRed = new BinaryReader(FilStr);

               //Adding the values to the columns

               // adding the image path to the path column

               DataRow dr = this.DsImages.Tables["images"].NewRow();

               dr["path"] = this.openFileDialog1.FileName;

               //Important:

               // Here you use ReadBytes method to add a byte array of the image stream.

               //so the image column will hold a byte array.

               dr["image"] = BinRed.ReadBytes((int)BinRed.BaseStream.Length);

               this.DsImages.Tables["images"].Rows.Add(dr);

               FilStr.Close();

               BinRed.Close();

               //create the report object

               DynamicImageExample DyImg = new DynamicImageExample();

               // feed the dataset to the report.

               DyImg.SetDataSource(this.DsImages);

               this.crystalReportViewer1.ReportSource = DyImg;

      }

      catch(Exception er)

      {

               MessageBox.Show(er.Message,"Error");

      }

}

You write this in the FileOk method of the openFileDialog. You use the BinaryReader.ReadBytes method to read the byte array.

dr["image"] = BinRed.ReadBytes((int)BinRed.BaseStream.Length);

Upvotes: 2

Related Questions