|
|
|
|
|
|
Author Rank:
|
|
Total page views :
2544
|
|
Total downloads :
|
|
|
|
|
|
|
Similar ArticlesMost ReadTop RatedLatest
|
|
|
|
|
|
|
|
|
|
Before we write any imaging code, let's explore the .NET Framework library and see what kind of imaging support it offers. The Bitmap class provides functionality to work with raster images, and the Metafile class provides functionality to work with vector images. Both classes are inherited from the Image class. In this article we will discuss the Image and Bitmap classes and their members.
We'll start this discussion with the Image class, which is defined in the System.Drawing namespace. Understanding this class is important because we will be using its members in our samples throughout this article.
The Image class is an abstract base class for the Bitmap, Metafile, and Icon classes. Some common Image class properties (all read-only) are described in Table 7.2.
The Pixel Format
The pixel format (also known as color depth) defines the number of bits within each pixel. The format also defines the order of color components within a single pixel of data. In the .NET Framework library, the PixelFormat enumeration represents the pixel format.
Beside the properties discussed in Table 7.2, the Image class provides methods, which are described in Table 7.3.
An Image Viewer Application
Now we will write an application that will use some of the properties and methods of the Image class. You will learn to open, view, manipulate, and save images. The application is a simple image viewer.
TABLE 7.2: Image class properties
|
Property |
Description |
|
Flags |
Gets or sets attribute flags for an image. |
|
FrameDimensionsList |
Returns an array of GUIDs that represent the dimensions of frames within an image. |
|
Height, Width |
Return the height and width of an image. |
|
HorizontalResolution |
Returns the horizontal resolution, in pixels per inch, of an image. |
|
Palette |
Gets or sets the color palette used for an image. |
|
PhysicalDimension |
Returns the width and height of an image. |
|
PixelFormat |
Returns the pixel format for an image. |
|
PropertyIdList |
Returns an array of the property IDs stored in an image. |
|
PropertyItems |
Returns an array of PropertyItem objects for an image. |
|
RawFormat |
Returns the format of an image. |
|
Size |
Returns the width and height of an image. |
|
VerticalResolution |
Returns the vertical resolution, in pixels per inch, of an image. |
To begin:
-
Use Visual Studio .NET to create a Windows application project called ImageViewer.
-
Add a MainMenu control and some menu item to the form.
-
Change the text of the menu items to File, Open File, Save File and Exit, and the name of these menu items to FileMenu, OpenFileMenu, SaveFileMenu, and ExitMenu, respectively. The final form looks like Figure 7.3.
-
Write menu click event handlers for the OpenFileMenu, SaveFileMenu, and ExitMenu items by simply double-clicking on them.
The OpenFileMenu click event handler will allow us to browse and select one image and display it, the SaveFileMenu click event handler will save the image as a new file name, and the ExitMenu click event handler will simply close the application.
Before we write code for these menu event handlers, let's see how to create an Image object from a file and how to display it using the DrawImage method of the Graphics class.
TABLE 7.3: Image class methods
| Method |
Description |
|
FromFile, FromHbitmap, FromStream |
Creates an Image object from a file, a window handle, and a stream, respectively. |
|
GetBounds |
Returns the bounding rectangle for an image. |
|
GetEncoderParameterList |
Returns parameters supported by an image encoder. |
|
GetFrameCount |
Returns the total number of frames available in an image. Some images include multiple frames. Each frames is a separate layer with different properties. For example, and animated GIF can have multiple frames with different text and other properties. |
|
GetPixelFormatSize |
Returns the color depth. |
|
GetPropertyItem |
Returns the property item. |
|
GetThumbnailImage |
Returns the thumbnail for an image. |
|
IsAlphaPixelFormat |
Returns true if the pixel format for an Image object contains alpha information. |
|
IsCanonicalPixelFormat |
Returns true if the pixel format is canonical. This is a reserved format. |
|
IsExtendedPixelFormat |
Returns true if the pixel format is extended. This is a reserved format. |
|
RemovePropertyItem |
Removes the property item. |
|
RotateFlip |
Rotate and / or flips an image. |
|
Save |
Saves an image in a specified format. |
|
SaveAdd |
Takes one parameter of type EncoderParameters that defines parameters required by the image encoder that is used by the save-add operation. |
|
SelectActiveFrame |
Selects a frame specified by the dimensions and index. The first parameter of this method is the frame dimensions, which can be used to identify an image by its time, resolution, or page number. The second parameter is the frame index of the active frame. Calling this method causes all changes made to the previous frame to be discarded. |
|
SetPropertyItem |
Sets the value of a property item. |

FIGURE 7.3: A simple image viewer application
Conclusion
Hope the article would have helped you in understanding image class methods and properties in GDI+. Read other articles on GDI+ on the website.
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
60 FREE UI Controls from DevExpress
Register for your FREE copy on over 60 free presentation controls from
DevExpress - Absolutely Free-of-Charge without any royalties or distribution
costs. Visit Devexpress.com/60 today. Free controls include advanced lists box, dropdown calendar, rich text edit, spin
edit, tab control and so much more!
DevExpress engineers feature rich presentation controls and reporting tools for WinForms, ASP.NET, WPF, and Silverlight. Our technologies help you build your best, see complex software with greater clarity and deliver compelling business solutions for Windows and the web in the shortest possible time.
|
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.
|
Microsoft Visual Studio 2010
Visualize your workspace with new multiple monitor support, powerful Web development, new SharePoint support with tons of templates and Web parts, and more accurate targeting of any version of the .NET Framework. Get set to unleash your creativity.
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|