I have a small application that takes picture from my webcam. when i run it, it takes almost 1gb of the computer's memory. such a small application shouldn't take that much memory.
Please some once should help me on this.
below is my code
THis is the class that picture from the camera:
using AForge.Imaging.Filters;
using AForge.Video;
using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace PictureCapture
{
public class ImageStore:IDisposable
{
public event EventHandler ImageChanged;
public Image CurrentImage { get; private set; }
public void StoreImage(Bitmap img)
{
if (CurrentImage != null) CurrentImage.Dispose();
CurrentImage = img;
if (ImageChanged!=null)
{
var args = new EventArgs();
ImageChanged(this, args);
}
}
void Disposing(bool disposing)
{
if (disposing)
{
if (CurrentImage != null)
{
ImageChanged -= (s, e)=> { };
CurrentImage.Dispose();
CurrentImage = null;
}
}
}
public void Dispose()
{
Disposing(true);
GC.SuppressFinalize(this);
}
}
class ImageFromCamera
{
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource = null;
public ImageStore Store { get; private set; }
public ImageFromCamera(ImageStore store)
{
Store = store;
}
string _cameraResolution;
string _cameraInfo;
public string CameraInfo
{
get { return _cameraInfo; }
}
public string CameraResolution
{
get { return _cameraResolution; }
}
private bool _started = false;
public void Start()
{
if (_started) return;
getCamList();
}
private void getCamList()
{
try
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count == 0)
{
_cameraResolution = "Нет устройства захвата в системе";
}
else
{
videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
try
{
if (videoSource.VideoCapabilities.Length > 0)
{
string highestSolution = "0;0";
for (int i = 0; i < videoSource.VideoCapabilities.Length; i++)
{
if (videoSource.VideoCapabilities[i].FrameSize.Width > Convert.ToInt32(highestSolution.Split(';')[0]))
highestSolution = videoSource.VideoCapabilities[i].FrameSize.Width.ToString() + ";" + i.ToString();
}
int highResolutionCount = Convert.ToInt32(highestSolution.Split(';')[1]);
videoSource.VideoResolution = videoSource.VideoCapabilities[highResolutionCount];
_cameraResolution = "Camera Resolution:" + videoSource.VideoCapabilities[highResolutionCount].FrameSize.Width.ToString() + "x"+ videoSource.VideoCapabilities[highResolutionCount].FrameSize.Height.ToString();
}
}
catch (Exception ex)
{
throw new ApplicationException("Something went wrong. See inner exception for details", ex);
}
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
Stop();
videoSource.Start();
_cameraInfo = "устройство работает... " + videoSource.FramesReceived.ToString() + " FPS";
}
}
catch (ApplicationException)
{
_cameraResolution = "Нет устройства захвата в системе";
}
}
public void Stop()
{
if (!(videoSource == null))
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource = null;
}
}
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap img = (Bitmap)eventArgs.Frame.Clone();
var filll = new Mirror(false, true);
filll.ApplyInPlace(img);
Store.StoreImage(img);
}
}
}
This is my code to take the picture into the picture boxes
public partial class Form1 : Form
{
string _fileName;
ImageStore _store = new ImageStore();
string _cameraResolution;
public Form1(ImageStore store, string filemane, string cameraResolution)
{
InitializeComponent();
_fileName = filemane;
_store = store;
_cameraResolution = cameraResolution;
label2.Text = cameraResolution;
_store.ImageChanged += (s, e) =>
{
using (Image _image = (s as ImageStore).CurrentImage)
{
Bitmap _cropImage = (Bitmap)_image.Clone();
Bitmap _cropImage2 = (Bitmap)_image.Clone();
Crop filter = new Crop(new Rectangle(Convert.ToInt32(_cropImage.Width / 2.9), _cropImage.Height / 10, (Convert.ToInt32(_cropImage.Width / 3)), (Convert.ToInt32(_cropImage.Height / 1.2))));
Bitmap _getcroppedImage = filter.Apply(_cropImage);
pictureBox1.Image = GetShapesForImage(_cropImage2);
pictureBox2.Image = _getcroppedImage;
}
};
}
private Image GetShapesForImage(Bitmap _img)
{
using (Graphics g1 = Graphics.FromImage(_img))
{
Pen pen1 = new Pen(Color.FromArgb(160, 255, 160), 2);
Pen pen2 = new Pen(Color.Red, 2);
Pen blackPen = new Pen(Color.White, 3);
blackPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
Rectangle rect = new Rectangle(Convert.ToInt32(_img.Width / 3), _img.Height / 10, (Convert.ToInt32(_img.Width / 3)), (Convert.ToInt32(_img.Height / 1.3)));
g1.DrawEllipse(blackPen, rect);
g1.DrawLine(blackPen, _img.Width / 2, 0, _img.Width / 2, _img.Width);
g1.DrawLine(blackPen, _img.Width/3.000f, _img.Height/2.000f, _img.Width/1.500f, _img.Height/2.000f);
return _img;
}
}
What I have tried:
I've tried using() and IDisposable class but it didn't solve the problem.