Rectangle transparent performance improvements

This commit is contained in:
Jaex 2015-04-03 15:25:08 +03:00
parent 11bf1725f7
commit 25413ed0c2
3 changed files with 34 additions and 65 deletions

View file

@ -34,38 +34,18 @@ public class LayeredForm : Form
{
public LayeredForm()
{
InitializeComponent();
SuspendLayout();
AutoScaleDimensions = new SizeF(6F, 13F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(300, 300);
FormBorderStyle = FormBorderStyle.None;
Name = "LayeredForm";
ShowInTaskbar = false;
Text = "LayeredForm";
TopMost = true;
ResumeLayout(false);
}
#region Windows Form Designer generated code
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.SuspendLayout();
this.AutoScaleDimensions = new SizeF(6F, 13F);
this.AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new Size(292, 273);
this.FormBorderStyle = FormBorderStyle.None;
this.Name = "LayeredForm";
this.ShowInTaskbar = false;
this.Text = "LayeredForm";
this.TopMost = true;
this.ResumeLayout(false);
}
#endregion Windows Form Designer generated code
protected override CreateParams CreateParams
{
get
@ -76,12 +56,7 @@ protected override CreateParams CreateParams
}
}
public void SelectBitmap(Bitmap bitmap)
{
SelectBitmap(bitmap, 255);
}
public void SelectBitmap(Bitmap bitmap, int opacity)
public void SelectBitmap(Bitmap bitmap, int opacity = 255)
{
if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
{
@ -100,14 +75,14 @@ public void SelectBitmap(Bitmap bitmap, int opacity)
SIZE newSize = new SIZE(bitmap.Width, bitmap.Height);
POINT sourceLocation = new POINT(0, 0);
POINT newLocation = new POINT(this.Left, this.Top);
POINT newLocation = new POINT(Left, Top);
BLENDFUNCTION blend = new BLENDFUNCTION();
blend.BlendOp = NativeMethods.AC_SRC_OVER;
blend.BlendFlags = 0;
blend.SourceConstantAlpha = (byte)opacity;
blend.AlphaFormat = NativeMethods.AC_SRC_ALPHA;
NativeMethods.UpdateLayeredWindow(this.Handle, screenDc, ref newLocation, ref newSize, memDc, ref sourceLocation, 0, ref blend, NativeMethods.ULW_ALPHA);
NativeMethods.UpdateLayeredWindow(Handle, screenDc, ref newLocation, ref newSize, memDc, ref sourceLocation, 0, ref blend, NativeMethods.ULW_ALPHA);
}
finally
{

View file

@ -26,7 +26,6 @@ You should have received a copy of the GNU General Public License
using ShareX.HelpersLib;
using ShareX.ScreenCaptureLib.Properties;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
@ -59,23 +58,11 @@ public Rectangle SelectionRectangle0Based
}
}
public Point CurrentMousePosition { get; private set; }
public Point CurrentMousePosition0Based
{
get
{
return new Point(CurrentMousePosition.X - ScreenRectangle.X, CurrentMousePosition.Y - ScreenRectangle.Y);
}
}
public bool ShowRectangleInfo { get; set; }
private Timer timer;
private Image backgroundImage;
private TextureBrush backgroundBrush;
private Pen borderDotPen, borderDotPen2;
private Point positionOnClick;
private Point currentPosition, positionOnClick;
private bool isMouseDown;
private Stopwatch penTimer;
@ -204,8 +191,9 @@ public Image GetAreaImage()
private void timer_Tick(object sender, EventArgs e)
{
CurrentMousePosition = CaptureHelpers.GetCursorPosition();
SelectionRectangle = CaptureHelpers.CreateRectangle(positionOnClick.X, positionOnClick.Y, CurrentMousePosition.X, CurrentMousePosition.Y);
currentPosition = CaptureHelpers.GetCursorPosition();
SelectionRectangle = CaptureHelpers.CreateRectangle(positionOnClick.X, positionOnClick.Y, currentPosition.X, currentPosition.Y);
Refresh();
}

