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. Assembling bytes to an image

Assembling bytes to an image

Scheduled Pinned Locked Moved C#
graphicssysadmindata-structureshelptutorial
7 Posts 4 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.
  • W Offline
    W Offline
    Wamuti
    wrote on last edited by
    #1

    Hi all. I have a client-server distributed system. I want to transfer and image from the server to the client. In the server i have converted a .bmp image to byte array as this:

    Image imageIn = Image.FromFile("me.bmp") ;
    MemoryStream msr = new MemoryStream();
    imageIn.Save(msr, System.Drawing.Imaging.ImageFormat.Gif);
    byte\[\] byteIm = msr.ToArray();
    
    ms.Send(byteIm);
    

    The bytes are being transfered okay to the client. The problem is that i don't know how to assemble this byte back to an image to show in a picture box. That is:

    pictureBox1.Image =

    :confused:

    Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

    OriginalGriffO D D 3 Replies Last reply
    0
    • W Wamuti

      Hi all. I have a client-server distributed system. I want to transfer and image from the server to the client. In the server i have converted a .bmp image to byte array as this:

      Image imageIn = Image.FromFile("me.bmp") ;
      MemoryStream msr = new MemoryStream();
      imageIn.Save(msr, System.Drawing.Imaging.ImageFormat.Gif);
      byte\[\] byteIm = msr.ToArray();
      
      ms.Send(byteIm);
      

      The bytes are being transfered okay to the client. The problem is that i don't know how to assemble this byte back to an image to show in a picture box. That is:

      pictureBox1.Image =

      :confused:

      Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      have a look at this[^]

      You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      W 1 Reply Last reply
      0
      • W Wamuti

        Hi all. I have a client-server distributed system. I want to transfer and image from the server to the client. In the server i have converted a .bmp image to byte array as this:

        Image imageIn = Image.FromFile("me.bmp") ;
        MemoryStream msr = new MemoryStream();
        imageIn.Save(msr, System.Drawing.Imaging.ImageFormat.Gif);
        byte\[\] byteIm = msr.ToArray();
        
        ms.Send(byteIm);
        

        The bytes are being transfered okay to the client. The problem is that i don't know how to assemble this byte back to an image to show in a picture box. That is:

        pictureBox1.Image =

        :confused:

        Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

        D Offline
        D Offline
        dan sh
        wrote on last edited by
        #3

        Here you go:

        MemoryStream memStream = new MemoryStream(imageBytes);
        Image image = Image.FromStream(memStream);

        W 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          have a look at this[^]

          You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

          W Offline
          W Offline
          Wamuti
          wrote on last edited by
          #4

          Actually that is where i knew how to convert and image to bytes but the function returning an Image, how will the pictureBox get the URL?

          Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

          OriginalGriffO 1 Reply Last reply
          0
          • D dan sh

            Here you go:

            MemoryStream memStream = new MemoryStream(imageBytes);
            Image image = Image.FromStream(memStream);

            W Offline
            W Offline
            Wamuti
            wrote on last edited by
            #5

            Thanks guys. It worked.

            Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

            1 Reply Last reply
            0
            • W Wamuti

              Actually that is where i knew how to convert and image to bytes but the function returning an Image, how will the pictureBox get the URL?

              Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Why would the picture box want a URL? Just give it the image, or save it to disk and then give it the url as the file location. From your original code:

               MemoryStream ms = new MemoryStream(byteArrayIn);
               pictureBox1.Image = Image.FromStream(ms);
              

              You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • W Wamuti

                Hi all. I have a client-server distributed system. I want to transfer and image from the server to the client. In the server i have converted a .bmp image to byte array as this:

                Image imageIn = Image.FromFile("me.bmp") ;
                MemoryStream msr = new MemoryStream();
                imageIn.Save(msr, System.Drawing.Imaging.ImageFormat.Gif);
                byte\[\] byteIm = msr.ToArray();
                
                ms.Send(byteIm);
                

                The bytes are being transfered okay to the client. The problem is that i don't know how to assemble this byte back to an image to show in a picture box. That is:

                pictureBox1.Image =

                :confused:

                Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

                D Offline
                D Offline
                DX Roster
                wrote on last edited by
                #7

                try following code: private void Button2_Click(object sender, System.EventArgs e) { connection.Open(); SqlCommand command1 = new SqlCommand("select imgfile from myimages where imgname=@param", connection); SqlParameter myparam = command1.Parameters.Add("@param", SqlDbType.NVarChar, 30); myparam.Value = txtimgname.Text; byte[] img = (byte[])command1.ExecuteScalar(); MemoryStream str = new MemoryStream(); str.Write(img, 0, img.Length); Bitmap bit = new Bitmap(str); Response.ContentType = "image/jpeg";//or you can select your imagetype from database or directly write it here bit.Save(Response.OutputStream, ImageFormat.Jpeg); connection.Close(); } change your database name, field name, image name, attribute name. If You Get your answer then please Rating me... Thanks.. "Are You Ready" DX-ARMY

                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