Several ways to convert from an image to a byte[] array

Source: Internet
Author: User
The highest performance, its array and pixel one by one correspond to the public static void Test1 (Image img) {Bitmap bmp = new Bitmap (IMG); BitmapData BitmapData = bmp. LockBits (New Rectangle (0, 0), IMG.
            Size), Imagelockmode.readwrite, Pixelformat.format24bpprgb);
            byte[] Bgrvalues = new byte[bitmapdata.stride * Bitmapdata.height];
            IntPtr Ptr = bitmapdata.scan0;
            System.Runtime.InteropServices.Marshal.Copy (Ptr, bgrvalues, 0, bgrvalues.length); Bmp.
        Unlockbits (BitmapData);  //low performance, less array content, unknown public static void Test2 (Image img) {System.Drawing.ImageConverter IC =
            New System.Drawing.ImageConverter ();
            byte[] BtImage1 = new Byte[0]; BtImage1 = (byte[]) IC.
        ConvertTo (IMG, Btimage1.gettype ()); //low performance, array content is picture format content, format unknown public static void Test3 (Image img) {System.IO.MemoryStream ms = N
            EW System.IO.MemoryStream (); Img.
    Save (ms,imageformat.bmp);        byte[] Byteimage = new Byte[0]; Byteimage = Ms.
        ToArray (); }

Let's talk about their characteristics.

Test1 and Test3 performance is very close, test2 performance is significantly lower than the first 2, it should be all memory operation, of course, fast.

Test3 in a single test all the time than test1 to be a little faster, really a little bit, but in doing 5,000 tests, Test3 obviously opened the distance;

Then you can see that test3 on a certain statement, but it may not be an array operation, compared in memory, I personally think that the ImageFormat encoding time consuming, of course, you use imageformat.png obtained byte[] length significantly shortened,

But time has also increased significantly.

Test2, as always, is noticeably slower.

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.