Я начал изучение C#, пытаюсь вывести изображение с веб камеры на форму. Если не ресайзить кадр то все нормально, но если сделать ресайз, то память моментально заполняется. Подскажите где ошибка.
using AForge.Imaging.Filters;
using AForge.Video;
using AForge.Video.DirectShow;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Things
{
public partial class Things : Form
{
private readonly FilterInfoCollection VideoCaptureDevices;
private VideoCaptureDevice FinalVideoDevice;
private Bitmap currentFrame;
public Things()
{
InitializeComponent();
VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
try
{
foreach (FilterInfo VidCapDev in VideoCaptureDevices)
{
comboBox1.Items.Add(VidCapDev.Name);
comboBox1.SelectedIndex = 0;
}
}
catch
{
MessageBox.Show("No camera found. Please connect your camera and click RESET.");
}
}
private void FinalVideoDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
currentFrame = (Bitmap)eventArgs.Frame.Clone();
ResizeFrame(in currentFrame, out currentFrame);
if (web_cam_box.Image != null)
{
web_cam_box.Image.Dispose();
}
web_cam_box.Image = currentFrame;
}
private void ResizeFrame(in Bitmap image, out Bitmap resized)
{
ResizeBicubic filterResize = new ResizeBicubic(640, 480);
resized = filterResize.Apply(image);
}
private void Things_FormClosing(object sender, FormClosingEventArgs e)
{
if (FinalVideoDevice != null)
{
FinalVideoDevice.Stop();
}
}
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (FinalVideoDevice != null)
{
if (FinalVideoDevice.IsRunning)
{
FinalVideoDevice.Stop();
}
}
try
{
FinalVideoDevice = new VideoCaptureDevice(VideoCaptureDevices[comboBox1.SelectedIndex].MonikerString);
FinalVideoDevice.NewFrame += new NewFrameEventHandler(FinalVideoDevice_NewFrame);
FinalVideoDevice.Start();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
}
}
Если закомментировать вызов функции ResizeFrame, то все нормально.
resized = filterResize.Apply(image);
вызватьGC.Collect();
то все опять нормально работает, но наверное это не правильно?