I created a C# application that receive a RGB stream in input, then compress the stream and save it on the disk. For my test I'm running an application that acts as server(on the same pc) which streams out the video.
The core of my client application is:
void client_ColorFrameReady(object sender, ColorFrameReadyEventArgs e)
{
if (writer != null)
{
count++;
if (count % 2 == 0)
writer.WriteVideoFrame(ResizeBitmap(BitmapImage2Bitmap(e.ColorFrame.BitmapImage), 320, 240));
}
else
{
writer.Close();
}
}
}
the type of writer
is VideoFileWriter
from aforge.net
library.
My application works well, but I have a problem related to the amount of RAM used.
When my application is acquiring frames the RAM used from the server application (the application that stream out the video) increases linearly with time. The RAM is freed only when writer.Close()
is executed.
The methods BitmapImage2Bitmap
and ResieBitmap are shown below:
private Bitmap BitmapImage2Bitmap(BitmapImage bitmapImage)
{
// BitmapImage bitmapImage = new BitmapImage(new Uri("../Images/test.png", UriKind.Relative));
using (MemoryStream outStream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapImage));
enc.Save(outStream);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);
// return bitmap; <-- leads to problems, stream is closed/closing ...
return new Bitmap(bitmap);
}
}
private static Bitmap ResizeBitmap(Bitmap sourceBMP, int width, int height)
{
Bitmap result = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(result))
g.DrawImage(sourceBMP, 0, 0, width, height);
return result;
}
Can I limit the usage of RAM in my application?