Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Problem
When try to display image from datatable and display image on print document it show as system.byte[] in case of image exist on datatable ?

public DataTable GetChecked()
      {
          DataTable table = new DataTable();
          table.Columns.Add("MemberCode", typeof(string));
          table.Columns.Add("MemberImage", typeof(Byte[]));

          for (int i = 0; i < dtDisplayDataPayment.Rows.Count; i++)
          {

              bool Ischecked = Convert.ToBoolean(GridFooter.Rows[i].Cells["PrintFlag"].Value);
              if (Ischecked == true)
              {
                  DataRow newRow = table.NewRow();
                  newRow["MemberCode"] = Utilities.ObjectConverter.ConvertToString(dtDisplayDataPayment.Rows[i]["MemberCode"]);



                      if (dtDisplayDataPayment.Rows[i]["MemberImage"] != System.DBNull.Value)
                      {
                          byte[] photo_aray = (byte[])dtDisplayDataPayment.Rows[i]["MemberImage"];
                          System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
                          Image img = (Image)converter.ConvertFrom(photo_aray);
                          newRow["MemberImage"] = imageToByteArray(img);



                  }
                  table.Rows.Add(newRow);
              }
          }

          return table;


      }


What I have tried:

private void button1_Click(object sender, EventArgs e)
        {
            //for datatable
            PrintDocument document = new PrintDocument();
            document.PrintPage += new PrintPageEventHandler(document_PrintPage);

            PrintPreviewDialog ppDialog = new PrintPreviewDialog();
            ppDialog.Document = document;
            ((ToolStripButton)((ToolStrip)ppDialog.Controls[1]).Items[0]).Enabled = false;
            ppDialog.Show();
            //==============
           
        }


void document_PrintPage(object sender, PrintPageEventArgs e)
        {
            dtGetChecked = GetChecked();
            //==============================For Datatable
            PrintDocument document = (PrintDocument)sender;
            Graphics g = e.Graphics;

            Brush brush = new SolidBrush(Color.Black);
            Pen pen = new Pen(brush);
            Font font = new Font("Arial", 10, FontStyle.Bold);
            Font fonte = new Font("Arial", 15, FontStyle.Bold);
            int x = 0, y = 0, width = 200, height = 30;

            SizeF sizeeee = g.MeasureString("TIME :: ", fonte);
            float xPaddingeee = (width - sizeeee.Width) / 2;
            g.DrawString("TIME :: ", fonte, brush, x + xPaddingeee, y + 5);
            x += width;


            SizeF sizee = g.MeasureString(DateTime.Now.ToString(), fonte);
            float xPaddinge = (width - sizee.Width) / 2;

            g.DrawString(DateTime.Now.ToString(), fonte, brush, x + xPaddinge, y + 5);
            x += width;

            for (int kk = 0; kk < 2; kk++)
            {
                SizeF sizeee = g.MeasureString("", font);
                float xPaddingee = (width - sizee.Width) / 2;

                g.DrawString("", font, brush, x + xPaddingee, y + 5);
                x += width;
            }
            x = 0;
            y += 60;


            foreach (DataColumn column in dtGetChecked.Columns)
            {
                g.DrawRectangle(pen, x, y, width, height);
                SizeF size = g.MeasureString(column.ColumnName, fonte);
                float xPadding = (width - size.Width) / 2;

                g.DrawString(column.ColumnName, fonte, brush, x + xPadding, y + 5);
                x += width;
            }


            x = 0;
            y += 30;
            int columnCount = dtGetChecked.Columns.Count;

            foreach (DataRow row in dtGetChecked.Rows)
            {
                for (int i = 0; i < columnCount; i++)
                {
                    g.DrawRectangle(pen, x, y, width, height);
                    SizeF size = g.MeasureString(row[i].ToString(), font);
                    float xPadding = (width - size.Width) / 2;
                   
                    g.DrawString(row[i].ToString(), font, brush, x + xPadding, y + 5);
                    
                    x += width;
                }
                x = 0;
                y += 30;
            }
Posted
Updated 31-Jan-19 14:41pm
Comments
ahmed_sa 31-Jan-19 19:28pm    
please can any one help me

1 solution

Nowhere in your print drawing code is there anything that checks what kind of value is in each DataTable cell. It assumes every column is a string. You're using DrawString for an image column. That's why you get the System.Byte[] on the print instead of an image.

You have to use DrawImage if you want a Bitmap to show up on the print.
 
Share this answer
 
Comments
ahmed_sa 31-Jan-19 20:43pm    
can you please if possible show me modified code please
Dave Kreskowiak 31-Jan-19 20:44pm    
Nope. I'm not here to write your code for you and I currently don't have the time.
ahmed_sa 31-Jan-19 20:47pm    
if (i == 3)
{
g.DrawImage(row[i].ToString(), font, brush, x + xPadding, y + 5);

}
else
{


g.DrawString(row[i].ToString(), font, brush, x + xPadding, y + 5);
}
Dave Kreskowiak 31-Jan-19 22:34pm    
FFS. Why would you use a FONT in an operation to draw an image??

Try reading the documentation on DrawImage, here[^]
ahmed_sa 31-Jan-19 20:47pm    
i try but give me error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900