Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. retrieve image from database

retrieve image from database

Scheduled Pinned Locked Moved C#
questiondatabasecsharpsql-servergraphics
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mridang Agarwalla
    wrote on last edited by
    #1

    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 (); }

    J 1 Reply Last reply
    0
    • M Mridang Agarwalla

      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 (); }

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • J Judah Gabriel 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

        M Offline
        M Offline
        Mridang Agarwalla
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups