retrieve image from database
-
Hi guys, I havent done image related stuff in C# as yet but i need to ask a small question. I have stored an image in my database (SQL Server 2000). The image field in the database has a datatype of image. Now, I want to retrieve the image and display it in a image box How do i do that? I have found this code but how do I modify it to show the image in a Imagebox?
// Put user code to initialize the page here MemoryStream stream = new MemoryStream (); SqlConnection connection = new SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india"); try { connection.Open (); SqlCommand command = new SqlCommand ("select Picture from Image", connection); byte[] image = (byte[]) command.ExecuteScalar (); stream.Write (image, 0, image.Length); Bitmap bitmap = new Bitmap (stream); Response.ContentType = "image/gif"; bitmap.Save (Response.OutputStream, ImageFormat.Gif); } finally { connection.Close (); stream.Close (); }
-
Hi guys, I havent done image related stuff in C# as yet but i need to ask a small question. I have stored an image in my database (SQL Server 2000). The image field in the database has a datatype of image. Now, I want to retrieve the image and display it in a image box How do i do that? I have found this code but how do I modify it to show the image in a Imagebox?
// Put user code to initialize the page here MemoryStream stream = new MemoryStream (); SqlConnection connection = new SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india"); try { connection.Open (); SqlCommand command = new SqlCommand ("select Picture from Image", connection); byte[] image = (byte[]) command.ExecuteScalar (); stream.Write (image, 0, image.Length); Bitmap bitmap = new Bitmap (stream); Response.ContentType = "image/gif"; bitmap.Save (Response.OutputStream, ImageFormat.Gif); } finally { connection.Close (); stream.Close (); }
Once you've read the image in as a
byte[]
(as you showed in your code snippet), write it to a stream, then create an image from that stream:byte[] imageBytes = ... // grab the image bytes from the database.
MemoryStream stream = new MemoryStream(imageBytes);
Image image = Image.FromStream(stream);
stream.dispose();Tech, life, family, faith: Give me a visit. I'm currently blogging about: Check out this cutie The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Once you've read the image in as a
byte[]
(as you showed in your code snippet), write it to a stream, then create an image from that stream:byte[] imageBytes = ... // grab the image bytes from the database.
MemoryStream stream = new MemoryStream(imageBytes);
Image image = Image.FromStream(stream);
stream.dispose();Tech, life, family, faith: Give me a visit. I'm currently blogging about: Check out this cutie The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
umm..There might be a slight problem and I might even have posted in the wrong forum (my bad). Its a web application in c#. Its and Image control