/// <summary>
/// Thread method that encodes the list of frames.
/// </summary>
private void DoWork()
{
int countList = _listBitmap.Count;
var processing = new Processing();
this.Invoke((Action) delegate //Needed because it's a cross thread call.
{
//Control ctrlParent = panelTransparent;
//Processing processing = new Processing();
panelTransparent.Controls.Add(processing);
processing.Dock = DockStyle.Fill;
processing.SetMaximumValue(countList);
processing.SetStatus(1);
});
if (Settings.Default.STencodingCustom) // if NGif encoding
{
#region Ngif encoding
int numImage = 0;
using (_encoder = new AnimatedGifEncoder())
{
_encoder.Start(_outputpath);
_encoder.SetQuality(Settings.Default.STquality);
_encoder.SetRepeat(Settings.Default.STloop ? (Settings.Default.STrepeatForever ? 0 : Settings.Default.STrepeatCount) : -1); // 0 = Always, -1 once
try
{
foreach (var image in _listBitmap)
{
numImage++;
this.BeginInvoke((Action)(() => processing.SetStatus(numImage)));
_encoder.SetFrameRate(Convert.ToInt32(numMaxFps.Value));
_encoder.AddFrame(image);
}
}
catch (Exception ex)
{
LogWriter.Log(ex, "Ngif encoding.");
}
}
#endregion
}
else //if paint.NET encoding
{
#region paint.NET encoding
//var imageArray = _listBitmap.ToArray();
var delay = 1000 / Convert.ToInt32(numMaxFps.Value);
var repeat = (Settings.Default.STloop ? (Settings.Default.STrepeatForever ? 0 : Settings.Default.STrepeatCount) : -1); // 0 = Always, -1 once
using (var stream = new MemoryStream())
{
using (var encoderNet = new GifEncoder(stream, null, null, repeat))
{
for (int i = 0; i < _listBitmap.Count; i++)
{
encoderNet.AddFrame((_listBitmap[i]).CopyImage(), 0, 0, TimeSpan.FromMilliseconds(delay));
this.Invoke((Action)(() => processing.SetStatus(i)));
}
}
stream.Position = 0;
using (
var fileStream = new FileStream(_outputpath, FileMode.Create, FileAccess.Write, FileShare.None,
Constants.BufferSize, false))
{
stream.WriteTo(fileStream);
}
}
#endregion
}
#region Memory Clearing
//TODO: Clean the list of delay.
listFramesPrivate.Clear();
listFramesUndo.Clear();
listFramesUndoAll.Clear();
listFramesPrivate = null;
listFramesUndo = null;
listFramesUndoAll = null;
_encoder = null;
GC.Collect(); //call the garbage colector to empty the memory
#endregion
#region Finish
try
{
this.Invoke((Action) delegate //must use invoke because it's a cross thread call
{
_caller.Text = Resources.Title_EncodingDone;
_stage = (int)Stage.Stoped;
panelTransparent.Controls.Clear(); //Clears the processing page.
processing.Dispose();
_caller.Invalidate();
btnRecordPause.Text = Resources.btnRecordPause_Record;
btnRecordPause.Image = Properties.Resources.Record;
flowPanel.Enabled = true;
//_caller.TopMost = false;
_caller.TopMost = false;
numMaxFps.Enabled = true;
tbHeight.Enabled = true;
tbWidth.Enabled = true;
_caller.MaximizeBox = true;
_caller.MinimizeBox = true;
_actHook.KeyDown += KeyHookTarget; //Set again the keyboard hook method
_actHook.Start(false, true); //start again the keyboard hook watcher
});
}
catch (Exception ex)
{
LogWriter.Log(ex, "Invoke error.");
}
#endregion
}