Click here to Skip to main content
Click here to Skip to main content

Simple Magnifier

By , 1 Apr 2007
 

Introduction

This is a fun little gadget for your desktop: a simple magnifier.

Background

I believe, many of you have already seen at least one magnifier application. I tried a few including the one which is built-in at the OS, however, I didn't like them one way or another. So, I decided to implement a simple one for my own usage.

Well, I've been using it quite sometime by now and it really works great. I thought you may find it interesting as well.

A Few Words About the Code

As I mentioned earlier, the program in fact is a simple application. However, it still demonstrates a few interesting points: First of all, it shows how to capture a screen image, double buffering, moving a Windows form programmatically, and serializing/deserializing configuration information through XmlSerializer. Second of all, the little application is designed to be a fun application in my mind, so it doesn't follow conventional Windows programming steps. Instead, I've used a very small borderless window as the main form. It has only 3 buttons (actually, hot spots) to do all functionality it provides. The first one instantiates a magnifier form, the second one to do the configuration, and finally, the third one is to exit from application.

Here is a screenshot of the application:

Screenshot - magSample1.jpg

The configuration section provides quite a few things to play with:

Screenshot - magConfig1.jpg

Some of the code snippets are also shown below:

// Make the window (the form) elliptical
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(ClientRectangle);
Region = new Region(gp);
 
//--- Double Buffering --- 

protected override void OnPaintBackground(PaintEventArgs e)
{
    if (mConfiguration.DoubleBuffered)
    {
        // Do not paint background (required for double buffering)!
    }
    else
    {
        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)
    {
        Rectangle dest = new Rectangle(0, 0, Width, Height);
        int w = (int)(Width / mConfiguration.ZoomFactor);
        int h = (int)(Height / 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);
    }

    if (mImageMagnifier != null)
    {
        g.DrawImage(mImageMagnifier, 0, 0, Width, Height);
    }

    if (mConfiguration.DoubleBuffered)
    {
        e.Graphics.DrawImage(mBufferImage, 0, 0, Width, Height);
    }      
} 
//--- XML Serialization --- 
public class XmlUtility
{
    public static void Serialize(Object data, string fileName)
    {
        Type type = data.GetType();
        XmlSerializer xs = new XmlSerializer(type);
        XmlTextWriter xmlWriter = new XmlTextWriter(fileName, System.Text.Encoding.UTF8);
        xmlWriter.Formatting = Formatting.Indented;
        xs.Serialize(xmlWriter, data);
        xmlWriter.Close();
    }

    public static Object Deserialize(Type type, string fileName)
    {
        XmlSerializer xs = new XmlSerializer(type);

        XmlTextReader xmlReader = new XmlTextReader(fileName);
        Object data = xs.Deserialize(xmlReader);

        xmlReader.Close();

        return data;
    }        
} 

Please take a look at the provided code to see the details in the implementation.

Conclusion

It was really fun to code this application. I shared it with my friends and many really liked it. Hope you guys will like it as much as I did.

Have a nice day!

History

  • 2nd April, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

otigli
Software Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionAbout Multimonitormembermagefesa8 Oct '12 - 22:26 
First of all, thanks for ypur piece of code, really amazing tool.
 
I have one problem. I'm trying to run the app on a secondary monitor, and i 've changed this line :
 
(VB)
 //'mScreenImage = New Bitmap(Screen.PrimaryScreen.Bounds.Width, //Screen.PrimaryScreen.Bounds.Height)
mScreenImage = New Bitmap(Screen.AllScreens(1).Bounds.Width, Screen.AllScreens(1).Bounds.Height)
 
But does not works.... Please could you help me ? ? ?
QuestionFrom Circle to Squarememberh.jaza2 Jan '10 - 7:03 
Hi otigli
Thanks for your great app.
How could I change your magnifier lens from circle shape to an square?
GeneralC++memberSuper Garrison30 Sep '09 - 7:19 
Nice job!
Do you have it in C++?
 
Nowlex
QuestionSuggestionmemberAmin856 Feb '09 - 3:20 
Thanks for your nice job!
Do you have any idea to let it work in real-time and dynamically?!
GeneralsuggestionmemberRakesh Muraharishetty23 Apr '07 - 12:08 
Dear Prof,
I have been searching some of the files in codeproject and found these to be interesting and can be added to your project. I wish to do this myself also when time permits. http://www.codeproject.com/csharp/ie_advanced_toolbar.asp[^]
GeneralRe: suggestionmemberRakesh Muraharishetty23 Apr '07 - 12:11 
I was also searching to make this a windows vista gadget, but found that this can be made only through html files. If your simple magnifier can be made as a vista gadget then that would be really wonderful as there is no magnifier in the vista gadgets till date.
GeneralRe: suggestionmemberotigli23 Apr '07 - 21:06 
Hi Rakesh,
 
Thanks for the suggestions. I'll take a look at them. How are things going? Are you in San Diego now?
Have a nice day...
 
Ogun TIGLI

GeneralReally coolmemberYury Goltsman11 Apr '07 - 1:07 
Excelent tool for reading. I'll recommend it to my father Wink | ;) Thanks.
 
As addition for GUI programmers I can propose non-anti-alias mode, pixel grid and cursor point marker. And as really great addition - measurement unit. For example pressing Ctrl marks start point and when you move lens, hint shows x and y position relative to start point (and may be color).
GeneralRe: Really coolmemberotigli11 Apr '07 - 9:12 
Yury,
Thanks for the comments and the good ideas. When I find sometime I may implement them.
Have a nice day.
 
Ogun TIGLI

GeneralGood job!!!memberaporra10 Apr '07 - 1:31 
Simply, good job!!! Thank you very much to share it.
 
Albert Porrà
BELGEN Bolsa SL

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 2 Apr 2007
Article Copyright 2007 by otigli
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid