-1

好吧,我想知道是否有人编写代码以在表单上显示位图。
是否可以像 c# 中的数组一样直接访问它?

我想知道这是因为位图已经存在于内存和屏幕上。
但我不确定如何指出它;喜欢指针或数组的位置,我想在 c# 中执行此操作,而且我也不知道表单上的数组数据结构(比如它是 RGB BGR / RGB24 等)

请注意,此图像源不是来自文件,而是来自网络摄像头。
另外,我想这样做的一个原因是因为 getpixel / putpixel 会减慢我想要的速度。

4

3 回答 3

1

如果要获取数组中的图像像素,可以这样做:

            Bitmap image = new Bitmap("somebitmap.png");
            Rectangle area = new Rectangle(0,0,image.Width, image.Height);
            BitmapData bitmapData = image.LockBits(area, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            int stride = bitmapData.Stride;
            IntPtr ptr = bitmapData.Scan0;
            int numBytes = bitmapData.Stride * image.Height;
            byte[] rgbValues = new byte[numBytes];
            Marshal.Copy(ptr, rgbValues, 0, numBytes);

当您更改它们时,您应该使用以下方法将该数组复制回位图:

            Marshal.Copy(rgbValues, 0, bitmapData.Scan0, bitmapData.Stride * image.Height);
            image.UnlockBits(bitmapData);

But if you realy want to deal with pixels like you said, you should use some FastBitmap implementation. There are many other implementations available also.

于 2012-10-25T22:52:47.633 回答
0

在 C# 中访问位图当然是可能的——甚至很容易——就像一个数组一样。使用位图类:

Bitmap bmp = new Bitmap(640, 480);
Color pixel = bmp.GetPixel(x, y);    // "array like" access
bmp.SetPixel(x,y, Color.FromArgb(red,green,blue);

在您的情况下,诀窍是将位图放入 Bitmap 类。如果它已经在表单上,​​您可以创建一个“图形上下文”并从中检索它:

Graphics g = this.CreateGraphics();
Bitmap bmp = new Bitmap(640, 480, g);

或从控件:

Graphics g = this.MyControl.CreateGraphics();
Bitmap bmp = new Bitmap(640, 480, g);

您无需担心它的内部 RGB(A) 格式,只需处理“颜色”类型并根据需要设置/获取它的 R/G/B 组件。

于 2012-10-25T22:42:19.533 回答
0

位图中的像素不通过数组暴露,即 mybitmap[x,y]。

您可以使用 GetPixel(x,y) 方法访问特定像素的颜色值,或使用 SetPixel(x,y) 更改值。

于 2012-10-25T22:43:53.953 回答