Hi Utkarsh,
I made a little app that mimics the magnifier behaviour you talked about,
with an image on the left and a magnified version on the right. I loaded a
3MB file and it seems to take about 50% of the CPU on my home computer when I
move the mouse very rapidly, but with no flickering.
I have 3 files:
1. ZoomSelectorPic tureBox.cs -> displays an image with a red rectangle
around the mouse indicating where the zoomed in portion of the image will be
displayed. This inherits from PictureBox (although it doesn't really need to)
2. ZoomMagnifierPi ctureBox which shows a zoomed in version of the selected
region in ZoomSelectorPic tureBox, this also inherits from picturebox
3. Form1.cs -> creates instances of the above classes. you will need to
modify the Form1_Load method to load an image on your computer.
I have pasted all three files below, but the formatting will be messed up in
these windows. If you like you can download the entire source from
http://www.markdawson.org/software/csharp/magnifier.zip which may be easier
than getting the text code out from below.
//FORM1.cs
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
namespace WindowsApplicat ion4
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
//
// TODO: Add any constructor code after InitializeCompo nent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(800, 469);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHan dler(this.Form1 _Load);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void Form1_Load(obje ct sender, System.EventArg s e)
{
ZoomSelectorPic tureBox selectorPicture Box = new ZoomSelectorPic tureBox();
selectorPicture Box.Image = Image.FromFile( @"c:\test.bmp") ;
this.Controls.A dd(selectorPict ureBox);
selectorPicture Box.Left = 0;
selectorPicture Box.Top = 0;
selectorPicture Box.Width = 400;
selectorPicture Box.Height = 400;
ZoomMagnifierPi ctureBox zoomMagnifier = new
ZoomMagnifierPi ctureBox(select orPictureBox);
this.Controls.A dd(zoomMagnifie r);
zoomMagnifier.L eft = 400;
zoomMagnifier.T op = 0;
zoomMagnifier.W idth = 400;
zoomMagnifier.H eight = 400;
}
}
}
//ZoomMagnifierPi ctureBox.cs
using System;
using System.Windows. Forms;
using System.Drawing;
namespace WindowsApplicat ion4
{
public class ZoomMagnifierPi ctureBox : PictureBox
{
private Rectangle _zoomRegion;
private Image _originalImage;
public ZoomMagnifierPi ctureBox(ZoomSe lectorPictureBo x zoomSelector) : base()
{
_originalImage = zoomSelector.Im age;
zoomSelector.Zo omRectangleChan ged += new
WindowsApplicat ion4.ZoomSelect orPictureBox.Zo omRectangleLoca tionChangedEven tHandler(zoomSe lector_ZoomRect angleChanged);
}
protected override void OnPaintBackgrou nd(PaintEventAr gs pevent)
{
//base.OnPaintBac kground (pevent);
}
protected override void OnPaint(PaintEv entArgs e)
{
//base.OnPaint (e);
e.Graphics.Draw Image(_original Image, new Rectangle(0,0, this.Width,
this.Height), _zoomRegion.X, _zoomRegion.Y, _zoomRegion.Wid th,
_zoomRegion.Hei ght, GraphicsUnit.Pi xel);
}
private void zoomSelector_Zo omRectangleChan ged(Rectangle zoomRectangle)
{
_zoomRegion = zoomRectangle;
this.Refresh();
}
}
}
//ZoomSelectorPic tureBox.cs
using System;
using System.Windows. Forms;
using System.Drawing;
namespace WindowsApplicat ion4
{
public class ZoomSelectorPic tureBox : PictureBox
{
private Rectangle _zoomRegion;
private bool _mouseIsOver = false;
public delegate void ZoomRectangleLo cationChangedEv entHandler(Rect angle
zoomRectangle);
public event ZoomRectangleLo cationChangedEv entHandler ZoomRectangleCh anged;
public ZoomSelectorPic tureBox() : base()
{
//create zoom region rectangle
_zoomRegion = new Rectangle(0, 0, 50, 50);
//set up event handlers
this.MouseMove += new MouseEventHandl er(ZoomSelector PictureBox_Mous eMove);
this.MouseLeave += new EventHandler(Zo omSelectorPictu reBox_MouseLeav e);
this.MouseEnter += new EventHandler(Zo omSelectorPictu reBox_MouseEnte r);
}
protected override void OnPaintBackgrou nd(PaintEventAr gs pevent)
{
//base.OnPaintBac kground (pevent);
}
protected override void OnPaint(PaintEv entArgs e)
{
base.OnPaint (e);
//only do this if the mouse is over the control
if(_mouseIsOver )
{
using(Pen p = new Pen(Color.Red, 5))
{
e.Graphics.Draw Rectangle(p, _zoomRegion);
}
}
}
private void ZoomSelectorPic tureBox_MouseMo ve(object sender,
MouseEventArgs e)
{
//make the mouse in the center of the zoom region
_zoomRegion.X = e.X - _zoomRegion.Wid th / 2;
_zoomRegion.Y = e.Y - _zoomRegion.Wid th / 2;
//raise the event to listeners to refresh their
//zoomed view
if(ZoomRectangl eChanged != null)
{
ZoomRectangleCh anged(_zoomRegi on);
}
//force image to be redrawn
this.Refresh();
}
private void ZoomSelectorPic tureBox_MouseLe ave(object sender, EventArgs e)
{
_mouseIsOver = false;
//force image to redraw without the rectangle
this.Refresh();
}
private void ZoomSelectorPic tureBox_MouseEn ter(object sender, EventArgs e)
{
_mouseIsOver = true;
}
}
}
Hope that helps
Mark R Dawson
http://www.markdawson.org
"utkarsh" wrote:
Thanks Mark for your code and so much help!! :)
But my problem is little bit different.
I have two screen left and right. In left panel I have loaded a Image
in picture box. When user move the mouse on the left panel, a
maginified view of a image section, near by the cursor, should be
visible in the right panel.
I have to do the similar work like the windows magnifier ( Programs ->
Accessories -> Accessibility -> Magnifier)
So I have loaded the same image in both the screens and I have to
update the right screen as mouse move in left. I have removed the
flickering by using the timer tick event. But only problem left is,
timer is consuming the 100% CPU.
Thanks again,
Utkarsh Panwar