Friday, May 1, 2009

Converting blob data from sql server to image in C#.NET

Byte[] bytImage=null;

//Change the ConnString as per your system.

string constring = @"Data Source=LOCAL;Initial Catalog=DA;Integrated Security=True;"; SqlCommand command = new SqlCommand(@"SELECT BlobData FROM Lib.LibBlob WHERE BlobID='04F24251-AE4C-4FDA-BDB7-0689C9616462'"); command.CommandType = CommandType.Text;

SqlConnection myconn = new SqlConnection(constring);

command.Connection = myconn;

myconn.Open();

SqlDataReader dr = command.ExecuteReader();

while(dr.Read())

{

bytImage = (byte[])dr["BlobData"];

}

if (bytImage !=null)

{

//saving this to bmp file

MemoryStream ms = new MemoryStream(bytImage);

System.Drawing.Bitmap BMP = new System.Drawing.Bitmap(ms);

BMP.Save("C:\\Temp\\Test.bmp");

//saving to jpg image

//System.Drawing.Image img = new System.Drawing.Bitmap(ms);

//img.Save("C:\\Temp\\Test1.jpeg", ImageFormat.Jpeg);

}

Post a Comment

Blog Popularty Partners

  ©All Right Reserved.

Back to TOP