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

Image Viewer Control in C#

, 10 Sep 2007
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)

About the Author

No Biography provided

Comments and Discussions

 
QuestionThanks Pinmemberfstylecihat25-Nov-12 22:46 
GeneralMy vote of 5 Pinmembermanoj kumar choubey26-Feb-12 20:56 
QuestionImageFromStream?? Pinmemberjeffshelley5-Jul-11 7:34 
AnswerRe: ImageFromStream?? Pinmemberjeffshelley11-Jul-11 1:34 
GeneralHere's the code PinmemberJames West25-Oct-09 12:49 
GeneralRe: Here's the code Pinmembergmagana5-Jan-10 4:41 
GeneralRe: Here's the code PinmemberEd Piccolo9-Mar-10 3:36 
GeneralMy vote of 1 PinmemberGilSab31-Aug-09 2:54 
QuestionMissing the Source code any suggestions? Pinmemberjijou24-Jul-09 9:45 
Questionhow we perform zoomin and zoomout operation PinmemberBabita Shivade18-Apr-09 0:30 
GeneralMy vote of 1 Pinmemberwpm000121-Mar-09 16:02 
GeneralSave Image Pinmemberbizzarrazzib16-Mar-09 5:15 
QuestionHow to Reload the images from this viewer. Pinmembermkrishna1$23-Dec-08 7:39 
Generalwhere is image property Pinmemberham rez29-Sep-08 18:43 
GeneralNew version PinmemberPrize Pr28-Jul-08 17:37 
QuestionHow to Save the picture after it was changed? PinmemberTomasz Januszewski17-Jun-08 22:23 
GeneralLoad stream as image PinmemberMember 63061920-May-08 18:18 
QuestionRun from command line? Pinmemberdennis powell18-Apr-08 5:20 
GeneralChange Backgroundcolor Pinmember_rush_28-Jan-08 8:43 
GeneralLicence Question Pinmember_rush_26-Jan-08 23:10 
GeneralHopes not come true Pinmembermhz_pisem_net9-Dec-07 23:33 
GeneralHopes not come true Pinmembermhz_pisem_net9-Dec-07 23:27 
Questionhave you sources of the DLL file ? Pinmemberramzy198025-Nov-07 9:10 
GeneralSorry for the missing source code, will update asap PinmemberAdonisVillamor2-Oct-07 13:59 
GeneralSource code / Remove the button in the bottom right PinmemberSHelwig2-Oct-07 2:09 
QuestionWhere's the source for ALVImageViewer PinmemberRanjan.D11-Sep-07 7:04 
QuestionWhere is the source for DLL? PinmemberVikas K.11-Sep-07 2:25 
QuestionSource?? Pinmemberring_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 | Mobile
Web03 | 2.8.140721.1 | Last Updated 10 Sep 2007
Article Copyright 2007 by adonis_villamor
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid