Saludos comunidad, estoy tratando de pasar a un PdfpCell (iTextSharp) una imagen desde un datagrid, a continuación mi code hardcodeado:
Código C:
Ver original//Método para cargar datos en el Grid desde SQL
private void btnExec_Click(object sender, EventArgs e)
{
try
{
SqlCommand cmd = new SqlCommand();
DataSet DataS = new DataSet();
cmd.CommandText = "storedproc";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@param", param.Value);
cmd.Connection = new System.Data.SqlClient.SqlConnection("strconections");
SqlDataAdapter Ad = new SqlDataAdapter(cmd);
Ad.Fill(DataS);
dgv.DataSource = DataS.Tables[0];
cmd.Connection.Close();
datagrid.ClearSelection();
metodoparagenerarpdf();
}
catch (Exception ex)
{
Console.WriteLine("{0} Error!", ex);
}
}
//Evento Cell Formatting para cargar la imagen en la columna del Datagrid
private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 2)
{
if (e.Value == DBNull.Value)
{
//Imagen x defectp
string path = (Environment.CurrentDirectory + @"\\img\0.jpg");
e.Value = new Bitmap(System.Drawing.Bitmap.FromFile(path), new Size(100, 100));
}
else
{
//Seteo la imagen y redimenciono medidas
System.IO.MemoryStream ms = new System.IO.MemoryStream((byte[])e.Value);
e.Value = new Bitmap(System.Drawing.Image.FromStream(ms), 100, 100);
e.FormattingApplied = true;
}
}
}
//Método para cargar los datos en una tabla
foreach (DataGridViewRow Row in dgv.Rows)
{
//PdfPCell c6 = new PdfPCell(new Phrase()); //Foto
PdfPCell c7 = new PdfPCell(new Phrase(Row.Cells[3].Value.ToString(), smallfont)); //Nombre
PdfPCell c8 = new PdfPCell(new Phrase(Row.Cells[4].Value.ToString() + " %", bigfont)); //Score
PdfPCell c9 = new PdfPCell(new Phrase(Row.Cells[0].Value.ToString(), bigfont)); //# Viajes
PdfPCell c10 = new PdfPCell(new Phrase(Row.Cells[5].Value.ToString(), smallfont)); //Terminal
c7.Border = 0;
c7.HorizontalAlignment = 1;
table.AddCell(c7);
c8.Border = 0;
c8.HorizontalAlignment = 1;
table.AddCell(c8);
c9.Border = 0;
c9.HorizontalAlignment = 1;
table.AddCell(c9);
c10.Border = 0;
c10.HorizontalAlignment = 1;
//table.AddCell(c10);
}
Yo lo que necesito es saber como puedo hacerle para pasar el valor que tengo en la columna (Row.Cells[2].Value.ToString()) en mi grid y convertirlo a bitmap o imagen y que se pueda ver en mi PDF, de antemano muchas gracias!
//EDIT
Intenté hacer esto, pero tampoco sin resultado :(
Código C:
Ver originalbyte[] bts= new byte[Row.Cells[2].Value.ToString().Length * sizeof(char)];
System.Buffer.BlockCopy(Row.Cells[2].Value.ToString().ToCharArray(), 0, bytes, 0, bytes.Length);
iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance(bts);
doc.Add(img1);
Saludos