從圖像轉換到byte[]數組的幾種方法

http://www.cnblogs.com/keyrratuff/archive/2009/02/11/1388009.html

// 性能最高,其數組和像素一一對應

public static void test1(Image img)

        {

            Bitmap bmp = new Bitmap(img);

            BitmapData bitmapData = bmp.LockBits(new Rectangle(new Point(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);

        }



// 性能較低,數組內容較少,內容未知

        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());

        }



// 性能較低,數組內容為圖片格式內容,格式未知

        public static void test3(Image img)

        {

            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            img.Save(ms,ImageFormat.Bmp);

            byte[] byteImage = new Byte[0];

            byteImage = ms.ToArray();

        }


你可能感兴趣的:(從圖像轉換到byte[]數組的幾種方法)