Converting a bitmap to a byte array 位元影像檔案與Byte[]轉換

來源:互聯網
上載者:User

發布一個位元影像檔案與Byte流間轉換的方法集。

方法1 從記憶體中直接擷取位元影像的Byte[]

// import this 
// using System.Runtime.InteropServices;
private unsafe byte[] BmpToBytes_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[] bmpBytes = new byte[byteCount];

    // Copy the locked bytes from memory
    Marshal.Copy (bData.Scan0, bmpBytes, 0, byteCount);

    // don't forget to unlock the bitmap!!
    bmp.UnlockBits (bData);

    return bmpBytes;
}

還原方法

private unsafe Bitmap BytesToBmp (byte[] bmpBytes, 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 (bmpBytes, 0, bData.Scan0, bmpBytes.Length);
    bmp.UnlockBits(bData);
    return bmp;
}

方法2  通過將位元影像檔案寫入記憶體流的方式擷取byte[]

// Bitmap bytes have to be created via a direct memory copy of the bitmap
private byte[] BmpToBytes_MemStream (Bitmap bmp)
...{
    MemoryStream ms = new MemoryStream();
    // Save to memory using the Jpeg format
    bmp.Save (ms, ImageFormat.Jpeg);
    
    // read to end
    byte[] bmpBytes = ms.GetBuffer();
    bmp.Dispose();
    ms.Close();

    return bmpBytes;
}

//Bitmap bytes have to be created using Image.Save()
private Image BytesToImg (byte[] bmpBytes)
...{
    MemoryStream ms = new MemoryStream(bmpBytes);
    Image img = Image.FromStream(ms);
    // Do NOT close the stream!
    
    return img;
}

方法3 使用序列化的方式獲得位元影像的byte[]

// import these
// using System.Runtime.Serialization;
// using System.Runtime.Serialization.Formatters.Binary;
private byte[] BmpToBytes_Serialization (Bitmap bmp)
...{
    // stream to save the bitmap to
    MemoryStream ms = new MemoryStream();
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize (ms, bmp);

    // read to end
    byte[] bmpBytes = ms.GetBuffer();
    bmp.Dispose();
    ms.Close();

    return bmpBytes;
}
private Bitmap BytesToBmp_Serialized (byte[] bmpBytes)
...{
    BinaryFormatter bf = new BinaryFormatter ();
    // copy the bytes to the memory
    MemoryStream ms = new MemoryStream (bmpBytes);
    return (Bitmap)bf.Deserialize(ms);
}

 

 方法4  擷取位元影像物件控點、

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

獲得控制代碼後,想怎麼操作都可以嘍

 轉自一個不明英文論壇

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.