Converts the obtained data stream into a Bitmap)

Source: Internet
Author: User

The principle is to serialize the obtained inverted data stream to the memory, and thenProcessed, Just deserialize the data from the memory.

The difficulty is thatHow to Implement Processing. Because Bitmap has a proprietary format, we often call this formatData Header. The processing process is to combine the data header with the data stream we obtained earlier. (That is, add this header to the front of the data stream we obtained earlier)

So what is this header? It is a data with a fixed length (14 bytes. For details, see the code. Since this header is common to any Bitmap object, the process of adding the header is basically the same. The Code is as follows:

1 using System;
2 using System. Collections. Generic;
3 using System. Linq;
4 using System. Text;
5 using System. Drawing;
6 using System. IO;
7
8 public Bitmap AddHeader (byte [] imageDataDetails)
9 {
10 Bitmap bitmap = null;
11 int length = imageDataDetails. GetLength (0 );
12 using (MemoryStream stream = new MemoryStream (length + 14) // free up 14 length spaces for the header
13 {
14 byte [] buffer = new byte [13];
15 buffer [0] = 0x42; // Bitmap fixed constant
16 buffer [1] = 0x4d; // Bitmap fixed constant
17 stream. Write (buffer, 0, 2); // first Write the first two bytes of the header
18
19 // convert the length of the previously obtained data stream to bytes,
20 // This is used to tell the "Header" how big the actual image data is.
21 byte [] bytes = bitconverter. getbytes (length );
22 stream. Write (bytes, 0, 4); // write this length to the header
23 buffer [0] = 0;
24 buffer [1] = 0;
25 buffer [2] = 0;
26 buffer [3] = 0;
27 stream. Write (buffer, 0, 4); // Write data of four bytes to the header
28 int num2 = 0x36; // Bitmap fixed constant
29 bytes = BitConverter. GetBytes (num2 );
30 stream. Write (bytes, 0, 4); // The length of the last 4 bytes in the Write
31 stream. getbuffer ();
32 stream. Write (imagedatadetails, 0, length); // append all the actual image data to the end of the header.
33 bitmap = new Bitmap (Stream); // uses the memory stream to construct a bitmap image.
34 bitmap. rotateflip (rotatefliptype. rotate180flipx );
35 stream. Close ();
36 return bitmap; // Finally, we get the image we want.

37}

38}

How can this problem be solved?

Next

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: info-contact@alibabacloud.com 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.