Improvements to async effect processing

This commit is contained in:
Jaex 2017-11-17 17:57:15 +03:00
parent 7df6f6c4f5
commit 7f90c61a9d
3 changed files with 14 additions and 30 deletions

View file

@ -62,7 +62,7 @@ public void WriteElapsedSeconds(string text = null)
public void WriteElapsedMilliseconds(string text = null)
{
Write(text, timer.ElapsedMilliseconds + " milliseconds.");
Write(text, timer.Elapsed.TotalMilliseconds + " milliseconds.");
}
public void Dispose()

View file

@ -35,7 +35,6 @@ public abstract class BaseEffectShape : BaseShape
public abstract string OverlayText { get; }
private Image canvasCopy;
private bool isEffectCaching;
private Image cachedEffect;
@ -131,32 +130,27 @@ private void CacheEffect()
{
if (!isEffectCaching)
{
isEffectCaching = true;
ClearCache(true);
if (canvasCopy == null)
if (IsInsideCanvas)
{
canvasCopy = (Image)Manager.Form.Canvas.Clone();
}
isEffectCaching = true;
TaskEx.Run(() =>
{
ClearCache();
cachedEffect = Manager.CropImage(RectangleInsideCanvas);
if (IsInsideCanvas)
TaskEx.Run(() =>
{
cachedEffect = Manager.CropImage(canvasCopy, RectangleInsideCanvas);
ApplyEffect((Bitmap)cachedEffect);
}
isEffectCaching = false;
});
isEffectCaching = false;
});
}
}
}
private void ClearCache()
private void ClearCache(bool forceClear = false)
{
if (cachedEffect != null)
if ((forceClear || !isEffectCaching) && cachedEffect != null)
{
cachedEffect.Dispose();
cachedEffect = null;
@ -166,11 +160,6 @@ private void ClearCache()
public override void Dispose()
{
ClearCache();
if (canvasCopy != null)
{
canvasCopy.Dispose();
}
}
}
}

View file

@ -1332,11 +1332,6 @@ public void CropArea(Rectangle rect)
}
public Image CropImage(Rectangle rect, bool onlyIfSizeDifferent = false)
{
return CropImage(Form.Canvas, rect, onlyIfSizeDifferent);
}
public Image CropImage(Image img, Rectangle rect, bool onlyIfSizeDifferent = false)
{
rect = CaptureHelpers.ScreenToClient(rect);
@ -1345,11 +1340,11 @@ public Image CropImage(Image img, Rectangle rect, bool onlyIfSizeDifferent = fal
rect.X -= offset.X;
rect.Y -= offset.Y;
rect.Intersect(new Rectangle(0, 0, img.Width, img.Height));
rect.Intersect(new Rectangle(0, 0, Form.Canvas.Width, Form.Canvas.Height));
if (rect.IsValid() && (!onlyIfSizeDifferent || rect.Size != img.Size))
if (rect.IsValid() && (!onlyIfSizeDifferent || rect.Size != Form.Canvas.Size))
{
return ImageHelpers.CropImage(img, rect);
return ImageHelpers.CropBitmap((Bitmap)Form.Canvas, rect);
}
return null;