report viewer pass image from form possible?

2019-02-14 16:01发布

Using visual Studio ultimate 2012.

Im currently building a report to be printed in report viewer. so far i have a bunch of text boxes that Gets its values from my form text boxes via parameters.

So far all works fine.

Then I hit a major problem. How do you pass an Image From my images on my form to an image on a report? 1 image pre exists on a database i beleive i can call into the image as a parameter(not sure). the bigger issue is the other image.

The other image uses an external API that generates QR images. this image is displayed in a picture box on my form at runtime. I am not saving the image anywhere i would prefer not too. BUT i understand if i may need to. Is there any way at all i can pass the QR image from the image box on my form to my report Image box?

Update heres the code for the error:

Microsoft.Reporting.WinForms.ReportParameter rpIMG1 = new Microsoft.Reporting.WinForms.ReportParameter("paramQRimg", base64String);
Microsoft.Reporting.WinForms.ReportParameter rpIMG2 = new Microsoft.Reporting.WinForms.ReportParameter("paramQRMi", "image/png");

reportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter[] { rp1, rp2, rp3, rp4, rp5, rp6, rp7, rp8, rp9, rp10, rpIMG1, rpIMG2 });

Error occurs on the set Parameters part all it says is:

An error occurred during local report processing.

no idea why it doesn't like this

2条回答
欢心
2楼-- · 2019-02-14 16:40
 public string ImageToBase64(Image image, 
  System.Drawing.Imaging.ImageFormat format)
{
  using (MemoryStream ms = new MemoryStream())
  {
    // Convert Image to byte[]
    image.Save(ms, format);
    byte[] imageBytes = ms.ToArray();

    // Convert byte[] to Base64 String
    string base64String = Convert.ToBase64String(imageBytes);
    return base64String;
  }
}

Convert your image to base64 string and then pass it to your report as parameter and then set the Report image to this parameter.

查看更多
神经病院院长
3楼-- · 2019-02-14 16:44
 private void header()
    {
        try
        {
            string name = "";
            string address = "";
            string phone = "";
            string mobile = "";
            string establish = "";

            db.sql.Close();
            db.sql.Open();
            SqlCommand cmd = new SqlCommand("select * from print_head", db.sql);
            SqlDataReader read = cmd.ExecuteReader();
            while (read.Read())
            {
                name = read[1].ToString();
                address = read[2].ToString();
                phone = read[3].ToString();
                mobile = read[4].ToString();
                establish = read[5].ToString();
                MemoryStream ms = new MemoryStream((byte[])read[6]);
                logo = Image.FromStream(ms);
                try
                {

                        // Convert Image to byte[]

                        byte[] imageBytes = ms.ToArray();

                        // Convert byte[] to Base64 String
                        base64String = Convert.ToBase64String(imageBytes);

                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            ReportParameterCollection r = new ReportParameterCollection();
            r.Add(new ReportParameter("name", name.ToString()));
            r.Add(new ReportParameter("address", address.ToString()));
            r.Add(new ReportParameter("phone", phone.ToString()));
            r.Add(new ReportParameter("mobile", mobile.ToString()));
            r.Add(new ReportParameter("establish", establish.ToString()));
            r.Add(new ReportParameter("logo", base64String.ToString()));

            this.reportViewer1.LocalReport.SetParameters(r);
            db.sql.Close();
        }
        catch
        {

        }
    }
查看更多
登录 后发表回答