在水晶报表中从本地路径动态添加图片时出现错误[英] Error While Add Image Dynamically from Local Path In Crystal Report

本文是小编为大家收集整理的关于在水晶报表中从本地路径动态添加图片时出现错误的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

在我的水晶报告中,我需要从特定文件夹路径添加标头图像,因为我在代码下完成了

private void AddImage_ProdfailReport()
    {
        try
        {
            // here i have define a simple datatable inwhich image will recide 
            DataTable dt = new DataTable();
            // object of data row 
            DataRow drow;
            // add the column in table to store the image of Byte array type 
            dt.Columns.Add("Image", System.Type.GetType("System.Byte[]"));
            drow = dt.NewRow();
            // define the filestream object to read the image 
            FileStream fs;
            // define te binary reader to read the bytes of image 
            BinaryReader br;
            // check the existance of image 
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Footer.Jpg"))
            {
                // open image in file stream 
                fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "Footer.Jpg", FileMode.Open);
            }
            else
            {
                // if phot does not exist show the nophoto.jpg file 
                fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "Footer.jpg", FileMode.Open);
            }
            // initialise the binary reader from file streamobject 
            br = new BinaryReader(fs);
            // define the byte array of filelength 
            byte[] imgbyte = new byte[fs.Length + 1];
            // read the bytes from the binary reader 
            imgbyte = br.ReadBytes(Convert.ToInt32((fs.Length)));
            drow[0] = imgbyte;
            // add the image in bytearray 
            dt.Rows.Add(drow);
            // add row into the datatable 
            br.Close();
            // close the binary reader 
            fs.Close();
            // close the file stream 
            CreRe_ProdFail rptobj = new CreRe_ProdFail();
            // object of crystal report 
            rptobj.SetDataSource(dt);
            // set the datasource of crystalreport object 
            crv1.ReportSource = rptobj;
            //set the report source 
        }
        catch (Exception ex)
        {
            // error handling 
           MessageBox.Show("Missing Footer.jpg in application folder");
        }
        // run the application to view image in report 
    }

我在数据集的数据表中创建了一个字段, 将数据类型更改为system.byte()

然后我将此字段拖到报告 但是它显示了一个错误"此字段名称尚不清楚",在打开报告时. 我没有得到什么问题 请帮助我.

推荐答案

我做到了; 我正在尝试添加来自数据库的数据和来自本地路径的图像,并在Crystal Report中显示;我通过以下代码完成了此操作:

首先:我在数据集的数据表中创建了一个新列(" image"),然后将数据类型更改为system.byte()

第二:拖放此图像我想要的位置.

private void LoadReport()
    {
        frmCheckWeigher rpt = new frmCheckWeigher();
        CryRe_DailyBatch report = new CryRe_DailyBatch();
        DataSet1TableAdapters.DataTable_DailyBatch1TableAdapter ta = new CheckWeigherReportViewer.DataSet1TableAdapters.DataTable_DailyBatch1TableAdapter();
        DataSet1.DataTable_DailyBatch1DataTable table = ta.GetData(clsLogs.strStartDate_rpt, clsLogs.strBatchno_Rpt, clsLogs.cmdeviceid); // Data from Database
        DataTable dt = GetImageRow(table, "Footer.Jpg");

        report.SetDataSource(dt);
        crv1.ReportSource = report;
        crv1.Refresh();

    }

//通过此功能,我将图像数据合并到DataTable

 private DataTable GetImageRow(DataTable dt, string ImageName)
    {

        try
        {

            FileStream fs;
            BinaryReader br;

            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + ImageName))
            {
                fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + ImageName, FileMode.Open);
            }
            else
            {
                // if photo does not exist show the nophoto.jpg file 
                fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + ImageName, FileMode.Open);
            }
            // initialise the binary reader from file streamobject 
            br = new BinaryReader(fs);
            // define the byte array of filelength 
            byte[] imgbyte = new byte[fs.Length + 1];
            // read the bytes from the binary reader 
            imgbyte = br.ReadBytes(Convert.ToInt32((fs.Length)));

            dt.Rows[0]["Image"] = imgbyte;


            br.Close();
            // close the binary reader 
            fs.Close();
            // close the file stream 




        }
        catch (Exception ex)
        {
            // error handling 
            MessageBox.Show("Missing " + ImageName + "or nophoto.jpg in application folder");
        }
        return dt;
        // Return Datatable After Image Row Insertion

    }

其他推荐答案

尝试这种方式

首先,将图形作为占位符添加到您希望显示的报告中.

然后,右键单击图形 - >'格式图形' - >'图片'选项卡 - >添加公式

在构建路径字符串的"图形位置"下.

类似于'c:\ my folder \' + {table.clgid} +'.jpg'应该有效的东西.

如果您的更高版本,则

