ch15_Compact Framework Graphics.pdf

(119 KB) Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Chapter 15
Compact Framework Graphics
This chapter introduces the basics of creating graphical output from a Compact Framework
System Colors ..................................................................................................... 17
Named Colors...................................................................................................... 18
Colors from RGB Values ..................................................................................... 19
Creating Brushes.............................................................................................................. 21
Creating Brushes with System Colors................................................................. 21
Creating Brushes with Named Colors ................................................................. 21
Creating Brushes with RGB Values .................................................................... 22
Creating Bitmaps .............................................................................................................. 22
Bitmaps: drawing surface or drawing object? ..................................................... 23
The Bitmap class ................................................................................................. 23
Creating an empty bitmap ................................................................................... 24
Creating a bitmap from an external file ............................................................... 25
Creating a bitmap from a resource...................................................................... 25
Image file sizes.................................................................................................... 29
Drawing Bitmaps .............................................................................................................. 30
Drawing entire bitmap at original image size ...................................................... 30
Drawing part of a bitmap at original image size .................................................. 31
Drawing part of a bitmap with change to image size .......................................... 31
Drawing part of a bitmap with change to image size and with transparency ...... 31
Sample: ShowBitmap ....................................................................................................... 33
Chapter 15 – Compact Framework Graphics Page 1
11/7/2003
Copyright © 2002-2003 Paul Yao & David Durant
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Vector Graphics ............................................................................................................................. 39
Creating Pens ...................................................................................................................40
A Game: JaspersDots ...................................................................................................... 41
Conclusion ..................................................................................................................................... 53
This chapter describes the support that a Compact Framework program has for creating
graphical output. As we mention elsewhere in this book, we prefer using Compact Framework
classes whenever possible. To accomplish something beyond what the Compact Framework
supports, however, we drill through the managed code layer to the underlying Win32 API
substrate. This chapter, and the two that follow, discusses the Compact Framework's built-in
support for creating graphical output; this chapter also touches on the limitations of that support,
and how to supplement that support with the help of GDI functions. [Comment 15.2]
An Introduction to Compact Framework Graphics
Generally speaking, programs do not create graphical output by drawing directly to device
hardware 1 . A program typically calls a library of graphical output functions. Those drawing
functions, in turn, rely on device drivers which provide the device-specific elements needed to
create output on a device. Historically, creating output on a graphic device such as a display
screen or a printer involves these three software layers: [Comment 15.3]
Drawing program
Graphic function library
Graphic device driver (display driver or printer driver)
The core graphics library on desktop Windows is the Graphics Device Interface ( gdi32.dll ).
With the coming of .NET, Microsoft added a second library, GDI+ ( gdiplus.dll 2 ) , to
supplement GDI drawing support. This second library provides a set of enhancements on top of
the core GDI drawing functions. While the primary role for GDI+ was to support graphics for the
managed code library, it also provides a nice bonus for native-mode application programmers:
the library can be called from unmanaged (native-mode) C++ programs. On the desktop, these
two graphic libraries – GDI and GDI+ – provide the underpinnings for all of the .NET graphic
classes. And so, with .NET Framework programs running on the Windows desktop, the
architecture of graphical output involves the following elements: [Comment 15.4]
Managed code program
Shared managed code library ( System.Drawing.dll )
GDI+ native code library ( gdiplus.dll )
GDI native code library ( gdi32.dll )
Graphic device driver (display driver or printer driver)
.NET Framework Drawing and Desktop Graphic Device Drivers
With the introduction of the .NET Framework, no changes were required
to the graphic device drivers of any version of Microsoft Windows. That is,
the device driver model used by both display screens and printer drivers
was robust enough to support the .NET drawing classes. [Comment 15.5]
Windows CE supports a select set of GDI drawing functions. There is no library explicitly
named GDI in Windows CE. Instead, the graphical output functions reside in the coredll.dll
1 But when necessary for performance reasons or to access device-specific features, a program might
bypass the intervening software layers and interact with hardware.
2 GDI+ is a native-mode, unmanaged code library.
Chapter 15 – Compact Framework Graphics Page 2
11/7/2003
Copyright © 2002-2003 Paul Yao & David Durant
870584819.001.png 870584819.002.png
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
library. These functions are exactly like their desktop counterparts, so even if there is no library
named GDI in Windows CE, we refer to these functions as GDI functions. [Comment 15.6]
Of the 400 or so functions that exist on desktop versions of GDI, only about 85 are included
in Windows CE. Windows CE has none of the drawing functions from the extended desktop
graphics library, GDI+. This places some limits on the extent to which Windows CE can support
.NET drawing functions. [Comment 15.7]
With just 85 of the graphical functions from the desktop's GDI library, and with none of the
functions from GDI+, you might wonder whether Windows CE has enough graphic support to
create interesting graphical output. The answer is a resounding: Yes! While there are not a lot of
graphical functions, the ones that are present were hand-picked as the ones that programs tend
to use the most. There are, for example, a good set of text, raster, and vector functions. A
program can use fonts to create rich text output, display bitmaps along with other kinds of raster
data (like JPEG files), and can draw vector objects such as lines and polygons. [Comment 15.8]
For graphical output, Compact Framework programs rely on System.Drawing.dll , which
is also the name of the graphical output library in the desktop framework. At 38K, the Compact
Framework library is significantly smaller than the 456K of its counterpart on the desktop. While
the desktop library supports five namespaces, the Compact Framework version supports one:
System.Drawing (plus tiny fragments of two other namespaces). The architecture for drawing
from a Compact Framework program is as follows: [Comment 15.9]
Managed code program
Managed code library ( System.Drawing.dll )
GDI functions in native code library ( coredll.dll )
Graphic device driver (display or printer)
From the arrangement of these software layers, a savvy Compact Framework programmer can
divine two interesting points: (1) The managed code library depends on the built-in GDI drawing
functions, and managed code programs can do the same; (2) As on the desktop, display screens
and printers require a dedicated graphic driver to operate. [Comment 15.10]
If possible, delegate graphical output to a control
Before you dig into Compact Framework graphics, ask yourself whether
you want to create the graphical output yourself, or whether you can
delegate that work to a control. If a control exists that can create the
output you require, you can save yourself a lot of effort by using that
control instead of writing the drawing code yourself. For example, the
PictureBox control displays bitmaps and JPEG images with little effort.
Aside from that single control, however, most controls are text-oriented.
Doing your own drawing – and making it look good – takes time and
energy. By delegating graphical output to controls, you can concentrate
on application-specific work. The built-in controls support a highly
interactive, if somewhat text-oriented, user-interface. [Comment 15.12]
Sometimes, however, you do your own drawing to give your program a
unique look and feel. When that is the case, you can create rich,
graphical output using classes in the Compact Framework's
System.Drawing namespace. [Comment 15.13]
Drawing Surfaces
On the Windows desktop, there are four types of drawing surfaces: [Comment 15.14]
1 Display Screen [Comment 15.15]
Chapter 15 – Compact Framework Graphics Page 3
11/7/2003
Copyright © 2002-2003 Paul Yao & David Durant
870584819.003.png
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
When we use the term 'drawing surface', we mean either a physical or logical drawing surface.
Two of the four drawing surfaces in the list are physical drawing surfaces, which require
dedicated device drivers: display screens and printers. The other two drawing surfaces are
logical drawing surfaces: bitmaps and metafiles. These latter two store pictures for eventual
output to a device. [Comment 15.19]
Bitmaps and metafiles are similar enough that they share a common base class in the
desktop .NET Framework: the Image 3 class. Metafiles are not officially supported in Windows
CE, however, and so its wrapper, the Metafile 4 class, does not exist in the current version of
the Compact Framework. Because metafiles might someday be supported in a future version of
the Compact Framework, they are worth a brief mention here. [Comment 15.20]
Display Screen
The display screen plays a central role in all GUI environments, for it is on the display screen
where a user interacts with the various GUI applications. The real stars of the display screen are
the windows, after which the operating system gets its very name. A window acts as a virtual
console 5 for interacting with a user. The physical console for a desktop PC consists of a display
screen, a mouse, and a keyboard. On a Pocket PC, the physical console is made up of a display
screen, a stylus and touch-sensitive screen for pointing, and hardware buttons for input
(supported, of course, by the on-screen keyboard. [Comment 15.21]
All graphical output on the display screen is directed to one window or another. Enforcement
of window boundaries relies on clipping . Clipping is the establishment and enforcement of
drawing boundaries; a program can draw inside clipping boundaries but not outside them. The
simplest clipping boundaries are a rectangle. The area inside a window where a program may
draw is referred to as the window's client area . [Comment 15.22]
Printer
Printers are the best established and most connected peripheral in the world of computers.
While some industry pundits rant about the soon-to-arrive future paperless office, just the
opposite has occurred. Demand for printed output has continued to go up, not down. Perhaps the
world of computers – with its flashing LCD displays, volatile RAM, and every-shrinking silicon –
makes a person want something that is more real. [Comment 15.23]
Printing from Windows CE-powered devices is still in its infancy. Perhaps users can print
what they want from desktop PCs. Or maybe the problem is that programs bundled with a Pocket
PC – programs like Pocket Word and Pocket Excel – do not support printing. Whatever the
cause, we show you several ways to print in chapter 18 ( Printing ), so that you can decide
whether the results are worth the effort. [Comment 15.24]
Bitmap
Bitmaps provide a way to store a picture. Like its desktop counterparts, Windows CE
supports device-independent bitmaps (DIBs) as first-class citizens. In-memory bitmaps can be
created of any size 6 , and treated like any other drawing surface. After a program has drawn to a
bitmap, that image can be put on the display screen. [Comment 15.25]
If you look closely, you can see that Windows CE – and the Compact Framework – support
other raster formats. Supported formats include GIF, PNG, and JPEG. When Visual Studio .NET
reads files with these formats – which it uses for inclusion in image lists, for example – it converts
the raster data to a bitmap. The same occurs when a PNG or JPEG file is read from the object
store into a Compact Framework program. Whatever external format is used for raster data,
3 Fully-qualified name: System.Drawing.Image .
4 Fully-qualified name: System.Drawing.Imaging.Metafile .
5 A term we first heard from Marlin Eller, a member of the GDI team for Windows 1.x.
6 The limitation on bitmap size is available system memory.
Chapter 15 – Compact Framework Graphics Page 4
11/7/2003
Copyright © 2002-2003 Paul Yao & David Durant
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Windows CE prefers bitmaps. In this chapter, we show how to create a bitmap from a variety of
sources and to draw those bitmaps onto the display screen from a Compact Framework program.
Metafile
A second picture-storing mechanism supported by desktop Windows consists of metafiles. A
metafile is a record-and-playback mechanism that stores the details of GDI drawing calls. The
32-bit version of Windows metafiles are known as Enhanced Metafiles (EMF). The following
Win32 native metafile functions are exported from coredll.dll , but are not officially supported
in Windows CE. These functions might gain official support in some future version of Windows
CreateEnhMetaFile
PlayEnhMetaFile
CloseEnhMetaFile
DeleteEnhMetaFile
Supported Drawing Surfaces
Of these four types of drawing surfaces, three have official support in Windows CE: display
screens, printers, and bitmaps. Only two are supported by the Compact Framework: display
screens and bitmaps. Support for bitmaps centers around the Bitmap 7 class, which we discuss
later in this chapter. We start this discussion of graphical output with the drawing surface that is
the focus in all GUI systems: the display screen. [Comment 15.28]
Drawing Function Families
All of the graphical output functions can be organized into one of three drawing function
Text
Raster
Vector
Each family has its own set of drawing attributes, and its own logic for how its drawing is done.
The distinction between these three kinds of output extends from the drawing program into the
graphic device drivers. Each family is complex enough for a programmer to spend many years
mastering the details and intricacies of each type of drawing. The drawing support is rich enough,
however, so that you do not have to be an expert to take advantage of what is offered. [Comment
Text
When discussing drawing text, the most important issue involves selection of the font,
because all text drawing requires a font, and the font choice has the greatest impact on the visual
display of text. The only other drawing attribute that affects text drawing is color – both the
foreground text and the color of the background area. We touch on text briefly in this chapter, but
the topic is important enough to warrant a complete chapter, which is what we provide in chapter
16 ( Text and Fonts ). [Comment 15.31]
Raster
Raster data involves working with arrays of pixels, sometimes known as bitmaps or image
data. Internally, raster data is stored as a Device Independent Bitmap ( DIB ). As we discuss in
detail later in this chapter, there are six basic DIB formats supported in the various versions of
7 Fully-qualified name: System.Drawing.Bitmap .
Chapter 15 – Compact Framework Graphics Page 5
11/7/2003
Copyright © 2002-2003 Paul Yao & David Durant
 
Zgłoś jeśli naruszono regulamin