How to Convert a BMP to a Byte Array

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Convert a BMP to a Byte Array
Use C# to create a byte array. (Hemera Technologies/ Images)

In object-oriented programming, it's almost a necessity to learn how to convert images into byte arrays. This is required when you work with media applications that import and export images from databases. It is recommended to store an image in a database in a byte data type instead of using an image. In Microsoft Visual C#, you can use the "MemoryStream" class together with the "ImageFormat.Gif" property to make the conversion easily.

Skill level:

Things you need

  • Microsoft Visual C# Express

Show MoreHide


  1. 1

    Open Microsoft Visual C# Express and select "New Project..." from the left pane of your screen. Click "Visual C#" under "Installed Templates" and double-click "Windows Forms Application."

  2. 2

    Double-click "PictureBox" to add a picture box control to the form. Add a button using the same technique.

  3. 3

    Right-click the "PictureBox" control and click "Properties." Find the "Image" property under Appearance and click the ellipsis to open the Select Resource dialogue window. Click "Import" and browse for a ".BMP" image. Click "Open" and click "OK."

  4. 4

    Double-click "button1" to create a click event for the button. Add the following namespace to the very first line of your code module:

    using System.IO;

  5. 5

    Copy and paste the following code to the button click event to convert the ".BMP" image in the picture box into a byte array:

    byte[] byteArray = null;

            MemoryStream ms = new MemoryStream();
            pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            byteArray = ms.ToArray();
  6. 6

    Press "F5" to run the program, and click "button1."

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.