Home >Backend Development >C++ >How to Convert Images to Byte Arrays and Back Again in C#?
Conversion between images and byte arrays in C#
Converting images to byte arrays and back is a common operation in many software applications. This conversion is especially useful when dealing with image storage, transfer, and processing.
Convert image to byte array
Convert the image to a byte array, you can follow the steps below:
Code example:
<code class="language-csharp">public byte[] ImageToByteArray(System.Drawing.Image imageIn) { using (var ms = new MemoryStream()) { imageIn.Save(ms, imageIn.RawFormat); return ms.ToArray(); } }</code>
Convert byte array to image
Convert byte array to image, you can follow these steps:
Code example:
<code class="language-csharp">public System.Drawing.Image ByteArrayToImage(byte[] imageByteArray) { using (var ms = new MemoryStream(imageByteArray)) { return System.Drawing.Image.FromStream(ms); } }</code>
These conversion methods are essential for processing images in a variety of applications, such as saving images to databases, transferring images over the network, and performing image processing operations.
The above is the detailed content of How to Convert Images to Byte Arrays and Back Again in C#?. For more information, please follow other related articles on the PHP Chinese website!