Reputation: 7344
I have asked a similar question before and the answer I used for that does not work in this sligtly different scenario.
I have a winform desktop c# app.
Inside a timer (interval 100ms) I convert a bitmap to an array of bytes.
I have a stream of 10 bitmaps per scecond
These bytes are then uploaded to the server.
There are no issues with the server handling this.
On the client side I inovke the [web service] using async mehthods.
I am checking a condition that the [web service] cannot be invoked again until the previouse call has completed.
The frame/image is 'skipped' if the server does not respnd in time.
This is my client code:
public void Init()
{
_live.StreamerCompleted += new wsLive.StreamerCompletedEventHandler(_live_StreamerCompleted);
}
private void _live_StreamerCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
_uploaded = true;
}
private static bool _uploaded = true;
private static wsLive.Live _live = new wsLive.Live();
private static byte[] imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
imageIn.Dispose();
return ms.ToArray();
}
public static void AddFrame(Image<Bgr, Byte> _newFrame)
{
if (_newFrame != null && _uploaded)
{
byte[] _data = GetDeflated(_newFrame.Copy());
_live.StreamerAsync(Shared.Alias, _data, Guid.NewGuid().ToString());
_newFrame.Dispose();
}
}
public static void AddFrame(Bitmap _newFrame)
{
byte[] _data = imageToByteArray(_newFrame);
_live.StreamerAsync(Shared.Alias, _data, Guid.NewGuid().ToString());
_newFrame.Dispose();
}
//declared at form load
Init();
//inside my timer...
if (Uploaded)
{
LiveStreaming.AddFrame((Bitmap)_currentFrame.Clone());
}
_currentFrame.dispose();
But the memory climbs. Am I doing this right?
thanks
Upvotes: 0
Views: 136
Reputation: 1531
One issue that jumps out is that you aren't disposing of the MemoryStream in imageToByteArray.
Try this:
private static byte[] imageToByteArray(Image imageIn)
{
using (MemoryStream ms = new MemoryStream())
{
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
imageIn.Dispose();
return ms.ToArray();
}
}
Upvotes: 2