report viewer pass image from form possible?
Asked Answered
M

3

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

Mantellone answered 15/11, 2012 at 11:39 Comment(1)
set your parameter as one by one reportViewer.LocalReport.SetParameters(paramLogo);Strom
S
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.

Strom answered 15/11, 2012 at 11:56 Comment(14)
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?Mantellone
@StevenSmith ok, try the updated function to convert into base 64Strom
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?Mantellone
that i did as the parameter it is a text but no image is displayedMantellone
ok, replace some part of your base64 string before passing it to report. that is like string base64 = base64.Replace("data:image/png;base64,", ""); @StevenSmithStrom
pass the updated string to report parameter, i hope it will work and its last thing i haveStrom
@StevenSmith if your problem is not solved, than check this thread #37193Strom
no change :( just getting an X box top right of the image as usualMantellone
@StevenSmith #37193Strom
ok from the information there =System.Convert.FromBase64String(Parameters!paramQRimg.Value) This when i hit ok in the image "usethisdifield" it has <<Expr>> MMI on the other hand =Parameters!paramQRmi.Value comes out at [@paramQRmi] the report allows this but still no image displayedMantellone
@StevenSmith no need to use its image to base64 convert method, just do elseStrom
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 postMantellone
satishjdotnet.blogspot.com/2009/03/… another technique to display image on reports @StevenSmithStrom
@lemunk: I've same issue, I'tried as you described here, but that not worked for me please check my question hereDiseuse
B
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
        {

        }
    }
Becalmed answered 11/1, 2018 at 7:50 Comment(0)
L
0

this is worked for Me:

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

and in report designer set picture property Source: external

Lawley answered 8/1, 2023 at 14:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.