問題描述
我一直在尋找將位圖轉換為 8bpp 的最快方法.我找到了兩種方法:
I was looking for the fastest way to convert a Bitmap to 8bpp. I found 2 ways:
1.
public static System.Drawing.Image ConvertTo8bpp(Bitmap oldbmp)
{
using (var ms = new MemoryStream())
{
oldbmp.Save(ms, ImageFormat.Gif);
ms.Position = 0;
return System.Drawing.Image.FromStream(ms);
}
}
2. http://www.wischik.com/lu/programmer/1bpp.html
但是:1. 結果質量非常低(托盤不良)
But: 1. Results in a very low quality result (bad pallet)
和 2 給了我一個負步幅的位圖,當我嘗試鎖定位并將數據復制到字節數組時,我得到一個異常:嘗試讀取或寫入受保護的內存.這通常表明其他內存已損壞.
and 2 gives me a Bitmap with negative stride, when I try to lockbits and copy the data to a byte array I get an exception: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
this.stride = bmpData.Stride;
this.bytesPerPixel = GetBytesPerPixel(bmp.PixelFormat);
int length = bmpData.Stride * bmp.Height;
if (this.stride < 0)
this.data = new byte[-length];
else
this.data = new byte[length];
Marshal.Copy(bmpData.Scan0, data, 0, length);
//Unlock the bitmap
bmp.UnlockBits(bmpData);
我怎樣才能讓 2 取得積極的進展?或者如何使用負步幅的鎖位復制數據?
How can I make 2 gives a positive stride? Or how can I copy data using lockbits of a negative stride??
推薦答案
一次復制 1 行,計算一行的起始指針為 ((byte*)scan0 + (y * stride))代碼>.無論是正步幅還是負步幅,代碼都是相同的.
Copy 1 row at a time, calculating the starting pointer for a row as ((byte*)scan0 + (y * stride))
. The code will be identical for either positive or negative stride.
這篇關于如何從具有負步幅的位圖中復制像素數據?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!