Pradeep
Pradeep

Reputation: 51

How to retrieve an image from varbinary code?

My database is SQL Server. In that one photo data column is there. That is varbinary datatype. How to retrieve the original image from that code. Please suggest any better way

Regards, Pradeep

Upvotes: 0

Views: 732

Answers (1)

Chris Gessler
Chris Gessler

Reputation: 23113

VarBinary is binary - so cast the field in the resultset to byte[]

byte[] bytes = (byte[])dataReader["fieldname"];

Then use a MemoryStream to convert the bytes to Image

public Image BytesToImage(byte[] bytes)
{
     using(MemoryStream ms = new MemoryStream(bytes))
     {
         Image image = Image.FromStream(ms);
         return image;
     }
}

Upvotes: 1

Related Questions