ASP.NET Crystal Report Image from URL
After wondering around for days to get a solution to this issue, I finally found a way to get the image from URL in crystal report on both .net web page and windows form applications. I’m using Visual Studio 2012 to develop this web page, as previous version are not compatible to perform this action. This post will explain to fetch Image from URL in .net pages.
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.
Make a new page in your webite project named Default.aspx and edit it as following :
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <h1>Page One</h1> <hr /> Enter Image Url : <asp:TextBox runat="server" ID="txtUrl" Width="350px"/> <br /> <asp:Button runat="server" ID="btnPassImage" Text="Pass" OnClick="btnPassImage_Click" /> </form> </body> </html>
And here is the code for the (.aspx.cs) page:
Default.aspx.cs:
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using CrystalDecisions.CrystalReports.Engine; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnPassImage_Click(object sender, EventArgs e) { Session["ImageUrl"] = txtUrl.Text.ToString(); Response.Redirect("PageTwo.aspx"); } }
This page contains a textbox in which the user will specify the URL of the image to be shown on Crystal Report. And a button to redirect the user to the page containing Crystal report (PageTwo.aspx).
Now create a new page named PageTwo.aspx. And edit is as following:
PageTwo.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageTwo.aspx.cs" Inherits="PageTwo" %> <%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <div> <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" Width="1000px" Height="10000px" AutoDataBind="true" /> <br /> Hitesh </div> </div> </form> </body> </html>
PageTwo.aspx.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CrystalDecisions.CrystalReports.Engine; public partial class PageTwo : System.Web.UI.Page { ReportDocument rd; protected void Page_Load(object sender, EventArgs e) { Report(); } public void Report() { try { rd = new ReportDocument(); string rptPath = Server.MapPath("CrystalReport.rpt"); rd.Load(rptPath); String ImageUrl = Session["ImageUrl"].ToString(); rd.SetParameterValue("Image11", ImageUrl); CrystalReportViewer1.ReportSource = rd; } catch (Exception ex) { } } }
This page contains the crystal report viewer on which the Image will be shown.
rd = new ReportDocument(); string rptPath = Server.MapPath("CrystalReport.rpt"); rd.Load(rptPath); String ImageUrl = Session["ImageUrl"].ToString(); rd.SetParameterValue("Image11", ImageUrl); CrystalReportViewer1.ReportSource = rd;
This is important:
- I’m making a Reportdocument object and mapping it to my CrystalReport.rpt Report file in my project files (Create a crystal report with name CrystalReport.rpt).
- Fetching the Image url which was stored in session from Page one.
- Setting the Parameter Value for the Image1 parameter to the stored image url variable.
CrystalReportViewer1 source as Report document object.
CrystalReport.rpt:
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.
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.
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.
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. 🙂
Pingback: C# Windows Form Application Crystal Report Image from URL and Path • ParallelCodes;