Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
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:

C#
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?
Posted

1 solution

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
 
Share this answer
 
Comments
charles henington 17-Jul-12 0:31am    
yet another 5 my friend very true on the issue of resources. I prefer to use a simple Panel with the OnPaint event
Sergey Alexandrovich Kryukov 17-Jul-12 0:39am    
Thank you, Charles.
That works with Panel, but in most cases using Control as a base class is the best, as there is no behavior of more derived classes which is needed. (If you use OnPaint, it means you create a derived class anyway as this method is protected; by handling the event Paint you can do the same without subclassing.)
--SA
bikekowal 17-Jul-12 2:41am    
Using System.Windows.Forms.PictureBox is connected with using picturebox for displaying an image (or it isn't?). What to use instead of PicBoxes?
Sergey Alexandrovich Kryukov 17-Jul-12 12:06pm    
I don't know what are "PicBoxes". The class PictureBox is good only if you need to so something very simple -- a static image. When you try to do anything dynamic, interactive, animated, etc, it becomes an obstacle instead of help. As I say, you can use just Control for rendering, and it will be faster, simpler, less development.
--SA
bikekowal 17-Jul-12 1:45am    
Thanks for a reply. I am a C# beginner, I don't know many things, thus any help is very valuable.

So as I understard you suggest me to add a following line:

return bData

instead of new Bitmap. Is that correct?

At the moment I am trying to access the pixels and do simple processing, the further development will be a building a new application from the scratch.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900