Click here to Skip to main content
11,571,102 members (69,579 online)
Click here to Skip to main content

Image Viewer Control in C#

, 10 Sep 2007 CPOL 72.3K 3.2K 30
Rate this:
Please Sign up or sign in to vote.
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:

using  ALV.ALVCS03CONTROLS;

Daclare the viewer object and instantiate it.

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:

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:

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)

Share

About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionThanks Pin
fstylecihat25-Nov-12 22:46
memberfstylecihat25-Nov-12 22:46 
GeneralMy vote of 5 Pin
manoj kumar choubey26-Feb-12 20:56
membermanoj kumar choubey26-Feb-12 20:56 
QuestionImageFromStream?? Pin
jeffshelley5-Jul-11 7:34
memberjeffshelley5-Jul-11 7:34 
AnswerRe: ImageFromStream?? Pin
jeffshelley11-Jul-11 1:34
memberjeffshelley11-Jul-11 1:34 
GeneralHere's the code Pin
James West25-Oct-09 12:49
memberJames West25-Oct-09 12:49 
GeneralRe: Here's the code Pin
gmagana5-Jan-10 4:41
membergmagana5-Jan-10 4:41 
GeneralRe: Here's the code Pin
Ed Piccolo9-Mar-10 3:36
memberEd Piccolo9-Mar-10 3:36 
GeneralMy vote of 1 Pin
GilSab31-Aug-09 2:54
memberGilSab31-Aug-09 2:54 
QuestionMissing the Source code any suggestions? Pin
jijou24-Jul-09 9:45
memberjijou24-Jul-09 9:45 
Questionhow we perform zoomin and zoomout operation Pin
Babita Shivade18-Apr-09 0:30
memberBabita Shivade18-Apr-09 0:30 
GeneralMy vote of 1 Pin
wpm000121-Mar-09 16:02
memberwpm000121-Mar-09 16:02 
GeneralSave Image Pin
bizzarrazzib16-Mar-09 5:15
memberbizzarrazzib16-Mar-09 5:15 
QuestionHow to Reload the images from this viewer. Pin
mkrishna1$23-Dec-08 7:39
membermkrishna1$23-Dec-08 7:39 
Generalwhere is image property Pin
ham rez29-Sep-08 18:43
memberham rez29-Sep-08 18:43 
GeneralNew version Pin
Prize Pr28-Jul-08 17:37
memberPrize Pr28-Jul-08 17:37 
QuestionHow to Save the picture after it was changed? Pin
Tomasz Januszewski17-Jun-08 22:23
memberTomasz Januszewski17-Jun-08 22:23 
GeneralLoad stream as image Pin
Member 63061920-May-08 18:18
memberMember 63061920-May-08 18:18 
QuestionRun from command line? Pin
dennis powell18-Apr-08 5:20
memberdennis powell18-Apr-08 5:20 
GeneralChange Backgroundcolor Pin
_rush_28-Jan-08 8:43
member_rush_28-Jan-08 8:43 
GeneralLicence Question Pin
_rush_26-Jan-08 23:10
member_rush_26-Jan-08 23:10 
GeneralHopes not come true Pin
mhz_pisem_net9-Dec-07 23:33
membermhz_pisem_net9-Dec-07 23:33 
GeneralHopes not come true Pin
mhz_pisem_net9-Dec-07 23:27
membermhz_pisem_net9-Dec-07 23:27 
Questionhave you sources of the DLL file ? Pin
ramzy198025-Nov-07 9:10
memberramzy198025-Nov-07 9:10 
GeneralSorry for the missing source code, will update asap Pin
AdonisVillamor2-Oct-07 13:59
memberAdonisVillamor2-Oct-07 13:59 
GeneralSource code / Remove the button in the bottom right Pin
SHelwig2-Oct-07 2:09
memberSHelwig2-Oct-07 2:09 
QuestionWhere's the source for ALVImageViewer Pin
Ranjan.D11-Sep-07 7:04
memberRanjan.D11-Sep-07 7:04 
QuestionWhere is the source for DLL? Pin
Vikas K.11-Sep-07 2:25
memberVikas K.11-Sep-07 2:25 
QuestionSource?? Pin
ring_010-Sep-07 22:36
memberring_010-Sep-07 22:36 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 10 Sep 2007
Article Copyright 2007 by adonis_villamor
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid