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. Bitmap to byte array?

Bitmap to byte array?

Scheduled Pinned Locked Moved C#
graphicsdata-structurestutorialquestion
6 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.
  • D Offline
    D Offline
    Dr Herbie
    wrote on last edited by
    #1

    Hi, I have a System.Drawing.Bitmap of a screen capture that I want to convert to a byte array. Any ideas on how to do this? I expected the bitmap object to have some sort of ToArray method, but it hasn't. I'm rather stumped. TIA Dr Herbie Remember, half the people out there have below average IQs.

    C T 2 Replies Last reply
    0
    • D Dr Herbie

      Hi, I have a System.Drawing.Bitmap of a screen capture that I want to convert to a byte array. Any ideas on how to do this? I expected the bitmap object to have some sort of ToArray method, but it hasn't. I'm rather stumped. TIA Dr Herbie Remember, half the people out there have below average IQs.

      C Offline
      C Offline
      Charlie Williams
      wrote on last edited by
      #2

      You can save the image to a System.IO.MemoryStream, which does have the elusive ToArray method. Charlie if(!curlies){ return; }

      1 Reply Last reply
      0
      • D Dr Herbie

        Hi, I have a System.Drawing.Bitmap of a screen capture that I want to convert to a byte array. Any ideas on how to do this? I expected the bitmap object to have some sort of ToArray method, but it hasn't. I'm rather stumped. TIA Dr Herbie Remember, half the people out there have below average IQs.

        T Offline
        T Offline
        thomasa
        wrote on last edited by
        #3

        This should do the trick :) public static byte[] GenerateImageBytes(Bitmap bm) { byte[] bytes = null; System.IO.MemoryStream ms = new System.IO.MemoryStream(); bm.Save(ms, ImageFormat.Gif); bytes = ms.ToArray(); ms.Close(); return bytes; } Thomas

        J 1 Reply Last reply
        0
        • T thomasa

          This should do the trick :) public static byte[] GenerateImageBytes(Bitmap bm) { byte[] bytes = null; System.IO.MemoryStream ms = new System.IO.MemoryStream(); bm.Save(ms, ImageFormat.Gif); bytes = ms.ToArray(); ms.Close(); return bytes; } Thomas

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

          Or, with slightly more pleasant syntax, with better resource management:

          public static byte[] GenerateImageBytes(Bitmap bm)
          {
          using(MemoryStream ms = new MemoryStream())
          {
          bm.Save(ms, ImageFormat.Gif);
          return ms.ToArray();
          }
          }

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: Cops & Robbers Judah Himango

          T 1 Reply Last reply
          0
          • J Judah Gabriel Himango

            Or, with slightly more pleasant syntax, with better resource management:

            public static byte[] GenerateImageBytes(Bitmap bm)
            {
            using(MemoryStream ms = new MemoryStream())
            {
            bm.Save(ms, ImageFormat.Gif);
            return ms.ToArray();
            }
            }

            Tech, life, family, faith: Give me a visit. I'm currently blogging about: Cops & Robbers Judah Himango

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

            Yes, this looks greate :) But, will the MemoryStream be closed (ms.Close())?

            J 1 Reply Last reply
            0
            • T thomasa

              Yes, this looks greate :) But, will the MemoryStream be closed (ms.Close())?

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

              That's what the using statement is for. The using clause automatically disposes of any IDisposable objects, including MemoryStream, thus it will call the appropriate close method when the scope leaves the using clause.

              Tech, life, family, faith: Give me a visit. I'm currently blogging about: Cops & Robbers Judah Himango

              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