report viewer pass image from form possible?

后端 未结 2 522
一向
一向 2021-01-01 03:43

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 fo

相关标签:
2条回答
  • 2021-01-01 04:08
     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 讨论(0)
  • 2021-01-01 04:15
     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.

    0 讨论(0)
提交回复
热议问题