I have a Magnifier Glass class and it have some problems how can i solve ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
This is the magnifier glass code:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;

namespace Magnifier20070401
{
public partial class MagnifierForm : Form
{
private bool _doMove;

public MagnifierForm()
{
InitializeComponent();

_doMove = true;
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
TopMost = true;
Width = 150;
Height = 150;
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(ClientRectangle);
Region = new Region(gp);
mTimer = new Timer();
mTimer.Enabled = true;
mTimer.Interval = 20;
mTimer.Tick += new EventHandler(HandleTimer);
mScreenImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
mStartPoint = new Point(500, 500);
mTargetPoint = new Point(500, 500);
speed = 0.35F;
zoom = 3.0F;
hidecursor = false;
hue = 30;
}

public MagnifierForm(bool MoveTheGlass, bool InTaskBar, int MagnifierWidth, int MagnifierHeight, Point MagnifierStartPoint, float SpeedFactor, float ChangeZoom, bool HideMouseCursor, float AdjustHue)
{
InitializeComponent();

FormBorderStyle = FormBorderStyle.None;
TopMost = true;
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(ClientRectangle);
Region = new Region(gp);
mTimer = new Timer();
mTimer.Enabled = true;
mTimer.Interval = 20;
mTimer.Tick += new EventHandler(HandleTimer);
mScreenImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);

_doMove = MoveTheGlass;
ShowInTaskbar = InTaskBar;
Width = MagnifierWidth;
Height = MagnifierHeight;
mStartPoint = MagnifierStartPoint;
mTargetPoint = MagnifierStartPoint;
speed = SpeedFactor;
zoom = ChangeZoom;
hidecursor = HideMouseCursor;
hue = AdjustHue;
}

protected override void OnShown(EventArgs e)
{
RepositionAndShow();
}

private delegate void RepositionAndShowDelegate();

private void RepositionAndShow()
{
if (InvokeRequired)
{
Invoke(new RepositionAndShowDelegate(RepositionAndShow));
}
else
{
Graphics g = Graphics.FromImage(mScreenImage);
g.CopyFromScreen(0, 0, 0, 0, new Size(mScreenImage.Width, mScreenImage.Height));
HSLAdjust.BitmapFunctions bf = new HSLAdjust.BitmapFunctions((Bitmap)mScreenImage);
bf.Hue(hue);
bf.Dispose();
g.Dispose();

if (hidecursor == true)
{
Cursor.Hide();
}
else
{
Cursor = Cursors.Cross;
}
Capture = true;
mCurrentPoint = Cursor.Position;
Show(); // to add here the bool of the Mouse Cursor to hide or not to hide
}
}

void HandleTimer(object sender, EventArgs e)
{
float dx = speed * (mTargetPoint.X - mCurrentPoint.X);
float dy = speed * (mTargetPoint.Y - mCurrentPoint.Y);

if (mFirstTime)
{
mFirstTime = false;

mCurrentPoint.X = mTargetPoint.X;
mCurrentPoint.Y = mTargetPoint.Y;

Left = (int)mCurrentPoint.X - Width / 2;
Top = (int)mCurrentPoint.Y - Height / 2;

return;
}

mCurrentPoint.X += dx;
mCurrentPoint.Y += dy;

if (Math.Abs(dx) < 1 && Math.Abs(dy) < 1)
{
mTimer.Enabled = false;
}
else
{
// Update location
Left = (int)mCurrentPoint.X - Width / 2;
Top = (int)mCurrentPoint.Y - Height / 2;
mLastMagnifierPosition = new Point((int)mCurrentPoint.X, (int)mCurrentPoint.Y);
}

Refresh();
}


protected override void OnMouseDown(MouseEventArgs e)
{
mOffset = new Point(Width / 2 - e.X, Height / 2 - e.Y);
mCurrentPoint = PointToScreen(new Point(e.X + mOffset.X, e.Y + mOffset.Y));
mTargetPoint = mCurrentPoint;
mTimer.Enabled = true;

}

protected override void OnMouseMove(MouseEventArgs e)
{
if (_doMove == true)
{
mTargetPoint = PointToScreen(new Point(e.X + mOffset.X, e.Y + mOffset.Y));
mTimer.Enabled = true;
}

}

protected override void OnPaintBackground(PaintEventArgs e)
{
/*if (mConfiguration.DoubleBuffered)
{
// Do not paint background (required for double buffering)!
}
else
{
base.OnPaintBackground(e);
}*/
base.OnPaintBackground(e);
}

protected override void OnPaint(PaintEventArgs e)
{
if (mBufferImage == null)
{
mBufferImage = new Bitmap(Width, Height);
}
Graphics bufferGrf = Graphics.FromImage(mBufferImage);

Graphics g;

/*if (mConfiguration.DoubleBuffered)
{
g = bufferGrf;
}
else
{*/
g = e.Graphics;
//}

if (mScreenImage != null)
{
SolidBrush blueBrush = new SolidBrush(Color.Blue);
Rectangle dest = new Rectangle(0, 0, Width, Height);
int w = (int)(Width / zoom);//mConfiguration.ZoomFactor);
int h = (int)(Height / zoom);//mConfiguration.ZoomFactor);
int x = Left - w / 2 + Width / 2;
int y = Top - h / 2 + Height / 2;

g.DrawImage(
mScreenImage,
dest,
x, y,
w, h,
GraphicsUnit.Pixel);

}
e.Graphics.DrawImage(mBufferImage, 0, 0, Width, Height);
}


//--- Data Members ---
#region Data Members
private bool hidecursor;
private float zoom;
private float hue;
private float speed;
private Timer mTimer;
private Image mBufferImage = null;
private Image mScreenImage = null;
private Point mStartPoint;
private PointF mTargetPoint;
private PointF mCurrentPoint;
private Point mOffset;
private bool mFirstTime = true;
private static Point mLastMagnifierPosition = Cursor.Position;
#endregion


// New code \

protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);

Point pt = Control.MousePosition;
int eX = pt.X - this.Left;
int eY = pt.Y - this.Top;

mOffset = new Point(0, 0);
mCurrentPoint = PointToScreen(new Point(eX + mOffset.X, eY + mOffset.Y));
mTargetPoint = mCurrentPoint;
mTimer.Enabled = true;
this.Capture = true;
}

protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);

if (_doMove)
{
Left = (int)mCurrentPoint.X - Width / 2;
Top = (int)mCurrentPoint.Y - Height / 2;
}
}

protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
if (_doMove == true)
{
_doMove = false;
}
else
{
_doMove = true;
}
}


}
}

First i want to be able to color the magnifier glass circle around the magnifier borders in black or any other color so when i will move the magnifier around i will the borders of it since sometimes if i move it on the same color it looks like: transparent .
I want to paint/color the border round of the magnifier.

Second: If i move the magnifier around stop it then i move to Chrome for exmaple to a site im in then move the magnifier there i see the window on the background behind the chrome. Instead zoom/magnify the curent website in chrome i see the window behind it. Maybe i need to refresh or invalidate it somewhere ?

Third: Sometimes when i activate the magnifier glass on/off many times fast im getting an exception in the magnifier code on this line:
g.DrawImage(
mScreenImage,
dest,
x, y,
w, h,
GraphicsUnit.Pixel);

Bitmap region is already locked
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=Bitmap region is already locked.
Source=System.Drawing
StackTrace:
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttrs, DrawImageAbort callback, IntPtr callbackData)
at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit)
at Magnifier20070401.MagnifierForm.OnPaint(PaintEventArgs e) in d:C-SharpScreenVideoRecorderScreenVideoRecorderWorkingVersionMagnifierForm.cs:line 236
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:



View the full article
 
Back
Top