C# Windows Form Application Crystal Report Image from URL and Path.

  • by
C# Windows Form Application Crystal Report Image from URL and Path

“First of all, a very happy new year, it’s 2017 first morning and the time is 6:47 AM and cold outside. This will be my first post in 2017, hope it goes well and you guys find it helpful.”

C# Windows Form Application Crystal Report Image from URL and Path.

C# Windows Form Application Crystal Report Image from URL and Path.So coming to the post, this will explain how to get image from URL in your crystal report of your C# windows form application.I’m using Visual Studio 2012 to develop this form application, as previous version are not compatible to perform this action.Download Link for Crystal Report : Download Crystal Report from SAP.

If you already have Visual studio 2012 or 2010 installed on your system, you can use Crystal Report Runtime for your application. Download from here :

Crystal report runtime for 32-bit system.

Crystal report rumtime for 64-bit system.
If you want the solution for ASP.NET website’s crystal report for the same, see :

ASP.NET Crystal Report Image from URL

 

Coming to this post:

  • Create a new windows form application project or in your existing solution, make a new form with name Form1.cs and design it as following :
    A label with text : “Image URL”
  • Textbox with ID : txtImageUrl
  • Button with ID : btnAssign
  • Another button with ID :  btnForm2
  • CrystalReportViewer with ID : crystalReportViewer1

Looks as below :

C# Windows Form Application Crystal Report Image from URL and Path 1

Form1.cs code :

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 CrystalDecisions.CrystalReports.Engine;

namespace CrystalReportImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); GetCrystalReport();
}

public void GetCrystalReport()
{
ReportDocument crReportDocument = new ReportDocument();
crReportDocument = new CrystalReport();
//crReportDocument.SetDataSource(ds);
crReportDocument.SetParameterValue("Image", txtImageUrl.Text.ToString());
crystalReportViewer1.ReportSource = crReportDocument;
}

private void btnAssign_Click(object sender, EventArgs e)
{
GetCrystalReport();
}

private void btnForm2_Click(object sender, EventArgs e)
{
SharedClass.ImageUrl = txtImageUrl.Text;
Form2 frm = new Form2();
frm.Show();
}
}
}

Explanation of above code :

GetCrystalReport() method will get the crystal report when the Form1 gets loaded.

public void GetCrystalReport()
{
ReportDocument crReportDocument = new ReportDocument();
crReportDocument = new CrystalReport();
//crReportDocument.SetDataSource(ds);
crReportDocument.SetParameterValue("Image", txtImageUrl.Text.ToString());
crystalReportViewer1.ReportSource = crReportDocument;
}
  • Report document variable will get the Crystalreport (.rpt) file which we will be creating later on.
  • Set the parameter value to the textbox value (It can be a image path on your computer having access to it, normally C: drive doesn’t have access, so make sure or a Image URL from a website).
  • Setting the Report source to our report document variable.

Also calling same method on button click method (btnAssign_click).

On clicking the btnForm2, the form will pass the textbox value to a shared class named SharedVariable.cs and shows the Form2 windows which has only crystal report on it.


Make a class named SharedVariable.cs.

SharedVariable.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CrystalReportImage
{
class SharedClass
{
public static string ImagePath = "";
public static string ImageUrl = "";
}
}

This will be sharing the value of our Image URL or Image Path in our application using string variables.


Another form with name : Form2.cs

Make a new form named Form2.cs and take a crystal Report viewer, looks like below (Crystal Report viewer id  : crystalReportViewer1)

 

C# Windows Form Application Crystal Report Image from URL and Path

Form2.cs code :

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 CrystalDecisions.CrystalReports.Engine;

namespace CrystalReportImage
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
GetCrystalReport();
}

public void GetCrystalReport()
{
ReportDocument crReportDocument = new ReportDocument();
crReportDocument = new CrystalReport();
//crReportDocument.SetDataSource(ds);
crReportDocument.SetParameterValue("Image", SharedClass.ImageUrl);
crystalReportViewer1.ReportSource = crReportDocument;
}

}
}

The code is same as previous form.


CrystalReport.rpt:

(Note : Ignore the ASP.NET title bar and website heading and also the .aspx.cs page titles, as crystal report for both my windows form application and website was same, I copied and pasted it in my project and it worked perfectly).

Create a new Crystal Report in your website project with name CrystalReport.rpt. Create an Image object (right click and Insert >> Picture). Choose any image of your choice, it doesn’t matter.
On the field explorer create a new parameter field named Image11.
Edit the Parameter Image11 :
Type : String
List of Value : Static

Drag and drop the Image11 parameter on the report if you want to see its value on your report.

C# Windows Form Application Crystal Report Image from URL and Path 3


C# Windows Form Application Crystal Report Image from URL and Path 6

Now right click on the Image object of your report which you created previously and click Format object.

The format editor will be shown where you can format the image object. Now click on the Picture tab as shown in the image and click on x2 shown against the Graphic location.


C# Windows Form Application Crystal Report Image from URL and Path 9

Formula workshop window will come up. Double click on the report field and on the Image11 parameter on the tree view. Formula will be added in the bottom window (as seen below).  Done.


C# Windows Form Application Crystal Report Image from URL and Path 10

Now run the project. Additionally, you can download the source code from the github link provided below to see what I have done in my project. Drop your comments below, if any. Thanks. 🙂


GITHUB DOWNLOAD LINK


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.