How to change image in Crystal report using C #

How can I change an image (its OLE object) in a Crystal report using C #?

+1


source to share


2 answers


you can add CRAXDRT.dll and CrystalDecisions.Shared.dll to your links then you can use this code:



                CRAXDRT.Report report2 = new CRAXDRT.Report();
                CRAXDRT.Application app2 = new CRAXDRT.Application();
                report2 = app2.OpenReport("YourReportName.rpt", OpenReportMethod.OpenReportByDefault);
                for (int i = 1; i < report2.Sections.Count + 1; i++)
                {
                    for (int j = 1; j < report2.Sections[i].ReportObjects.Count + 1; j++)
                    {
                        try
                        {
                            CRAXDRT.OleObject to2 = (CRAXDRT.OleObject)report2.Sections[i].ReportObjects[j];
                            CRAXDRT.OleObject to3 = report2.Sections[i].AddPictureObject("NewOleName.bmp", to2.Left, to2.Top);
                            to3.Height = to2.Height;
                            to3.Width = to2.Width;
                            report2.Sections[i].DeleteObject(to2);
                        }
                        catch (Exception) { }
                    }
                }

      

+2


source


Suppose you have an image in Bitmap object, save it in MemoryStream in Bitmap format,
Create DataSource, create DataTable in it with 1 DataColumn with byte array type

MemoryStream ms; //contains saved bitmap~!!!
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("img", typeof(Byte[])));
DataRow row = dt.NewRow();
row["img"] = ms.ToArray();

      



there is also a report (or subreport) linked to a DataSource with a schema as above insert an Image object bound to the "img" column to tell

+1


source







All Articles