Crystal report Image from Path
I finally figured it out. Here’s a solution to show Images in crystal report from Path.
In your Windows form application project, add a new DataSet with name ImageData.
In ImageData.xsd dataset add following rows.
ImageData.xsd Rows :
- Image with Datatype System.Byte[]
- Name with Datatype System.String
Making the Crystal Report form :
In your project, make a new Blank crystal report form as following :
To add the Dataset in your crystal form follow the following procedure :
- Right Click on your crystal report> Goto Database > Database Expert.
- Select Project Data >ADO.NET Datasets > Select the dataset you made above.
- Done.
Now open Field Explorer by right clicking on your crystal report form and Drag n Drop both the name and Image field onto your crystal report form.
Take a new Windows form and design it like below :
Code for Designing the page :
Warning : Your namespace will be different depending on your project name.
Form1.Designer.cs:
namespace ImagefromPath { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer(); this.btnforward = new System.Windows.Forms.Button(); this.btndefault = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(128)))), ((int)(((byte)(255))))); this.button1.FlatAppearance.BorderSize = 0; this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.button1.Font = new System.Drawing.Font("Lithos Pro Regular", 9.749999F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.button1.ForeColor = System.Drawing.Color.White; this.button1.Location = new System.Drawing.Point(209, 64); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(125, 32); this.button1.TabIndex = 0; this.button1.Text = "Choose Image"; this.button1.UseVisualStyleBackColor = false; this.button1.Click += new System.EventHandler(this.button1_Click); // // openFileDialog1 // this.openFileDialog1.FileName = "openFileDialog1"; // // crystalReportViewer1 // this.crystalReportViewer1.ActiveViewIndex = -1; this.crystalReportViewer1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.crystalReportViewer1.DisplayGroupTree = false; this.crystalReportViewer1.Location = new System.Drawing.Point(14, 102); this.crystalReportViewer1.Name = "crystalReportViewer1"; this.crystalReportViewer1.SelectionFormula = ""; this.crystalReportViewer1.Size = new System.Drawing.Size(1233, 487); this.crystalReportViewer1.TabIndex = 1; this.crystalReportViewer1.ViewTimeSelectionFormula = ""; // // btnforward // this.btnforward.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(128)))), ((int)(((byte)(255))))); this.btnforward.FlatAppearance.BorderSize = 0; this.btnforward.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btnforward.Font = new System.Drawing.Font("Lithos Pro Regular", 9.749999F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.btnforward.ForeColor = System.Drawing.Color.White; this.btnforward.Location = new System.Drawing.Point(340, 64); this.btnforward.Name = "btnforward"; this.btnforward.Size = new System.Drawing.Size(125, 32); this.btnforward.TabIndex = 2; this.btnforward.Text = "Forward"; this.btnforward.UseVisualStyleBackColor = false; this.btnforward.Click += new System.EventHandler(this.btnforward_Click); // // btndefault // this.btndefault.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(128)))), ((int)(((byte)(255))))); this.btndefault.FlatAppearance.BorderSize = 0; this.btndefault.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btndefault.Font = new System.Drawing.Font("Lithos Pro Regular", 9.749999F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.btndefault.ForeColor = System.Drawing.Color.White; this.btndefault.Location = new System.Drawing.Point(47, 64); this.btndefault.Name = "btndefault"; this.btndefault.Size = new System.Drawing.Size(156, 32); this.btndefault.TabIndex = 3; this.btndefault.Text = "Choose Default"; this.btndefault.UseVisualStyleBackColor = false; this.btndefault.Click += new System.EventHandler(this.btndefault_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1273, 601); this.Controls.Add(this.btndefault); this.Controls.Add(this.btnforward); this.Controls.Add(this.crystalReportViewer1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Image from File"; this.WindowState = System.Windows.Forms.FormWindowState.Maximized; this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button button1; private System.Windows.Forms.OpenFileDialog openFileDialog1; private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1; private System.Windows.Forms.Button btnforward; private System.Windows.Forms.Button btndefault; } }
Code file for above form.
Form1.cs :
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using CrystalDecisions.CrystalReports.Engine; namespace ImagefromPath { public partial class Form1 : Form { public static string ImagePathLoc = ""; public static byte[] imgbyte; ReportDocument crReportDocument;public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { using (OpenFileDialog dlg = new OpenFileDialog()) { dlg.Title = "Open Image"; dlg.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png"; if (dlg.ShowDialog() == DialogResult.OK) { // MessageBox.Show(dlg.FileName.ToString()); Image img = Image.FromFile(dlg.FileName); ImagePathLoc = dlg.FileName.ToString(); FileStream fstream = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read); imgbyte = new byte[fstream.Length]; fstream.Read(imgbyte, 0, Convert.ToInt32(fstream.Length)); fstream.Close(); ImageData ds = new ImageData(); DataRow drNew = ds.Tables[0].NewRow(); drNew["Image"] = imgbyte; drNew["Name"] = dlg.FileName.ToString(); ds.Tables[0].Rows.Add(drNew); crystalReportViewer1.ShowExportButton = true; crystalReportViewer1.ShowPrintButton = true; string mApppath = Application.StartupPath; crReportDocument = new CrystalReport1(); crReportDocument.SetDataSource(ds.Tables[0]); crystalReportViewer1.ReportSource = crReportDocument; } } } private void btnforward_Click(object sender, EventArgs e) { Shared shared = new Shared(); ImageData ds = new ImageData(); DataRow drNew = ds.Tables[0].NewRow(); drNew["Image"] = imgbyte; drNew["Name"] = ImagePathLoc; ds.Tables[0].Rows.Add(drNew); Shared.ds1 = ds; CrystalForm ct = new CrystalForm(); ct.Show(); } private void btndefault_Click(object sender, EventArgs e) { Image img = Image.FromFile(@"C:\Temp\default.jpg"); ImagePathLoc = @"C:\Temp\default.jpg"; FileStream fstream = new FileStream(@"C:\Temp\default.jpg", FileMode.Open, FileAccess.Read); imgbyte = new byte[fstream.Length]; fstream.Read(imgbyte, 0, Convert.ToInt32(fstream.Length)); fstream.Close(); ImageData ds = new ImageData(); DataRow drNew = ds.Tables[0].NewRow(); drNew["Image"] = imgbyte; drNew["Name"] = "default.jpg"; ds.Tables[0].Rows.Add(drNew); crystalReportViewer1.ShowExportButton = true; crystalReportViewer1.ShowPrintButton = true; string mApppath = Application.StartupPath; crReportDocument = new CrystalReport1(); crReportDocument.SetDataSource(ds.Tables[0]); crystalReportViewer1.ReportSource = crReportDocument; } } }
What I’m doing over here is :
- Getting the Image from the open file dialog box or from a specified path.
- Converting the Image captured into Byte array form.
- Storing the Image byte array data in the DataSet.
- Referencing the Crystal report to show the Blob data from the Dataset.
- And we’re done.??
One note : Please do comment below.
Very good post. Thanks a lot.