Wednesday, 11 September 2013

C# out of memory exception in wpf application

C# out of memory exception in wpf application

I am creating an application where I am getting live images from camera
and trying to put that on image control of wpf. But after some time it
will start throwing out of memory exception.
here is the code
try
{
imgControl.Dispatcher.Invoke(DispatcherPriority.Normal,
(Action)(() =>
{
using (MemoryStream memory = new MemoryStream())
{
lastImage.Save(memory, ImageFormat.Png);
memory.Position = 0;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
ImageSource imageSource = bitmapImage;
imgControl.Source = imageSource;
}
}));
}
catch (Exception ex)
{
//exception handling
}
is there a way I can reduce memory consumption and find a work around for
this out of memory exception?
Thanks

No comments:

Post a Comment