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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. display a pic from the database

display a pic from the database

Scheduled Pinned Locked Moved C#
helpdatabasegraphicsquestion
4 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.
  • A Offline
    A Offline
    Ahmed Galal
    wrote on last edited by
    #1

    am developing an application that uploads an image to the database and other users can view this pic, uploading is ok, but the problem in displaying the pic from the database, heres my code.

    image = (byte[])this.mData.Tables[0].Rows[x]["Image"];
    
    System.IO.MemoryStream memStream = new System.IO.MemoryStrea(image);
    
    System.Drawing.Image Img = System.Drawing.Image.FromStream(memStream);
    

    the data type of the column "Image" in the database is "image" everytime i run the code i get Argument Exception "Invalid parameter used." on the line System.Drawing.Image Img = System.Drawing.Image.FromStream(memStream); i look everywhere on the net but they r using the same way i think, i dunno where the problem is. any help ? thanx

    S 1 Reply Last reply
    0
    • A Ahmed Galal

      am developing an application that uploads an image to the database and other users can view this pic, uploading is ok, but the problem in displaying the pic from the database, heres my code.

      image = (byte[])this.mData.Tables[0].Rows[x]["Image"];
      
      System.IO.MemoryStream memStream = new System.IO.MemoryStrea(image);
      
      System.Drawing.Image Img = System.Drawing.Image.FromStream(memStream);
      

      the data type of the column "Image" in the database is "image" everytime i run the code i get Argument Exception "Invalid parameter used." on the line System.Drawing.Image Img = System.Drawing.Image.FromStream(memStream); i look everywhere on the net but they r using the same way i think, i dunno where the problem is. any help ? thanx

      S Offline
      S Offline
      Sasuko
      wrote on last edited by
      #2

      I use this instead: OleDbCommand cmd = new OleDbCommand("select Image " + "from Table where Name=@Name", conn); OleDbParameter param = new OleDbParameter("@Name", OleDbType.VarChar); param.Value = textBoxName.Text; cmd.Parameters.Add(param); OleDbDataReader reader = cmd.ExecuteReader(); reader.Read(); long length = reader.GetBytes(0, 0, null, 0, 0); byte[] buffer = new byte[length]; length = reader.GetBytes(0, 0, buffer, 0, (int)length); if (buffer.Length == length) { MemoryStream ms = new MemoryStream(buffer); mImage = Image.FromStream(ms); Adapt this code to your and enjoy. }

      A 2 Replies Last reply
      0
      • S Sasuko

        I use this instead: OleDbCommand cmd = new OleDbCommand("select Image " + "from Table where Name=@Name", conn); OleDbParameter param = new OleDbParameter("@Name", OleDbType.VarChar); param.Value = textBoxName.Text; cmd.Parameters.Add(param); OleDbDataReader reader = cmd.ExecuteReader(); reader.Read(); long length = reader.GetBytes(0, 0, null, 0, 0); byte[] buffer = new byte[length]; length = reader.GetBytes(0, 0, buffer, 0, (int)length); if (buffer.Length == length) { MemoryStream ms = new MemoryStream(buffer); mImage = Image.FromStream(ms); Adapt this code to your and enjoy. }

        A Offline
        A Offline
        Ahmed Galal
        wrote on last edited by
        #3

        am using dataset instead of the SqlDataReader, so how i retrive the column in byte format, casting ? so it will be like byte[] img = (byte[])mData.Tables[0].Rows[0]["image"]; MemoryStream ms = new MemoryStream(img); System.Drawing.Image myImage = System.Drawing.Image.FromStream(ms); then, i'll back the same point and to the same exception. could u explain much more please. thanx for ur reply

        1 Reply Last reply
        0
        • S Sasuko

          I use this instead: OleDbCommand cmd = new OleDbCommand("select Image " + "from Table where Name=@Name", conn); OleDbParameter param = new OleDbParameter("@Name", OleDbType.VarChar); param.Value = textBoxName.Text; cmd.Parameters.Add(param); OleDbDataReader reader = cmd.ExecuteReader(); reader.Read(); long length = reader.GetBytes(0, 0, null, 0, 0); byte[] buffer = new byte[length]; length = reader.GetBytes(0, 0, buffer, 0, (int)length); if (buffer.Length == length) { MemoryStream ms = new MemoryStream(buffer); mImage = Image.FromStream(ms); Adapt this code to your and enjoy. }

          A Offline
          A Offline
          Ahmed Galal
          wrote on last edited by
          #4

          i also mentioned somethin, that everytime the length of the byte is 13 even if i changed the pic. i mean that this line of code always returns a bytes of length 13 byte[] img = (byte[])mData.Tables[0].Rows[0]["image"]; byte length is always 13

          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