Bytetobmp and BMP tobyte (image)

Source: Internet
Author: User
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;
}
}
}

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.