Skip to content

Instantly share code, notes, and snippets.

@riccardopirani
Last active January 16, 2018 15:24
Show Gist options
  • Select an option

  • Save riccardopirani/5a090d2bd673ee8c446f2f1d0612fa74 to your computer and use it in GitHub Desktop.

Select an option

Save riccardopirani/5a090d2bd673ee8c446f2f1d0612fa74 to your computer and use it in GitHub Desktop.
Convert any file varbinary to bmp
int conta_righe = 0;
SqlConnection conn = Database.apriconnessione();
try {
String Query = "SELECt DocumentiCantiere.IdDocumento,''+Utente.nome+' '+Utente.Cognome as InseritoDA,convert(varchar, DataInserimento,103) as DataInserimento,DocumentiCantiere.DocumentoFile FROM DocumentiCantiere inner join Utente on Utente.IdUtente = DocumentiCantiere.IdUtenteInserimento where DocumentiCantiere.IdCantiere=@IdCantiere";
SqlCommand cmd = new SqlCommand(Query, conn);
cmd.Parameters.AddWithValue("@IdCantiere", c.IdCantiere);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count != 0)
{
dataGridViewDocumenti.Rows.Clear();
foreach (DataRow dr in dt.Rows)
{
String[] row = { dr["IdDocumento"].ToString(), dr["InseritoDA"].ToString(), dr["DataInserimento"].ToString()};
dataGridViewDocumenti.Rows.Add(row);
//conversione immagine
byte[] picData = dr["DocumentoFile"] as byte[] ?? null;
if (picData != null)
{
using (MemoryStream ms = new MemoryStream(picData))
{
Bitmap bmp = new Bitmap(new Bitmap(ms), 90, 90);
bmp.Save(ms, ImageFormat.Png);
dataGridViewDocumenti.Rows[conta_righe].Cells["Documento"].Value = bmp;
}
}
//fine conversione
conta_righe++;
}
}
}
catch(Exception ex)
{
MessageBox.Show("error");
Managementerror.SendError("error: " + ex);
}
db.chiudiconnessione();
conn.Close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment