There are many ways to process byte bitmap and bitmap to byte.
However, I don't know why bitmap converted bytes cannot be read by image. fromstream but can be read by bitmap. fromstream.
Using system;
Using system. Collections. Generic;
Using system. collections;
Using system. text;
Using system. drawing;
Using system. Drawing. imaging;
Using system. IO;
Namespace sicnco. Utility
{
Public class superimage
{
Public superimage ()
{
}
Private Static bitmap converttorgb (image porgimage)
{
Bitmap vorgbmp = new Bitmap (porgimage );
// If (vorgbmp. width> vorgbmp. Height)
//{
// Vorgbmp. rotateflip (rotatefliptype. rotate90flipnone );
//}
Bitmap vtemp = new Bitmap (480,640, pixelformat. format24bpprgb );
// Vtemp. setresolution (600f, 600f );
Graphics vgrap = graphics. fromimage (vtemp );
Vgrap. drawimage (vorgbmp, new rectangle (0, 0,480,640), 0, 0, vorgbmp. Width, vorgbmp. Height, graphicsunit. pixel );
Vgrap. Save ();
Return vtemp;
}
Public static bitmap converttobmp (byte [] pdatas)
{
Bitmap BMP = new Bitmap (480,640 );
Bitmapdata bdata = BMP. lockbits (New rectangle (new point (), BMP. Size ),
Imagelockmode. writeonly,
Pixelformat. format24bpprgb );
// Copy the bytes to the bitmap object
System. runtime. interopservices. Marshal. Copy (pdatas, 0, bdata. scan0, pdatas. Length );
BMP. unlockbits (bdata );
Return BMP;
}
// Public static bitmap converttobmp (byte [] pdatas)
//{
// Memorystream vstream = new memorystream (pdatas );
// Bitmap vbmp = bitmap. fromstream (vstream );
// Vstream. Close ();
// Vstream = NULL;
// Return vbmp;
//}
Public static byte [] converttobytes (image porgimage)
{
Bitmap vbmp = converttorgb (porgimage );
Bitmapdata vbmpdata = vbmp. lockbits (New rectangle (0, 0, vbmp. Width, vbmp. Height), imagelockmode. readonly, pixelformat. format24bpprgb );
Intptr vptr = vbmpdata. scan0;
Int vbytes = vbmpdata. stride * vbmp. height;
Byte [] vdatas = new byte [vbytes];
System. runtime. interopservices. Marshal. Copy (vptr, vdatas, 0, vbytes );
Vbmp. unlockbits (vbmpdata );
Return vdatas;
}
// Public static byte [] converttobytes (image porgimage)
//{
// Bitmap vbmp = converttorgb (porgimage );
// Memorystream vstream = new memorystream ();
// Vbmp. Save (vstream, imageformat. JPEG );
// Byte [] vbytes = vstream. getbuffer ();
// Vbmp. Dispose ();
// Vstream. Close ();
// Vstream = NULL;
// Return vbytes;
//}
Private Static imagecodecinfo getencoderinfo (string mimetype)
{
Int J;
Imagecodecinfo [] encoders;
Encoders = imagecodecinfo. getimageencoders ();
For (j = 0; j <encoders. length; ++ J)
{
If (encoders [J]. mimetype = mimetype)
Return encoders [J];
}
Return NULL;
}
}
}