問(wèn)題描述
我一直在尋找將位圖轉(zhuǎn)換為 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. 結(jié)果質(zhì)量非常低(托盤(pán)不良)
But: 1. Results in a very low quality result (bad pallet)
和 2 給了我一個(gè)負(fù)步幅的位圖,當(dāng)我嘗試鎖定位并將數(shù)據(jù)復(fù)制到字節(jié)數(shù)組時(shí),我得到一個(gè)異常:嘗試讀取或?qū)懭胧鼙Wo(hù)的內(nèi)存.這通常表明其他內(nèi)存已損壞.
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 取得積極的進(jìn)展?或者如何使用負(fù)步幅的鎖位復(fù)制數(shù)據(jù)?
How can I make 2 gives a positive stride? Or how can I copy data using lockbits of a negative stride??
推薦答案
一次復(fù)制 1 行,計(jì)算一行的起始指針為 ((byte*)scan0 + (y * stride))代碼>.無(wú)論是正步幅還是負(fù)步幅,代碼都是相同的.
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.
這篇關(guān)于如何從具有負(fù)步幅的位圖中復(fù)制像素?cái)?shù)據(jù)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!