使用指针访问位图 [英] Accessing a bitmap with pointers

查看:85
本文介绍了使用指针访问位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有以下问题.我想构建一个实时处理相机图像的应用程序.问题是我无法正确获取位图数据.

下面的代码正在运行,没有错误,但是问题是图像没有改变.代码如下:

Hi all,

I have a following problem. I want to build a application processing images from a camera in real time. The problem is that I cannot get to the bitmap data correctly.

The following piece of code is running without errors but the problem is that the images don''t change. The code looks as follows:

BitmapData bData = m_Imgs[i].LockBits(new Rectangle(0, 0, Width, Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
int[] imageData = new int[m_Imgs[i].Width * m_Imgs[i].Height];
                                
Marshal.Copy(bData.Scan0, imageData, 0, imageData.Length);
for (int x = 0; x < imageData.Length; x++)
    {
    imageData[x] = 255 - imageData[x]; 
    }

   Marshal.Copy(imageData, 0, bData.Scan0, imageData.Length);
   // Unlock it kill it resize it!
    m_Imgs[i].UnlockBits(bData);
    m_Imgs[i].Dispose();
    m_Imgs[i] = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
   m_PicBoxes[i].Image = m_Imgs[i];



此代码应进行图像反转.



This code should do image inversion. Can you give me some instructions where to look for an error?

推荐答案

如我所见,您可以操纵位图,然后完全忘记结果,将其替换为位图的全新实例(带new Bitmap(&hellip)的代码行).

此外,名称"m_PicBoxes"表明您使用的是类System.Windows.Forms.PictureBox,这不是一个很好的标志.这堂课经常被滥用;在此类上做一些动态的事情通常是没有意义的.该课程只会吃掉额外的资源和开发时间,没有任何回报.通常,最好使用重写的方法OnPaint在控件上立即渲染位图(或其他图形).

—SA
As I can see, you manipulate the bitmap and then completely forget the result, replacing it with a brand new instance of a bitmap (a line of code with new Bitmap(&hellip)).

Besides, the name "m_PicBoxes" suggests that you use the class System.Windows.Forms.PictureBox, which is not a very good sign; this class is often abused; doing something dynamic with this class usually makes no sense. This class only eats up additional resources and development time, giving nothing in return. It''s usually much better to render bitmaps (or any other graphics) immediately on a control in the overridden method OnPaint.

—SA


这篇关于使用指针访问位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