Blue Theme Orange Theme Green Theme Red Theme
 
World Class ASP.NET Hosting – Click Here for 3 Months Free/NO Setup Fee!
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Windows Forms » Image Viewer in C#

Image Viewer in C#

This program allows you to open and view image files including JPEG, GIF, WMF and other images.

Total page views :  80225
Total downloads :  1818
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ImageViewerCodeST.zip
 
Become a Sponsor

Description

This program allows you to open and view image files including JPEG, GIF, WMF and other images. Program also provides options to stretch and shrink them, rotate at different angles through all axes and save them in different formats.

This example uses menus, radio buttons, group boxes, open and save dialog boxes, picture box and status bar controls.

Compiling Code with Visual Studio .NET

To compile and execute code with VS.NET, create a Windows application and copy and paste this code in Form1.cs class. Replace all code from Form1.cs accept namespace includes.

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Text;
using System.Drawing.Imaging;
using System.IO;
public class win:Form
{
Stream stm;
Panel pnlTop;
StatusBar stbBtm;
Label lblPicMode;
MainMenu mnuMain;PictureBox pbxImg;
bool blnPicLoaded;
ComboBox cmbPicMode;
OpenFileDialog dlgFile;
Button btnTransform,btnOrg;
GroupBox gpbRotate,gpbFlip;
MenuItem mnuFile, mnuOpen, mnuSave, mnuExit, mnuSep;
String strImgName,strRot,strFlip,strRotFlip,strStatus;
RadioButton bnRotNone,rbnRotX,rbnRotY,rbnRotXY,rbnFlipNone,rbnFlip90,rbnFlip180,rbnFlip270,rbnTemp;
public win()
{
try
{
this.Text="Images";
pnlTop=new Panel();
this.Size=new Size(770,570);
this.Controls.Add(pnlTop);
this.Menu=fncBuildMenus();
setControls();
strRot=rbnRotNone.Name;
strFlip=rbnFlipNone.Name;
pnlTop.Location = new Point(0,0);
pnlTop.Size = new Size(750,500);
}
catch (Exception e)
{
Console.WriteLine("error .... " + e.StackTrace);
}
}
private static void Main()
{
Application.Run(new win());
}
private MainMenu fncBuildMenus()
{
/* build the menu's */
mnuMain=new MainMenu();
mnuFile=new MenuItem();
mnuOpen=new MenuItem();
mnuSave=new MenuItem();
mnuExit=new MenuItem();
mnuSep=new MenuItem();
mnuFile.Text="&File";
mnuOpen.Text="&Open";
mnuSave.Text="Save &As";
mnuExit.Text="E&xit";
mnuSep.Text="-";
mnuFile.MenuItems.Add(mnuOpen);
mnuFile.MenuItems.Add(mnuSave);
mnuFile.MenuItems.Add(mnuSep);
mnuFile.MenuItems.Add(mnuExit);
mnuMain.MenuItems.Add(mnuFile);
mnuOpen.Click+=new EventHandler(fncOpen);
mnuSave.Click+=new EventHandler(fncSave);
mnuExit.Click+=new EventHandler(fncExit);
return mnuMain;
}
private void setControls()
{
/* initialize and add the controls to the form. */
gpbRotate = new GroupBox();
gpbRotate.Location = new Point(0,0);
gpbRotate.Size=new Size(300,50);
gpbRotate.Text = "Rotate";
gpbFlip = new GroupBox();
gpbFlip.Location = new Point(300,0);
gpbFlip.Size = new Size(300,50);
gpbFlip.Text = "Flip";
rbnRotNone = fncRadBtns("None","None",50,10,20);
rbnFlipNone = fncRadBtns("None","None",50,10,20);
rbnRotX = fncRadBtns("90 deg","90",70,80,20);
rbnRotY = fncRadBtns("180 deg","180",70,150,20);
rbnRotXY = fncRadBtns("270 deg","270",70,220,20);
rbnFlip90 = fncRadBtns("X - axis","X",70,80,20);
rbnFlip180 = fncRadBtns("Y - axis","Y",70,150,20);
rbnFlip270 = fncRadBtns("XY - axis","XY",70,220,20);rbnRotNone.Checked = true;
rbnFlipNone.Checked = true;
btnTransform = new Button();
btnTransform.Text="Transform Image";
btnTransform.Location = new Point(0,65);
btnTransform.Width=100;
btnOrg = new Button();
btnOrg.Text = "Original Position";
btnOrg.Location = new Point(200,65);
btnOrg.Width = 100;
lblPicMode = new Label();
lblPicMode.Text = "Picture Mode ";
lblPicMode.Location = new Point(350,67);
lblPicMode.Width = 70;
cmbPicMode = new ComboBox();
cmbPicMode.Location = new Point(420,65);
cmbPicMode.DropDownStyle=ComboBoxStyle.DropDownList;
cmbPicMode.Items.Add("Auto Size");
cmbPicMode.Items.Add("Center Image");
cmbPicMode.Items.Add("Normal");
cmbPicMode.Items.Add("Stretch Image");
cmbPicMode.SelectedIndex=2;
pbxImg = new PictureBox();
pbxImg.Location = new Point(0,100);
pbxImg.Size = new Size(750,400);
stbBtm=new StatusBar();
stbBtm.Text = "Normal mode - Image is clipped if it is bigger than the Picture Box.";
stbBtm.BackColor=Color.Green;
stbBtm.Size=new Size(750,20);
stbBtm.Location = new Point(0,550);
gpbRotate.Controls.Add(rbnRotNone);
gpbRotate.Controls.Add(rbnRotX);
gpbRotate.Controls.Add(rbnRotY);
gpbRotate.Controls.Add(rbnRotXY);
gpbFlip.Controls.Add(rbnFlipNone);
gpbFlip.Controls.Add(rbnFlip90);
gpbFlip.Controls.Add(rbnFlip180);
gpbFlip.Controls.Add(rbnFlip270);
pnlTop.Controls.Add(gpbRotate);
pnlTop.Controls.Add(gpbFlip);
pnlTop.Controls.Add(btnTransform);
pnlTop.Controls.Add(btnOrg);
pnlTop.Controls.Add(lblPicMode);
pnlTop.Controls.Add(cmbPicMode);
Controls.Add(stbBtm);
pnlTop.Controls.Add(pbxImg);
blnPicLoaded=false;
strStatus=stbBtm.Text;
}
private RadioButton fncRadBtns(string strText,string strName,int intWidth,int intX,int intY)
{
RadioButton rbnTmp;
rbnTmp = new RadioButton();
rbnTmp.Text = strText;
rbnTmp.Name = strName;
rbnTmp.Width = intWidth;
rbnTmp.Location = new Point(intX,intY);
return rbnTmp;
}
private void fncOpen(object obj,EventArgs ea)
{
/* load the picture. */
try
{
dlgFile=new OpenFileDialog();
dlgFile.Filter="JPEG Images (*.jpg,*.jpeg)|*.jpg;*.jpeg|Gif Images (*.gif)*.gif|Bitmaps (*.bmp)|*.bmp";
dlgFile.FilterIndex=1;
if (dlgFile.ShowDialog()== DialogResult.OK)
{
if((stm=dlgFile.OpenFile())!=null)
{
strImgName=dlgFile.FileName;
stm.Close();
pbxImg.Image=Image.FromFile(strImgName);
blnPicLoaded=true;
}
}
if (blnPicLoaded)
{
/* if the picture is loaded then enable the events. */
rbnRotNone.Click+=new EventHandler(fncRot);
rbnRotX.Click+=new EventHandler(fncRot);
rbnRotY.Click+=new EventHandler(fncRot);
rbnRotXY.Click+=new EventHandler(fncRot);
rbnFlipNone.Click+=new EventHandler(fncFlip);
rbnFlip90.Click+=new EventHandler(fncFlip);
rbnFlip180.Click+=new EventHandler(fncFlip);
rbnFlip270.Click+=new EventHandler(fncFlip);
btnTransform.Click+= new EventHandler(fncTransform);
btnOrg.Click += new EventHandler(fncTransform);
cmbPicMode.SelectionChangeCommitted += new EventHandler(fncPicMode);
}
}
catch(Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
private void fncSave(object sender, EventArgs ea)
{
try
{
/* save the image in the required format. */
SaveFileDialog dlgSave = new SaveFileDialog();
dlgSave.Filter="JPEG Images (*.jpg,*.jpeg)|*.jpg;*.jpeg|Gif Images (*.gif)*.gif|Bitmaps (*.bmp)|*.bmp";
if (dlgSave.ShowDialog()==DialogResult.OK)
{
strImgName=dlgSave.FileName;
if (strImgName.EndsWith("jpg"))
pbxImg.Image.Save(strImgName,ImageFormat.Jpeg);
if (strImgName.EndsWith("gif"))
pbxImg.Image.Save(strImgName,ImageFormat.Gif);
if (strImgName.EndsWith("bmp"))
pbxImg.Image.Save(strImgName,ImageFormat.Bmp);
}
}
catch(Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
private void fncRot(object obj,EventArgs ea)
{
rbnTemp = (RadioButton)obj;
strRot=rbnTemp.Name;
setStatus();
}
private void fncFlip(object obj,EventArgs ea)
{
rbnTemp = (RadioButton)obj;
strFlip = rbnTemp.Name;
setStatus();
}
private void fncTransform(object obj,EventArgs ea)
{
Button btnTemp=(Button)obj;
if (btnTemp.Text=="Transform Image")
{
strRotFlip=strRot + strFlip;
switch (strRotFlip)
{
case "180None" :
pbxImg.Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
break;
case "180X" :
pbxImg.Image.RotateFlip(RotateFlipType.Rotate180FlipX);
break;
case "180Y" :
pbxImg.Image.RotateFlip(RotateFlipType.Rotate180FlipY);
break;
case "180XY" :
pbxImg.Image.RotateFlip(RotateFlipType.Rotate180FlipXY);
break;
case "270None" :
pbxImg.Image.RotateFlip(RotateFlipType.Rotate270FlipNone);
break;
case "270X" :
pbxImg.Image.RotateFlip(RotateFlipType.Rotate270FlipX);
break;
case "270Y" :
pbxImg.Image.RotateFlip(RotateFlipType.Rotate270FlipY);
break;
case "270XY" :
pbxImg.Image.RotateFlip(RotateFlipType.Rotate270FlipXY);
break;
case "90None" :
pbxImg.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
break;
case "90X" :
pbxImg.Image.RotateFlip(RotateFlipType.Rotate90FlipX);
break;
case "90Y" :
pbxImg.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
break;
case "90XY" :
pbxImg.Image.RotateFlip(RotateFlipType.Rotate90FlipXY);
break;
case "NoneNone" :
pbxImg.Image.RotateFlip(RotateFlipType.RotateNoneFlipNone);
break;
case "NoneX" :
pbxImg.Image.RotateFlip(RotateFlipType.RotateNoneFlipX);
break;
case "NoneY" :
pbxImg.Image.RotateFlip(RotateFlipType.RotateNoneFlipY);
break;
case "NoneXY" :
pbxImg.Image.RotateFlip(RotateFlipType.RotateNoneFlipXY);
break;
default :
pbxImg.Image.RotateFlip(RotateFlipType.RotateNoneFlipNone);
break;
}
}
else if (btnTemp.Text=="Original Position")
{
pbxImg.Image=Image.FromFile(strImgName);
pbxImg.Refresh();
}
pbxImg.Refresh();
}
private void fncPicMode(object obj,EventArgs ea)
{
ComboBox objTemp = (ComboBox)obj;
switch (objTemp.SelectedIndex)
{
case 0 :
pbxImg.SizeMode=PictureBoxSizeMode.AutoSize;
strStatus="AutoSize mode - The PictureBox is sized equal to the size of the image
that it contains.";
break;
case 1 :
pbxImg.SizeMode=PictureBoxSizeMode.CenterImage;
strStatus="CenterImage mode - Image is placed in the center of the Picture Box.";
strStatus=strStatus + "If the image is big then outside edges of Image is clipped.";
break;
case 2 :
pbxImg.SizeMode=PictureBoxSizeMode.Normal;
strStatus="Normal mode - Image is clipped if it is bigger than the Picture Box.";
break;
case 3 :
pbxImg.SizeMode=PictureBoxSizeMode.StretchImage;
strStatus="Stretch mode - Image is stretched or shrunk to fit the Picture Box.";
break;
}
pbxImg.Refresh();
stbBtm.Text=strStatus;
}
private void setStatus()
{
strStatus="The Image is rotated ";
if (strRot != null && strRot != "None")
strStatus=strStatus + strRot + " degrees";
if (strFlip !=null && strFlip != "None")
strStatus=strStatus +" around " + strFlip + " axis.";
stbBtm.Text=strStatus;
}
private void fncExit(object obj,EventArgs ea)
{
Application.Exit();
}
}


Login to add your contents and source code to this article
 About the author
 
S Thangaraju
S.Thangaraju, employed as a software engineer in a CMM level 4 company.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ImageViewerCodeST.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments
lost picture by rane On January 26, 2007
please help.. i can't see any picture there.. thank you!
Reply | Email | Delete | Modify | 
Hello by Justinas On January 15, 2009
Hello Mr. Thangaraju. I'm quite new in C# so i get a few problems. Please, can you send me full project to drakosaz@gmail.com ? I think that it would be the best way, because i can find what i'm doing wrong on my own.
Reply | Email | Delete | Modify | 
tip by vinioth On October 1, 2009

adsfasdfadsf
Reply | Email | Delete | Modify | 
m,nmn, by amir On January 9, 2010
kkjjkljklkjlkjlkj
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.