Converting a bitmap to a byte array bitmap file and byte [] Conversion

Source: Internet
Author: User

Releases a method set for converting bitmap files and byte streams.

Method 1 obtain the byte of the bitmap from the memory []

// Import this
// Using system. runtime. interopservices;
Private unsafe byte [] BMP tobytes_unsafe (bitmap BMP)
...{
Bitmapdata bdata = BMP. lockbits (New rectangle (new point (), BMP. Size ),
Imagelockmode. readonly,
Pixelformat. format24bpprgb );
// Number of bytes in the bitmap
Int bytecount = bdata. stride * BMP. height;
Byte [] BMP bytes = new byte [bytecount];

// Copy the locked bytes from memory
Marshal. Copy (bdata. scan0, BMP bytes, 0, bytecount );

// Don't forget to unlock the bitmap !!
BMP. unlockbits (bdata );

Return BMP bytes;
}

Restoration Method

Private unsafe bitmap bytestobmp (byte [] BMP bytes, size imagesize)
...{
Bitmap BMP = new Bitmap (imagesize. Width, imagesize. Height );

Bitmapdata bdata = BMP. lockbits (New rectangle (new point (), BMP. Size ),
Imagelockmode. writeonly,
Pixelformat. format24bpprgb );

// Copy the bytes to the bitmap object
Marshal. Copy (BMP bytes, 0, bdata. scan0, BMP bytes. Length );
BMP. unlockbits (bdata );
Return BMP;
}

Method 2 get byte by writing bitmap files to the memory stream []

// Bitmap bytes have to be created via a direct memory copy of the bitmap
Private byte [] BMP tobytes_memstream (bitmap BMP)
...{
Memorystream MS = new memorystream ();
// Save to memory using the JPEG format
BMP. Save (MS, imageformat. JPEG );

// Read to end
Byte [] BMP bytes = Ms. getbuffer ();
BMP. Dispose ();
Ms. Close ();

Return BMP bytes;
}

// Bitmap bytes have to be created using image. Save ()
Private image bytestoimg (byte [] BMP bytes)
...{
Memorystream MS = new memorystream (BMP bytes );
Image IMG = image. fromstream (MS );
// Do not close the stream!

Return IMG;
}

Method 3 obtain the byte of A Bitmap Using serialization []

// Import these
// Using system. runtime. serialization;
// Using system. runtime. serialization. formatters. Binary;
Private byte [] BMP tobytes_serialization (bitmap BMP)
...{
// Stream to save the bitmap
Memorystream MS = new memorystream ();
Binaryformatter BF = new binaryformatter ();
BF. serialize (MS, BMP );

// Read to end
Byte [] BMP bytes = Ms. getbuffer ();
BMP. Dispose ();
Ms. Close ();

Return BMP bytes;
}
Private bitmap bytestobmp_serialized (byte [] BMP bytes)
...{
Binaryformatter BF = new binaryformatter ();
// Copy the bytes to the memory
Memorystream MS = new memorystream (BMP bytes );
Return (Bitmap) BF. deserialize (MS );
}

 

Method 4 obtain the bitmap object handle,

Private intptr getimagehandle (image IMG)
...{
Fieldinfo Fi = typeof (image). getfield ("nativeimage", bindingflags. nonpublic | bindingflags. instance );
If (FI = NULL)
Return intptr. zero;

Return (intptr) Fi. getvalue (IMG );
}

After obtaining the handle, you can do anything you want.

Transferred from an unknown English Forum

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.