Convert an image into a byte array

Source: Internet
Author: User

The previous article explains how to convert the obtained data stream into a Bitmap image)

In turn, how can we achieve this?

This principle is relatively simple. It also uses memory serialization to directly serialize the image to the memory, and deserializes the stream in the memory into a byte array.

In combination with the previous article, we can easily convert data streams and images.

The Code is as follows:

Using System;
Using System. IO;

Public byte [] ToByte (Image imageData)
MemoryStream MS = new MemoryStream ();
Image. Save (MS, System.Drawing.Imaging.ImageFormat.bmp); // serialize image data to memory
Byte [] imgByte = new byte [Ms. Length];
Ms. Position = 0;
Ms. Read (imgByte, 0, Convert. ToInt32 (Ms. Length); // reverse sequence, stored in the byte array
Ms. Close ();

Return imgByte; // here we get the byte array of the image.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.