我是位图的新手,每像素使用 16 位Format16bppRgb555;
I am new in working with Bitmap and using 16 bits per pixel Format16bppRgb555;
我想从位图数据中提取 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 实际上可以转换您的图像到所需的像素格式,这意味着不需要进一步转换.只需将图像锁定为 Format32bppArgb,您就可以简单地从单个字节中获取颜色值.
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);
使用此代码,您最终会得到字节数组 data,其中填充了 ARGB 格式的图像数据,这意味着颜色分量字节将按 [B, G, R, A].请注意,stride 是跳转到图像下一行的字节数,因为这不总是等于width * bytes per pixel",应始终予以考虑.
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.
现在你明白了,你可以用它做任何你想做的事......
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;
}
您甚至可以编辑字节,然后根据需要根据它们构建新图像.
You can even edit the bytes, and then build a new image from them if you want.
这篇关于c# 位图数据中的 RGB 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
右键单击 Silverlight 4 应用程序中的列表框Right-click on a Listbox in a Silverlight 4 app(右键单击 Silverlight 4 应用程序中的列表框)
WPF c# webbrowser 在顶部菜单上滚动WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在顶部菜单上滚动)
C# 控制台应用程序 - 如何制作交互式菜单?C# Console app - How do I make an interactive menu?(C# 控制台应用程序 - 如何制作交互式菜单?)
如何避免在 .NET Windows Forms 中创建重复的表单?How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中创建重复的表单?)
UI自动化控制桌面应用程序并单击菜单条UI Automation Control Desktop Application and Click on Menu Strip(UI自动化控制桌面应用程序并单击菜单条)
删除菜单项周围的细边框Removing thin border around the menuitems(删除菜单项周围的细边框)