Preface
In C# programming, the mutual conversion between Bitmap type and Byte[] type is a common requirement in image processing and data transmission. The Bitmap type represents a bitmap image, while the Byte[] type is a byte array that can be used to store image data. Sometimes we need to convert the Bitmap type to Byte[] type for data transfer or storage, and sometimes we need to convert the Byte[] type to Bitmap type to display the image on the client side. This article will explain in detail how to convert between the two types.
1. Bitmap type to Byte[] type
Save method using Bitmap class
The Save method of the Bitmap class can save images to a file and read the file contents into the Byte[] array. Here is an example:
using System; using ; using ; namespace BitmapToByteArray { class Program { static void Main(string[] args) { // Create a Bitmap object Bitmap bitmap = new Bitmap(""); // Save Bitmap object to file using (FileStream fileStream = new FileStream("", )) { (fileStream, ); } // Read file content to Byte[] array byte[] bytes = (""); // Output Byte[] array length ("Byte[] array length:" + ); // Free up resources (); } } }
Use the GetBytes method of Bitmap class
The Bitmap class does not have a direct way to convert itself to Byte[], but we can use the GetBytes method of the Bitmap class to get the pixel data of the image and then convert it to a Byte[] array. Here is an example:
using System; using ; using ; namespace BitmapToByteArray { class Program { static void Main(string[] args) { // Create a Bitmap object Bitmap bitmap = new Bitmap(""); // Get pixel data of the image BitmapData bitmapData = (new Rectangle(0, 0, , ), , ); // Calculate the size of Byte[] array int bytesCount = * ; byte[] bytes = new byte[bytesCount]; // Copy pixel data to Byte[] array (bitmapData.Scan0, bytes, 0, bytesCount); // Free up resources (bitmapData); // Output Byte[] array length ("Byte[] array length:" + ); } } }
2. Byte[] type to Bitmap type
Convert Byte[] array to Bitmap object using MemoryStream
With MemoryStream we can reconstruct the Byte[] array into a Bitmap object. Here is an example:
using System; using ; using ; namespace ByteArrayToBitmap { class Program { static void Main(string[] args) { // Read Byte[] array byte[] bytes = (""); // Convert Byte[] array to Bitmap object using MemoryStream using (MemoryStream memoryStream = new MemoryStream(bytes)) { Bitmap bitmap = new Bitmap(memoryStream); // Output information of Bitmap object ("Image Width:" + ); ("Image Height:" + ); // Free up resources (); } } } }
Use class
The class provides a way to create a Bitmap object from a Byte[] array. Here is an example:
using System; using ; using ; namespace ByteArrayToBitmap { class Program { static void Main(string[] args) { { // Read Byte[] array byte[] bytes = (""); // Create Bitmap object using class Bitmap bitmap = new Bitmap(bytes); // Output information of Bitmap object ("Image Width:" + ); ("Image Height:" + ); // Free up resources (); } } }
In this code, we first read the contents of a JPEG image file named "" and store it in the Byte[] array. Then, we use the Bitmap class constructor, passing the Byte[] array as a parameter, creating a new Bitmap object. Finally, we output the width and height information of the newly created Bitmap object and free up the resource.
Note: In the example above, we used methods to read the file contents. If you need to work with image files in other formats, you may need to use different methods to read the file contents, such as using classes.
Summarize
In C#, the mutual conversion between the Bitmap type and Byte[] type can be achieved by using the Save method, the GetBytes method, the MemoryStream, and the BitmapImage class. These methods can meet common needs in image processing, such as saving images to files, reading image content from files, or converting image data into Byte[] arrays in network transmission.
The above is a detailed explanation of the examples of C# implementing the mutual conversion of Bitmap type and Byte[] type. For more information about the mutual conversion of C# Bitmap and Byte[], please pay attention to my other related articles!