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. Retrieving Image from SQLServer database

Retrieving Image from SQLServer database

Scheduled Pinned Locked Moved C#
databasegraphicsquestion
4 Posts 3 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
    Meysam Mahfouzi
    wrote on last edited by
    #1

    Hi I read a field of type Image from a SQLServer database. Now, what I want to do is creating a System.Drawing.Image object and holding my retrieved image into it. How?


    Don't forget, that's

    Persian Gulf

    not Arabian gulf!

    P A 2 Replies Last reply
    0
    • M Meysam Mahfouzi

      Hi I read a field of type Image from a SQLServer database. Now, what I want to do is creating a System.Drawing.Image object and holding my retrieved image into it. How?


      Don't forget, that's

      Persian Gulf

      not Arabian gulf!

      P Offline
      P Offline
      peenu
      wrote on last edited by
      #2

      doesn't Image myImage = Image:FromFile(filename); work?

      1 Reply Last reply
      0
      • M Meysam Mahfouzi

        Hi I read a field of type Image from a SQLServer database. Now, what I want to do is creating a System.Drawing.Image object and holding my retrieved image into it. How?


        Don't forget, that's

        Persian Gulf

        not Arabian gulf!

        A Offline
        A Offline
        Arjan Einbu
        wrote on last edited by
        #3

        Use SqlCommand's ExecuteScalar method to retrieve the blob as a byte array, and load it with the Image.FromStream(...) static method using a MemoryStream object. Sounds easy, eh? Well, here's a code sample. GetImage() returns an Image object as you requested.

        public Image GetImage()
        {
        return Image.FromStream(new MemoryStream(LoadImageBLOB(mImagePK)));
        }

        private byte[] LoadImageBLOB(int imagePK)
        {
        string sql = "SET TEXTSIZE 1000000\n" +
        "SELECT the_blob_column FROM the_table " +
        "WHERE the_where_clause\n" +
        "SET TEXTSIZE 0";

        SqlConnection cn = new SqlConnection("_the\_connection\_string_");
        SqlCommand cmd = new SqlCommand(sql, cn);
        
        cn.Open();
        byte\[\] blob = (byte\[\])pq.ExecuteScalar();
        cn.Close();
        
        return blob;
        

        }

        M 1 Reply Last reply
        0
        • A Arjan Einbu

          Use SqlCommand's ExecuteScalar method to retrieve the blob as a byte array, and load it with the Image.FromStream(...) static method using a MemoryStream object. Sounds easy, eh? Well, here's a code sample. GetImage() returns an Image object as you requested.

          public Image GetImage()
          {
          return Image.FromStream(new MemoryStream(LoadImageBLOB(mImagePK)));
          }

          private byte[] LoadImageBLOB(int imagePK)
          {
          string sql = "SET TEXTSIZE 1000000\n" +
          "SELECT the_blob_column FROM the_table " +
          "WHERE the_where_clause\n" +
          "SET TEXTSIZE 0";

          SqlConnection cn = new SqlConnection("_the\_connection\_string_");
          SqlCommand cmd = new SqlCommand(sql, cn);
          
          cn.Open();
          byte\[\] blob = (byte\[\])pq.ExecuteScalar();
          cn.Close();
          
          return blob;
          

          }

          M Offline
          M Offline
          Meysam Mahfouzi
          wrote on last edited by
          #4

          Thanx :):rose::):rose::)


          Don't forget, that's

          Persian Gulf

          not Arabian gulf!

          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