Good morning,
I'm coding a game which ovject is to capture the feed of a webcam, then apply some filters to detect movement, so far i've managed to get the webcam to work but when i apply the filters it says "Bitmap locked" i've managed to solve that error, right after that said something about PixelFormat overlay must be valid, or something like that, anyways i fixed that one also, but now i'm stuck on this error "System out of memory exception", i clearly have enough memory for my game.
The error appears on:
image = eventArgs.Frame.Clone(Rec, PixelFormat.Format8bppIndexed);
But when i used the breakpoint thing it says the eroor comes from:
for (int i = 0; i < imgData.Length; i++)
{
byteData[i] = (byteData[i] & 0x000000ff) << 16 | (byteData[i] & 0x0000FF00) | (byteData[i] & 0x00FF0000) >> 16 | (byteData[i] & 0xFF000000);
}
Below are 2 of the methods:
private Texture2D GetTexture(GraphicsDevice dev, System.Drawing.Bitmap bmp)
{
int[] imgData = new int[bmp.Width * bmp.Height];
Texture2D texture = new Texture2D(dev, bmp.Width, bmp.Height);
unsafe
{
// lock bitmap
System.Drawing.Imaging.BitmapData origdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
uint* byteData = (uint*)origdata.Scan0;
// Switch bgra -> rgba
for (int i = 0; i < imgData.Length; i++)
{
byteData[i] = (byteData[i] & 0x000000ff) << 16 | (byteData[i] & 0x0000FF00) | (byteData[i] & 0x00FF0000) >> 16 | (byteData[i] & 0xFF000000);
}
// copy data
System.Runtime.InteropServices.Marshal.Copy(origdata.Scan0, imgData, 0, bmp.Width * bmp.Height);
byteData = null;
// unlock bitmap
bmp.UnlockBits(origdata);
}
texture.SetData(imgData);
return texture;
}
private void device_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
image = eventArgs.Frame.Clone(Rec, PixelFormat.Format8bppIndexed);
MoveTowards moveTowardsFilter = new MoveTowards();
Difference differenceFilter = new Difference();
IFilter thresholdFilter = new Threshold(15);
if (backgroundFrame == null)
{
backgroundFrame = processingFilter1.Apply(image);
width = image.Width;
height = image.Height;
return;
}
processingFilter1.Add(new Difference(backgroundFrame));
processingFilter1.Add(new Threshold(15));
processingFilter1.Add(new Opening());
processingFilter1.Add(new Edges());
tmpImage = processingFilter1.Apply(image);
if (++counter == 2)
{
counter = 0;
moveTowardsFilter.OverlayImage = tmpImage;
moveTowardsFilter.ApplyInPlace(backgroundFrame);
}
differenceFilter.OverlayImage = backgroundFrame;
bitmapData = tmpImage.LockBits(new System.Drawing.Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
differenceFilter.ApplyInPlace(bitmapData);
thresholdFilter.Apply(bitmapData);
blobCounter.ProcessImage(bitmapData);
System.Drawing.Rectangle[] rects = blobCounter.GetObjectsRectangles();
Graphics g = Graphics.FromImage(image);
using (Pen pen = new Pen(System.Drawing.Color.Red, 1))
{
foreach (System.Drawing.Rectangle rc in rects)
{
g.DrawRectangle(pen, rc);
if ((rc.Width > 15) && (rc.Height > 15))
{
//Para os retângulos maiores
}
}
}
g.Dispose();
}