View file

@ -58,38 +58,40 @@ public Rectangle SelectionRectangle0Based
}
}
public Point CurrentMousePosition { get; private set; }
private Rectangle PreviousSelectionRectangle { get; set; }
public Point CurrentMousePosition0Based
private Rectangle PreviousSelectionRectangle0Based
{
get
{
return new Point(CurrentMousePosition.X - ScreenRectangle.X, CurrentMousePosition.Y - ScreenRectangle.Y);
return new Rectangle(PreviousSelectionRectangle.X - ScreenRectangle.X, PreviousSelectionRectangle.Y - ScreenRectangle.Y,
PreviousSelectionRectangle.Width, PreviousSelectionRectangle.Height);
}
}
public bool ShowRectangleInfo { get; set; }
private Timer timer;
private Bitmap surface;
private Graphics gSurface;
private Pen borderDotPen, borderDotPen2;
private Point positionOnClick;
private Pen clearPen, borderDotPen, borderDotPen2;
private Point currentPosition, positionOnClick;
private bool isMouseDown;
private Stopwatch penTimer;
public RectangleTransparent()
{
clearPen = new Pen(Color.FromArgb(1, 0, 0, 0));
borderDotPen = new Pen(Color.Black, 1);
borderDotPen2 = new Pen(Color.White, 1);
borderDotPen2.DashPattern = new float[] { 5, 5 };
penTimer = Stopwatch.StartNew();
ScreenRectangle = CaptureHelpers.GetScreenBounds();
surface = new Bitmap(ScreenRectangle.Width, ScreenRectangle.Height);
gSurface = Graphics.FromImage(surface);
gSurface.InterpolationMode = InterpolationMode.NearestNeighbor;
gSurface.SmoothingMode = SmoothingMode.HighSpeed;
gSurface.CompositingMode = CompositingMode.SourceCopy;
gSurface.Clear(Color.FromArgb(1, 0, 0, 0));
StartPosition = FormStartPosition.Manual;
Bounds = ScreenRectangle;
@ -113,6 +115,7 @@ public RectangleTransparent()
protected override void Dispose(bool disposing)
{
if (timer != null) timer.Dispose();
if (clearPen != null) clearPen.Dispose();
if (borderDotPen != null) borderDotPen.Dispose();
if (borderDotPen2 != null) borderDotPen2.Dispose();
if (gSurface != null) gSurface.Dispose();
@ -185,19 +188,22 @@ public Image GetAreaImage()
private void timer_Tick(object sender, EventArgs e)
{
CurrentMousePosition = CaptureHelpers.GetCursorPosition();
SelectionRectangle = CaptureHelpers.CreateRectangle(positionOnClick.X, positionOnClick.Y, CurrentMousePosition.X, CurrentMousePosition.Y);
currentPosition = CaptureHelpers.GetCursorPosition();
PreviousSelectionRectangle = SelectionRectangle;
SelectionRectangle = CaptureHelpers.CreateRectangle(positionOnClick.X, positionOnClick.Y, currentPosition.X, currentPosition.Y);
RefreshSurface();
}
private void RefreshSurface()
{
gSurface.Clear(Color.FromArgb(1, 0, 0, 0));
// Clear previous rectangle selection
gSurface.DrawRectangleProper(clearPen, PreviousSelectionRectangle0Based);
if (isMouseDown)
{
gSurface.DrawRectangleProper(borderDotPen, SelectionRectangle0Based);
borderDotPen2.DashOffset = (int)(penTimer.Elapsed.TotalMilliseconds / 100) % 10;
gSurface.DrawRectangleProper(borderDotPen, SelectionRectangle0Based);
gSurface.DrawRectangleProper(borderDotPen2, SelectionRectangle0Based);
}