C# Image Viewer Sample Application

Jim C. Huang

Sr. Technical Marketing Engineer, Software & Solutions Group

Why Should I Care About This Code Sample?

This code sample demonstrates how easy it is to create rapid prototypes with the jazzy user interface in C#. The code sample demonstrates the use of a PictureBox property to display an image, how to quickly create a bitmap button without writing a line of code, and how to manipulate the Form's property to create a simple Dialogbox.


Just when you thought the woods were clear and the language of choice was going to be C, C++, and Visual Basic, Microsoft* introduces a new language to make a developer's life easier: C#. This simple, modern, object oriented, and type-safe programming language derives from C and C++, but has the high productivity of Visual Basic.

Developers who have used Visual Studio 6.0 have determined that it was time- consuming, if not difficult, to create any visually appealing user interface prototype. Developing in C# using Visual Studio .Net removes many hurdles developers faced when trying to create a visually appealing interface.

The C# Image Viewer sample code was written with very little effort. C# includes a lot of built-in functionality, which makes it simple and easy to develop a simple image viewing utility.

Target Audience
Developers writing C# applications who need to reference sample code to understand how to manipulate the user interface properties.

Sample Category:
Full Project

Implementation Language:

Target Hardware & Software Platforms

Hardware Systems:
Systems running Intel® Pentium® processors

Operating Systems:
Microsoft Windows* 98, Microsoft Windows* 2000, Microsoft Windows* XP with .NET Framework.

Visual Studio .Net

Sample Application
Given the right tool and functionality, any developer can create applications that are powerful and easy to use. This document does not describe the C# language or syntax, but mainly the properties that are used to create this application. To learn more about C# language specification, please visit http://msdn.microsoft.com* and search for C#.

The illustration shows the user interface of the Image Viewer. The following sections describe the components used to create this simple application.

The following subsections describe the properties that are used to create the custom look of the application.

  • Framework
    • Creating a dialog box is not as intuitive as it used to be. The first difference is working with Forms and not Windows or dialog boxes. The best part of Forms is the flexibility of transforming the Forms into a dialog box look and feel, or a dynamic Window. You can think of the Form as an empty canvas where you can tra nsform the canvas to meet your requirement.

      To develop the custom look and feel of the dialog box, the following properties of the Form are changed:

      FormBorderStyle           Fixed3D

      MaximizeBox                False
  • Buttons
    • The differences between creating a button in C# Form and C++ dialog box is the ability to attach images to the button without having to write a line of code. This is a feature that was introduced in Visual Basic, but has always been lacking in Visual Studio C++. Not only can images be associated with a button, but changing the color of the button is also simple and straightforward.

      Changing the button's Image properties enables the designer to attach any image. The designer can also change the button's color, text font, text color, and other properties that used to require a Visual C++ developer to painfully create the code.
  • PictureBox
    • The PictureBox Form component was available to Visual Basic (VB) programmers, but for Visual C++ (VC++) programmers, displaying an image in a frame had to be manually coded. This made C++ programmers require more time to create prototypes.

      This is another great example where C# has an advantage over VB and VC++. Not only can you use components that are only available in VB, but you can use your VC++ coding style to display the images. Displaying an image in the PictureBox is as simple as setting the Image property to the image you want to display. The images are limited to bitmap (BMP), metafile, icons (ICO), JPEG, GIF, and PNG formats.

      The following example illustrates how to display a picture in the display area of the picture box.

      PictureBox.Image = System.Drawing.Image.FromFile(picturefile);

      The sample application also utilizes the SizeMode property to show change how the image is displayed.

For C and C++ programmers who do rapid prototyping, working in the C# environment can reduce the time and effort to create compelling prototypes.

Download Code Sample