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