Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#

Image Viewer Control in C#

Rate me:
Please Sign up or sign in to vote.
1.23/5 (14 votes)
10 Sep 2007CPOL3 min read 116.3K   5.2K   30   29
An image viewer control with Zoom, Flip, Rotate, and Image Grab. Supports even multipage images like TIFF.

Screenshot - 1.jpg

Introduction

First of all, this is not a complicated control. The goal is to create a user control in which you can drag drop a Button or Label and all the methods and properties that are related to your/my daily image viewing needs.

The idea came when I wanted to create a generic viewer that could load almost anything from the browser like PDF, Word/Excel (Office), image etc. in just one interface. Hopefully, this can help a lot of people who needs image viewing in their projects.

This can be used for Win32 supported images like BMP, JPG, GIF etc., and also for multipage images like TIFF.

Using the Code

To use the control, you must first add the DLL to the references. After that, you add the directive with the using statement:

C#
using  ALV.ALVCS03CONTROLS;

Daclare the viewer object and instantiate it.

C#
private ALVImageViewer alvImageViewer1;
// ..
this.alvImageViewer1 = new ALVImageViewer();

Once this is done, you can now use its different methods/properties during the events triggered by your buttons etc.

You just create a button for Flip left/Flip Right, Rotate left and right, Zoom in and Zoom Out. A radio button for Auto Fit To Screen (locates best fit by the width and height of the control), Auto Fit By Width (based on the width of the control) or by Height (you know..).

The very first step is loading an image file:

C#
alvImageViewer1.ImageFromFile("C:\MyImage.BMP");

Now, based on whether any of the fit types is selected (the default is always fit to screen), the image should load appropriately. To get the original image size, just set all the AutoFit properties to false. (Note that when you set auto-fit to true, if there is another auto-fit that is active, it will deactivated or set to false.)

Here are the rest of the methods you need to know:

C#
alvImageViewer1.Rotate90Left(); //rotate 90D Left
alvImageViewer1.Rotate90Right(); //you know..
alvImageViewer1.FrameMoveTo(pageNo) //for multipage like tif
alvImageViewer1.FlipHorizontal(); //obvious
alvImageViewer1.FlipVertical(); //duh!
alvImageViewer1.ZoomRatio //now here is somthing i have to note

The zoom ratio is used for the zoom in and out feature. I limited the max value to 10 and min value to 0.1. The zoom ratio is actually the scale of the image where 1.0 is 100% or the real size of the image, 2.0 is 2x the original scale of the image, and 0.1 is 1/10th of the original image scale. The zoom ratio "will" always be set to something else "if" any of the auto-fit properties is set to true. So, if you have Fit To Width set to true, then the image will scale the zoom ratio to the size that will fit by width, and whenever you try to change the zoom ratio to something else, the control would just ignore the command.

Points of Interest

Creating the control was not that easy. Sometimes you don't understand how the bugs happen, like image view missing and scroll bars not responding correctly etc. But, after a day's work it's all worth it. I will upload a better version if I add PDF and Office support.

History

  • September 2004 - Image Viewer was created with Notepad and .NET SDK 2003.
  • April 2005 - GUI for version 1.0.
  • July 2007 - DMS project revision into VB.NET because the project leader does not know C# or VB.NET, so I had to switch to something it "might/possibly or hopefully" be familiar with.
  • September 2007 - 2005 GUI recreated because I lost the old one, and this is much better I believe.

License

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


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

Comments and Discussions

 
GeneralHopes not come true Pin
mhz_pisem_net9-Dec-07 23:33
mhz_pisem_net9-Dec-07 23:33 
GeneralHopes not come true Pin
mhz_pisem_net9-Dec-07 23:27
mhz_pisem_net9-Dec-07 23:27 
Questionhave you sources of the DLL file ? Pin
ramzy198025-Nov-07 9:10
ramzy198025-Nov-07 9:10 
GeneralSorry for the missing source code, will update asap Pin
AdonisVillamor2-Oct-07 13:59
AdonisVillamor2-Oct-07 13:59 
GeneralSource code / Remove the button in the bottom right Pin
SHelwig2-Oct-07 2:09
SHelwig2-Oct-07 2:09 
QuestionWhere's the source for ALVImageViewer Pin
Ranjan.D11-Sep-07 7:04
professionalRanjan.D11-Sep-07 7:04 
QuestionWhere is the source for DLL? Pin
Vikas K.11-Sep-07 2:25
Vikas K.11-Sep-07 2:25 
QuestionSource?? Pin
ring_010-Sep-07 22:36
ring_010-Sep-07 22:36 
Source missing ?

Man having 1 bit brain with parity error

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.