AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

PictureBox Crash

The forum is to discuss topics related to different areas of image processing and computer vision.

PictureBox Crash

Postby mbonamusa » Mon Feb 23, 2009 5:19 pm

Hello,
I'm working with C# and Aforge. (I'm not informatic, I'm hardware electronic). I capture and image from my own usb camera with my own dll that lets image captured on a int array. I copy this array to a bitmap and then I process it with Aforge. Finally I paint the bitmap using a picturebox. All is ok. But when I open a new form and I move it over the picturebox, after few seconds the image disappear and appears a error image and a error message. What's the problem?
My code is:

//When I click a button begins a Thread showing images captured
private void cmdcaptura_Click(object sender, EventArgs e)
{
bstop = true;
txFastFers = new Thread(loopcapture);
txFastFers.IsBackground = true;
txFastFers.Priority = ThreadPriority.Highest;
txFastFers.Start();
}

private unsafe void loopcapture()
{
Boolean ok = false;
int[] imatge = new int[ampla * alt * 3]; //[width * heigth * 3]
Bitmap returnFastMap1 = new Bitmap(ampla, alt, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

for (; bstop; )
{
frames++;
ok = MC1API.captura(imatge); //Capture a frame from my usb cam

if (ok)
{
System.Drawing.Imaging.BitmapData bitmapFastData1 = returnFastMap1.LockBits(new Rectangle(0, 0, ampla, alt), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
byte* imageFastPointer1 = (byte*)bitmapFastData1.Scan0;

int ArrayFastPointer2 = 0;
for (int y = 0; y < alt; y++) // rows
{
for (int x = 0; x < ampla; x++) // columns
{
imageFastPointer1[ArrayFastPointer2 + 0] = (byte)imatge[ArrayFastPointer2 + 0]; //
imageFastPointer1[ArrayFastPointer2 + 1] = (byte)imatge[ArrayFastPointer2 + 1]; //
imageFastPointer1[ArrayFastPointer2 + 2] = (byte)imatge[ArrayFastPointer2 + 2]; //
ArrayFastPointer2 = ArrayFastPointer2 + 3;
}
}
returnFastMap1.UnlockBits(bitmapFastData1);

// Grayscale
AForge.Imaging.Filters.Grayscale filter = new AForge.Imaging.Filters.Grayscale(0.2125, 0.7154, 0.0721);
Bitmap grayImage = filter.Apply(returnFastMap1);

//Paint on a PictureBox
Picture1.Image = grayImage;
Picture1.Refresh();

successes++;
}
else failures++;

if (bstop == true && cblive.Checked == true)
{
bstop = true;
}

else
{
bstop = false;
}
}
}

//While program is capturing and showing frames I open the other Form with a new Thread.

Thank you!
mbonamusa
 
Posts: 12
Joined: Fri Feb 20, 2009 1:43 pm

Re: PictureBox Crash

Postby mbonamusa » Tue Feb 24, 2009 5:00 pm

Hello again,
I attach an screen capture of the error when I move a second form over Form1 picturebox. I've tried to debug and the problem seems to be related to lock-unlock bits of the image painted in picturebox.

Thank you.
Attachments
picturebox_captured_error2.jpg
Capture im
picturebox_captured_error2.jpg (81.45 KiB) Viewed 12904 times
mbonamusa
 
Posts: 12
Joined: Fri Feb 20, 2009 1:43 pm

Re: PictureBox Crash

Postby andrew.kirillov » Tue Feb 24, 2009 7:06 pm

Hello,

The code itself does not look to be suspicious. At least it is not easy to spot there an issue with locks/unlocks ... There may be an issue with picture box and locks/unlocks when the picture box is refreshing and updating its content, but at the same moment the image is processed in another thread and is locked at that moment. But in your case it does not seem to be so, since you always create a new image and then give it picture box.

If you could provide more details about exception name, message, etc., these could be helpful. Please, in English if possible. Spanish is not my language :?
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3434
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: PictureBox Crash

Postby mbonamusa » Wed Feb 25, 2009 11:17 am

Hello,
Sorry, but I have spanish on my PC and my english is not good, so I've found anotger way to show you my problem:
I'm searching for internet forums and I've found one forum post with the same problem (I think). I attach you the forum post link, but I can't understand the solution proposed, that seems to be good. Can you help me with the solution and how to implement in my code?
(I am working with C# and forum post is about VB.NET... but it seems very similar)
Post link:
http://www.vbdotnetforums.com/graphics- ... ocked.html

Thank you!!!
Regards.
mbonamusa
 
Posts: 12
Joined: Fri Feb 20, 2009 1:43 pm

Re: PictureBox Crash

Postby andrew.kirillov » Wed Feb 25, 2009 5:32 pm

Hello,

Well, that forum post says that you just need to clone your image before giving it to picture box. Instead of doing this:
mbonamusa wrote:Picture1.Image = grayImage;

you may need to do this:
Code: Select all
Picture1.Image = (Bitmap) grayImage.Clone();

This will make sure that picture box has its own copy of image, which is not affected by somebody else.

This approach is OK in some cases and even may be required. It all depends on what you do in your other threads. You may try it ...

From the code you gave, I would not say it is required. But ... I believe it is not the whole code :)
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3434
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: PictureBox Crash

Postby mbonamusa » Thu Feb 26, 2009 10:26 am

Thank you Andrew! It works fine!

That's not all my code, but it's the only part of code dedicated to capture, process and paint images, and always that any windows form is moved over picturebox, program crash or picturebox image disapear, and always I move my form leaving picturebox out of screen bounds then picturebox image disapear.

Now, with image clone, all works fine.

If you want I can send you my entire code... I'm not informatic and perhaps I've made some programming errors.

Thank you!
Regards!
mbonamusa
 
Posts: 12
Joined: Fri Feb 20, 2009 1:43 pm




Return to Image Processing and Computer Vision