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. Add image to database

Add image to database

Scheduled Pinned Locked Moved C#
csharpdatabasequestion
5 Posts 5 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.
  • A Offline
    A Offline
    Amirjalaly
    wrote on last edited by
    #1

    Hi How can insert a image or read it and show in ado.net? i want to store image (for exmaple .jpg or bmp) in database and show them in form(with which controls??) Regards' Amirjalay

    J M H T 4 Replies Last reply
    0
    • A Amirjalaly

      Hi How can insert a image or read it and show in ado.net? i want to store image (for exmaple .jpg or bmp) in database and show them in form(with which controls??) Regards' Amirjalay

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

      SQL database can accept the Binary type, which corresponds to an C# byte[]. So convert your bitmap into an array of bytes when you want to store it in the database. And when you're ready to read it from the database, construct one from the read in bytes (you should be able to use something like Bitmap.FromStream). The graveyards are filled with indispensible men.

      1 Reply Last reply
      0
      • A Amirjalaly

        Hi How can insert a image or read it and show in ado.net? i want to store image (for exmaple .jpg or bmp) in database and show them in form(with which controls??) Regards' Amirjalay

        M Offline
        M Offline
        Mazdak
        wrote on last edited by
        #3

        http://codeproject.com/cs/database/albumviewer.asp[^] Mazy "A bank is a place that will lend you money if you can prove that you don't need it." - Bob Hope

        1 Reply Last reply
        0
        • A Amirjalaly

          Hi How can insert a image or read it and show in ado.net? i want to store image (for exmaple .jpg or bmp) in database and show them in form(with which controls??) Regards' Amirjalay

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          This has been covered many times in this forum. Please click "Search comments" above. One specific thread not so long ago can be found here[^].

          Microsoft MVP, Visual C# My Articles

          1 Reply Last reply
          0
          • A Amirjalaly

            Hi How can insert a image or read it and show in ado.net? i want to store image (for exmaple .jpg or bmp) in database and show them in form(with which controls??) Regards' Amirjalay

            T Offline
            T Offline
            turbochimp
            wrote on last edited by
            #5

            1. Create a binary column in your database large enough to store the picture (image is a good choice). 2. Stream the target picture (System.Drawing.Image instance or whatever) to a byte array (I generally use the System.IO.MemoryStream class and the System.Runtime.Serialization.Formatters.Binary.BinaryFormatter to create the byte stream for writing to the DB) Example Image Encode: ===================== Image myImage = Image.FromFile(filePath); MemoryStream stream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, myImage); byte[] imageBytes = stream.GetBuffer(); //Insert code to update the database column from step 1 with the //contents of imageBytes 3. To retrieve the target image, load the row containing it, and get the byte array from the column containing the image data, then use the System.Drawing.Image.FromStream static method to instantiate a new Image instance. hope this helps... No design ever survives first contact with its users.

            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