报告标头rightClick => insert => ole Object =>创建新的AS Drawing

右键单击对象=>选择图片选项卡=>在图形位置下设置位置公式

本文地址:https://www.itbaoku.cn/post/559614.html

问题描述

In my Crystal Report I need To add Header Image from specific Folder Path, for that I done Below Code

private void AddImage_ProdfailReport()
    {
        try
        {
            // here i have define a simple datatable inwhich image will recide 
            DataTable dt = new DataTable();
            // object of data row 
            DataRow drow;
            // add the column in table to store the image of Byte array type 
            dt.Columns.Add("Image", System.Type.GetType("System.Byte[]"));
            drow = dt.NewRow();
            // define the filestream object to read the image 
            FileStream fs;
            // define te binary reader to read the bytes of image 
            BinaryReader br;
            // check the existance of image 
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Footer.Jpg"))
            {
                // open image in file stream 
                fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "Footer.Jpg", FileMode.Open);
            }
            else
            {
                // if phot does not exist show the nophoto.jpg file 
                fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "Footer.jpg", FileMode.Open);
            }
            // initialise the binary reader from file streamobject 
            br = new BinaryReader(fs);
            // define the byte array of filelength 
            byte[] imgbyte = new byte[fs.Length + 1];
            // read the bytes from the binary reader 
            imgbyte = br.ReadBytes(Convert.ToInt32((fs.Length)));
            drow[0] = imgbyte;
            // add the image in bytearray 
            dt.Rows.Add(drow);
            // add row into the datatable 
            br.Close();
            // close the binary reader 
            fs.Close();
            // close the file stream 
            CreRe_ProdFail rptobj = new CreRe_ProdFail();
            // object of crystal report 
            rptobj.SetDataSource(dt);
            // set the datasource of crystalreport object 
            crv1.ReportSource = rptobj;
            //set the report source 
        }
        catch (Exception ex)
        {
            // error handling 
           MessageBox.Show("Missing Footer.jpg in application folder");
        }
        // run the application to view image in report 
    }

I created a field in a datatable of the dataset and change the DataType to System.Byte()

Then I drag this field to the report But its Showing An Error "This Field name is not known" , while Opening Report.. I am not getting what is Problem Please help me in it.

推荐答案

I done It; I am trying to add Data from Database And Image From Local Path and show in Crystal report; I done it by below code:

First: I created a New Column ("Image") in a datatable of the dataset and change the DataType to System.Byte()

Second : Drag And drop this image Filed Where I want.

private void LoadReport()
    {
        frmCheckWeigher rpt = new frmCheckWeigher();
        CryRe_DailyBatch report = new CryRe_DailyBatch();
        DataSet1TableAdapters.DataTable_DailyBatch1TableAdapter ta = new CheckWeigherReportViewer.DataSet1TableAdapters.DataTable_DailyBatch1TableAdapter();
        DataSet1.DataTable_DailyBatch1DataTable table = ta.GetData(clsLogs.strStartDate_rpt, clsLogs.strBatchno_Rpt, clsLogs.cmdeviceid); // Data from Database
        DataTable dt = GetImageRow(table, "Footer.Jpg");

        report.SetDataSource(dt);
        crv1.ReportSource = report;
        crv1.Refresh();

    }

// By this Function I merge My Image data into dataTable

 private DataTable GetImageRow(DataTable dt, string ImageName)
    {

        try
        {

            FileStream fs;
            BinaryReader br;

            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + ImageName))
            {
                fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + ImageName, FileMode.Open);
            }
            else
            {
                // if photo does not exist show the nophoto.jpg file 
                fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + ImageName, FileMode.Open);
            }
            // initialise the binary reader from file streamobject 
            br = new BinaryReader(fs);
            // define the byte array of filelength 
            byte[] imgbyte = new byte[fs.Length + 1];
            // read the bytes from the binary reader 
            imgbyte = br.ReadBytes(Convert.ToInt32((fs.Length)));

            dt.Rows[0]["Image"] = imgbyte;


            br.Close();
            // close the binary reader 
            fs.Close();
            // close the file stream 




        }
        catch (Exception ex)
        {
            // error handling 
            MessageBox.Show("Missing " + ImageName + "or nophoto.jpg in application folder");
        }
        return dt;
        // Return Datatable After Image Row Insertion

    }

其他推荐答案

Try this way

First, add a graphic as a placeholder into your report where you want it to display.

Then, right-click the graphic -> 'Format Graphic' -> 'Picture' tab -> Add a formula

under 'Graphic Location' that builds your path string.

Something like 'C:\My Folder\' + {table.Clgid} + '.jpg' should work.

If your Higher version then,

Report Header RightClick => INSERT => OLE Object => CREATE NEW as Drawing

After Right Click ON object => select Picture tab => set Location formula under Graphic Location