問題描述
我是位圖的新手,每像素使用 16 位Format16bppRgb555
;
I am new in working with Bitmap and using 16 bits per pixel Format16bppRgb555
;
我想從位圖數(shù)據(jù)中提取 RGB 值.這是我的代碼
I want to Extract RGB Values from Bitmap Data. Here is my code
static void Main(string[] args)
{
BitmapRGBValues();
Console.ReadKey();
}
static unsafe void BitmapRGBValues()
{
Bitmap cur = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format16bppRgb555);
//Capture Screen
var screenBounds = Screen.PrimaryScreen.Bounds;
using (var gfxScreenshot = Graphics.FromImage(cur))
{
gfxScreenshot.CopyFromScreen(screenBounds.X, screenBounds.Y, 0, 0, screenBounds.Size, CopyPixelOperation.SourceCopy);
}
var curBitmapData = cur.LockBits(new Rectangle(0, 0, cur.Width, cur.Height),
ImageLockMode.ReadWrite, PixelFormat.Format16bppRgb555);
try
{
byte* scan0 = (byte*)curBitmapData.Scan0.ToPointer();
for (int y = 0; y < cur.Height; ++y)
{
ulong* curRow = (ulong*)(scan0 + curBitmapData.Stride * y);
for (int x = 0; x < curBitmapData.Stride / 8; ++x)
{
ulong pixel = curRow[x];
//How to get RGB Values from pixel;
}
}
}
catch
{
}
finally
{
cur.UnlockBits(curBitmapData);
}
}
推薦答案
LockBits 實(shí)際上可以轉(zhuǎn)換您的圖像到所需的像素格式,這意味著不需要進(jìn)一步轉(zhuǎn)換.只需將圖像鎖定為 Format32bppArgb
,您就可以簡單地從單個字節(jié)中獲取顏色值.
LockBits can actually convert your image to a desired pixel format, meaning no further conversion should be needed. Just lock the image as Format32bppArgb
and you can simply take your colour values from single bytes.
BitmapData curBitmapData = cur.LockBits(new Rectangle(0, 0, cur.Width, cur.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
Int32 stride = curBitmapData.Stride;
Byte[] data = new Byte[stride * cur.Height];
Marshal.Copy(curBitmapData.Scan0, data, 0, data.Length);
cur.UnlockBits(curBitmapData);
使用此代碼,您最終會得到字節(jié)數(shù)組 data
,其中填充了 ARGB 格式的圖像數(shù)據(jù),這意味著顏色分量字節(jié)將按 [B, G, R, A].請注意,stride
是跳轉(zhuǎn)到圖像下一行的字節(jié)數(shù),因?yàn)檫@不總是等于width * bytes per pixel",應(yīng)始終予以考慮.
With this code, you end up with the byte array data
, which is filled with your image data in ARGB format, meaning the colour component bytes will be in there in the order [B, G, R, A]. Note that the stride
is the amount of bytes to skip to get to a next line on the image, and since this is not always equal to "width * bytes per pixel", it should always be taken into account.
現(xiàn)在你明白了,你可以用它做任何你想做的事......
Now you got that, you can do whatever you want with it...
Int32 curRowOffs = 0;
for (Int32 y = 0; y < cur.Height; y++)
{
// Set offset to start of current row
Int32 curOffs = curRowOffs;
for (Int32 x = 0; x < cur.Width; x++)
{
// ARGB = bytes [B,G,R,A]
Byte b = data[curOffs];
Byte g = data[curOffs + 1];
Byte r = data[curOffs + 2];
Byte a = data[curOffs + 3];
Color col = Color.FromArgb(a, r, g, b);
// Do whatever you want with your colour here
// ...
// Increase offset to next colour
curOffs += 4;
}
// Increase row offset
curRowOffs += stride;
}
您甚至可以編輯字節(jié),然后根據(jù)需要根據(jù)它們構(gòu)建新圖像.
You can even edit the bytes, and then build a new image from them if you want.
這篇關(guān)于c# 位圖數(shù)據(jù)中的 RGB 值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!