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 ByteArray Conversion...?

Bitmap to ByteArray Conversion...?

Scheduled Pinned Locked Moved C#
csharpgraphicsperformancehelpquestion
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.
  • D Offline
    D Offline
    DeepOceans
    wrote on last edited by
    #1

    hi plz tell me the way to convert the Bitmap object to ByteArray in C#? I don't want to use the Memory stream as it causes problem with my System memory. reply me ASAP Regardz

    Shanzay

    L G A L 4 Replies Last reply
    0
    • D DeepOceans

      hi plz tell me the way to convert the Bitmap object to ByteArray in C#? I don't want to use the Memory stream as it causes problem with my System memory. reply me ASAP Regardz

      Shanzay

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      try: bitmap.Save("picture.bmp"); byte[] data = File.ReadAllBytes("picture.bmp");

      1 Reply Last reply
      0
      • D DeepOceans

        hi plz tell me the way to convert the Bitmap object to ByteArray in C#? I don't want to use the Memory stream as it causes problem with my System memory. reply me ASAP Regardz

        Shanzay

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        DeepOceans wrote:

        plz tell me the way to convert the Bitmap object to ByteArray in C#?

        Do you want the object serialised, the raw image data, or compressed as an image format?

        DeepOceans wrote:

        I don't want to use the Memory stream as it causes problem with my System memory.

        What kind of problem?

        Despite everything, the person most likely to be fooling you next is yourself.

        1 Reply Last reply
        0
        • D DeepOceans

          hi plz tell me the way to convert the Bitmap object to ByteArray in C#? I don't want to use the Memory stream as it causes problem with my System memory. reply me ASAP Regardz

          Shanzay

          A Offline
          A Offline
          Anthony Mushrow
          wrote on last edited by
          #4

          You should find out what problem using a MemoryStream is causing, because there shouldn't be any. And i thought that first reply was a good way to get it into a byte array without using a stream in memory (which would probably be the best solution) Apart from that you could write the data yourself, reading the colour of each pixel and then writing this data into an array - of course you'd need to check the bit-depth of the image and make sure you write the data correctly, plus you'd probably want to write the bitmap header as well... it's a fair bit of effort.

          My current favourite word is: I'm starting to run out of fav. words!

          -SK Genius

          Game Programming articles start -here[^]-

          1 Reply Last reply
          0
          • D DeepOceans

            hi plz tell me the way to convert the Bitmap object to ByteArray in C#? I don't want to use the Memory stream as it causes problem with my System memory. reply me ASAP Regardz

            Shanzay

            L Offline
            L Offline
            Lutoslaw
            wrote on last edited by
            #5

            Use a LockBits method. You should get something like this:

            unsafe static byte[] getBytes(BitmapData data)
            {
            byte* firstByte = (byte*)data.Scan0.ToPointer();
            int size = data.Stride * data.Height;
            byte[] bytes = new byte[size];
            for (int i = 0; i < size; i++)
            bytes[i] = firstByte[i];
            return bytes;
            }
            unsafe static void setBytes(BitmapData data, byte[] bytes)
            {
            byte* firstByte = (byte*)data.Scan0.ToPointer();
            int size = data.Stride * data.Height;
            for (int i = 0; i < size; i++)
            firstByte[i] = bytes[i];
            }
            static void Main()
            {
            using (Bitmap bmp = (Bitmap)Bitmap.FromFile(@"test.bmp")) {
            BitmapData data = bmp.LockBits(new Rectangle(new Point(0), bmp.Size),
            ImageLockMode.ReadOnly, bmp.PixelFormat);
            byte[] bytes = getBytes(data);
            bmp.UnlockBits(data);
            using (Bitmap copy = new Bitmap(data.Width, data.Height, bmp.PixelFormat)) {
            data = copy.LockBits(new Rectangle(new Point(0), copy.Size),
            ImageLockMode.WriteOnly, copy.PixelFormat);
            setBytes(data, bytes);
            copy.UnlockBits(data);
            copy.Save(@"test - copy.bmp");
            }
            }
            }

            Compile with /unsafe. This code copies a BMP file. Insted getBytes and setBytes you may want to use System.Runtime.InteropServices.Marshal.Copy method, but it has many function we don't need here so it's better to stick to a simplier version.

            Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

            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