5

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

BenMorel
  • 34,448
  • 50
  • 182
  • 322
lemunk
  • 2,616
  • 12
  • 57
  • 87

3 Answers3

11
 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.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Talha
  • 18,898
  • 8
  • 49
  • 66
  • sorry just looked at this again i dont see where my image is called? my image is generated at runtime and put into the picture box on my form. what would the filename be? – lemunk Nov 15 '12 at 11:59
  • @StevenSmith ok, try the updated function to convert into base 64 – Talha Nov 15 '12 at 12:04
  • right created the method, created a parameter and passed the string into it(all on form). in the report i Have an image i set the "usethisimage" to Parameters!paramQRimg.Value. it errors sayin no definition. i then decide to create a parameter on the report which works fine, then pass it into my image box just like my textboxes, no error but no image iether, i did notice when setting the type of parameter it only had primitive types, no image. any idea how to set the report part? – lemunk Nov 15 '12 at 12:15
  • that i did as the parameter it is a text but no image is displayed – lemunk Nov 15 '12 at 12:24
  • ok, replace some part of your base64 string before passing it to report. that is like string base64 = base64.Replace("data:image/png;base64,", ""); @StevenSmith – Talha Nov 15 '12 at 12:27
  • pass the updated string to report parameter, i hope it will work and its last thing i have – Talha Nov 15 '12 at 12:28
  • @StevenSmith if your problem is not solved, than check this thread http://stackoverflow.com/questions/36693/how-can-i-render-a-png-image-as-a-memory-stream-onto-a-net-reportviewer-repor – Talha Nov 15 '12 at 12:33
  • no change :( just getting an X box top right of the image as usual – lemunk Nov 15 '12 at 12:33
  • 2
    @StevenSmith http://stackoverflow.com/questions/36693/how-can-i-render-a-png-image-as-a-memory-stream-onto-a-net-reportviewer-repor – Talha Nov 15 '12 at 12:35
  • ok from the information there =System.Convert.FromBase64String(Parameters!paramQRimg.Value) This when i hit ok in the image "usethisdifield" it has <> MMI on the other hand =Parameters!paramQRmi.Value comes out at [@paramQRmi] the report allows this but still no image displayed – lemunk Nov 15 '12 at 12:45
  • @StevenSmith no need to use its image to base64 convert method, just do else – Talha Nov 15 '12 at 12:56
  • Ok relised i never called the 2nd paramter that deals with the MII. I called this but an error occurs. the error itself has no information it simply says an error occured Ive updated my post – lemunk Nov 15 '12 at 13:11
  • http://satishjdotnet.blogspot.com/2009/03/external-images-in-rdlc-reports-aspnet.html another technique to display image on reports @StevenSmith – Talha Nov 15 '12 at 13:18
  • @lemunk: I've same issue, I'tried as you described here, but that not worked for me please check my question [here](http://stackoverflow.com/q/42020490/5743676) – Hina Khuman Mar 20 '17 at 10:42
0
 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
        {

        }
    }
0

this is worked for Me:

ReportParameter rp = new ReportParameter("ImagePath",new Uri(yourImagePath).AbsoluteUri ));

and in report designer set picture property Source: external

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
abo_mbark
  • 11
  • 1
  • 4