Click here to Skip to main content
15,881,881 members
Articles / Programming Languages / C#
Article

A simple Windows Forms image viewer

Rate me:
Please Sign up or sign in to vote.
4.28/5 (24 votes)
31 Jan 20031 min read 136.6K   2.9K   51   12
This is a simple viewer application which allows you to apply basic operation on images such as Rotate, Flip, ROI (Region of interest) zoom and Panning.

Sample Image - LimfROI.jpg
Sample Image - LimfROI2.jpg

Introduction

This is a simple viewer application which allows you to apply basic operation on images such as Rotate, Flip, ROI (Region of interest) zoom and Panning. It offers a sample example for the powerful Matrix class use and the way to have unclassical forms look. A simple windowing method is implemented using middle muse button (available for grayscale image).

Using the code

The interesting code is based on the DisplayPort class which acts as a viewport. A command mechanism is also develop to allow multiple DisplayPort to interact with the user cooperatively. Adding new behaviour such as displaying and designing graphics above the image is easy. See the DisplayLinkRind class which is a manager of simple text overlays.

To add a DisplayPort to a WindowsForm just add a member like this and chain the MouseDown event:

C#
public LimfWnd()
{
    this.ClientSize = new System.Drawing.Size(464, 344);

    this.m_DisplayPort = new DisplayPort();

    InitializeComponent();

    this.m_DisplayPort.MouseDown += new MouseEventHandler(LimfWnd_MouseDown);

            ...

Then add the controls to the form:

C#
private void InitializeComponent()
{
    // 
    // LimfWnd
    // 
    this.AllowDrop = true;
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.BackColor = System.Drawing.SystemColors.WindowText;

    this.ClientSize = new System.Drawing.Size(SystemInformation.WorkingArea.Width/2, 
                                              SystemInformation.WorkingArea.Height/2);

    this.Controls.AddRange(new System.Windows.Forms.Control[] {this.m_DisplayPort,
                                                                          this.splitter1});

            ...

Add image to the DisplayPort:

C#
private void LoadImage(object sender, System.EventArgs e)
{
    FileDialog  Boite = new OpenFileDialog();
    Boite.ShowDialog();

    if(Boite.FileName.Length > 0)
    {
        Image Img = Image.FromFile(Boite.FileName);
        m_DisplayPort.SetImage(Img);
    }
}
            ...

Chain the menu list event:

C#
private void LimfWnd_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if(e.Button == MouseButtons.Right)
    {
        CMenuActionList CommandsLst;

        if(sender.GetType() == m_DisplayPort.GetType())
            CommandsLst = ((DisplayPort) sender).GetCommand(null);
        else
            CommandsLst = new CMenuActionList();

        if(CommandsLst == null)
            CommandsLst = new CMenuActionList();

        CommandsLst.AddMenu(1, "Quit", 1, new System.EventHandler(Quit));

Points of Interest

The main difficulty was to create the basic rotate/flip routines to manage the coordinate converter. The only limit I found regarding the use of GDI+ was the 16 bits per pixel format which is not implemented, so no use for such images.

History

Version 1.0 is the first release.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerError on Windows xp Pin
pippo pioppo28-Mar-12 3:09
pippo pioppo28-Mar-12 3:09 
Questionloading image in the specified location with actual size Pin
MrKyaw13-Mar-12 17:39
MrKyaw13-Mar-12 17:39 
GeneralGreat Code Pin
afshin_a30-Jul-08 11:13
afshin_a30-Jul-08 11:13 
GeneralGreat component Pin
athmok@yahoo.co.uk1-Feb-07 1:17
athmok@yahoo.co.uk1-Feb-07 1:17 
GeneralGreat Sample Pin
vivekthangaswamy27-Jan-06 6:15
professionalvivekthangaswamy27-Jan-06 6:15 
Generalreally nice one Pin
richarding16-Jan-06 4:16
richarding16-Jan-06 4:16 
GeneralGreat but needed in vb.net Pin
chinnivenkat9-Aug-04 20:20
chinnivenkat9-Aug-04 20:20 
GeneralRe: Great but needed in vb.net Pin
chinnivenkat10-Aug-04 17:07
chinnivenkat10-Aug-04 17:07 
GeneralGood job! Pin
don!marco22-Jun-04 22:15
don!marco22-Jun-04 22:15 
GeneralDemo project liks MSCOREE.DLL Pin
Georgi Petrov5-May-04 2:06
Georgi Petrov5-May-04 2:06 
GeneralRe: Demo project liks MSCOREE.DLL Pin
don!marco22-Jun-04 22:19
don!marco22-Jun-04 22:19 
GeneralOne Really Weird Thing. Pin
kiplringkiplring21-Feb-03 5:08
kiplringkiplring21-Feb-03 5:08 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.