Home >Backend Development >C++ >How to Create a Bitmap from a Byte Array in C#?

How to Create a Bitmap from a Byte Array in C#?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-30 15:57:14676browse

How to Create a Bitmap from a Byte Array in C#?

Creating a Bitmap from Byte Array in C#

Creating a Bitmap image from a byte array is a common task in image processing applications. In C#, you can achieve this using the Bitmap class and the MemoryStream class.

Converting Byte Array to Bitmap

To convert a byte array to a Bitmap, follow these steps:

  1. Create a new MemoryStream object using the byte array as input:
using (var ms = new MemoryStream(imageData))
{
    // Create a Bitmap object using the MemoryStream
    Bitmap bmp = new Bitmap(ms);
}
  1. The Bitmap(MemoryStream) constructor overload reads the image data from the MemoryStream and creates a Bitmap object.

Additional Notes

  • Ensure that the byte array indeed contains valid image data, otherwise an ArgumentException will be thrown.
  • The Bitmap(MemoryStream) constructor has limitations on image dimensions; dimensions greater than 65,535 pixels in any direction will cause an ArgumentException.

The above is the detailed content of How to Create a Bitmap from a Byte Array in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn