Out of memory error
Using
AForge.Video.DirectShow
to integrate a QR Code scanner in my WPF App but whenever the scanner is open it crashes in 15 seconds due to out of memory error as it reaches 4gb memory usage and I'm not able to exactly pinpoint the reason why its happening, BitmapToImageSource()
throws the out of memory error.
https://pastebin.com/XjsE9BuJ10 Replies
I think if you use Dispose() function,can solve memory error problem.
try it!
i tried bitmap.Dispose() and it didn't make a difference
sorry. it is my mistake
Severity Code Description Project File Line Suppression State
Error CS0120 An object reference is required for the non-static field, method, or property 'NewFrameEventArgs.Frame' C:\Users\rusha\source\repos\QRCodeApp\QRCodeApp\Scanner.xaml.cs 46
Can you share your project?
or use Documentation.
https://pastebin.com/XjsE9BuJ that's the entire scanner page
another possibility is use "using":
code :
private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
try
{
using (Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone())
{
Application.Current.Dispatcher.Invoke(() =>
{
cameraImage.Source = BitmapToImageSource(bitmap);
});
BarcodeReader barcodeReader = new BarcodeReader(); Result result = barcodeReader.Decode(bitmap);
if (result != null) { string downloadsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string initialDirectory = Path.Combine(downloadsPath, "QR Codes");
if (!Directory.Exists(initialDirectory)) { Directory.CreateDirectory(initialDirectory); }
DateTime currentDateTime = DateTime.Now; string fdt = currentDateTime.ToString("yyyy-MM-dd HH_mm_ss"); string fileName = "QRCode " + fdt + ".png";
fileName = CleanFileName(fileName);
string path = Path.Combine(initialDirectory, fileName); bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Png); videoSource.NewFrame -= VideoSource_NewFrame; videoSource.SignalToStop();
System.Threading.Tasks.Task.Run(() => { Application.Current.Dispatcher.Invoke(() => { cameraImage.Source = null; videoSource = null; videoDevices = null; myframe.frame.Content = new Scanned(bitmap, path, false, false); }); }); } } } catch (Exception ex) { MessageBox.Show($"Error: {ex.Message}", "An Exception occured"); } } try this code. How is about? How is about?
BarcodeReader barcodeReader = new BarcodeReader(); Result result = barcodeReader.Decode(bitmap);
if (result != null) { string downloadsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string initialDirectory = Path.Combine(downloadsPath, "QR Codes");
if (!Directory.Exists(initialDirectory)) { Directory.CreateDirectory(initialDirectory); }
DateTime currentDateTime = DateTime.Now; string fdt = currentDateTime.ToString("yyyy-MM-dd HH_mm_ss"); string fileName = "QRCode " + fdt + ".png";
fileName = CleanFileName(fileName);
string path = Path.Combine(initialDirectory, fileName); bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Png); videoSource.NewFrame -= VideoSource_NewFrame; videoSource.SignalToStop();
System.Threading.Tasks.Task.Run(() => { Application.Current.Dispatcher.Invoke(() => { cameraImage.Source = null; videoSource = null; videoDevices = null; myframe.frame.Content = new Scanned(bitmap, path, false, false); }); }); } } } catch (Exception ex) { MessageBox.Show($"Error: {ex.Message}", "An Exception occured"); } } try this code. How is about? How is about?
im trying it rn
It didn't fix it and the memory usage still increases at the same rate and gives out of memory exception
https://stackoverflow.com/questions/78074037/wpf-out-of-memory-error-in-a-qr-code-scanner
You never dispose of the bitmap you create in
BitmapToImageSource
that you are assigning to cameraImage.Source
yup all that time I wasn't able to figure out how to exactly dispose the bitmap and which one but now i entirely fixed it: https://stackoverflow.com/a/78082665/23190731