位图数据中的c#RGB值

编程入门 行业动态 更新时间:2024-10-28 15:35:06
本文介绍了位图数据中的c#RGB值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是位图的新手,每个像素使用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是要跳转到图像下一行要跳过的字节数,并且由于 not 始终等于"width *每像素字节数",因此应始终为考虑在内.

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值

本文发布于:2023-07-30 16:24:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1251019.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位图   数据   RGB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!