Book Overview

The Windows Presentation Foundation (WPF) is a next generation graphics platform that is part of .NET 3.0 and .NET 3.5. It allows you to build advanced user interfaces that incorporate documents, media, 2D and 3D graphics, animations, and web-like characteristics. Practical WPF Graphics Programming provides all the tools you need to develop professional graphics applications using WPF and C# based on the .NET framework. I hope this book would be useful for WPF and C# programmers of all skill levels.

The book is written with the intention of providing you with a complete and comprehensive explanation of the WPF graphics capability, and pays special attention to the code implementation details, which will be useful when you create your own real-world WPF graphics Applications. It includes over 120 code examples, which cover a broad array of topics on WPF graphics programming. The book shows you how to create a variety of graphics, ranging from simple 2D shapes to complex 3D surfaces and interactive 3D models. You can learn from this book how to create a full range of 2D and 3D graphics applications and how to implement custom 3D geometries and shapes that can be reused in your WPF projects.

What this Book Includes

This book and its sample code listings provide you with:

Is this Book for You?

You do not have to be an experienced WPF developer or an expert to use this book. I designed this book to be useful to people of all levels of WPF programming experience. In fact, I believe that if you have some experience with the programming language C#, Windows Forms, HTML, and the .NET framework, you will be able to sit down in front of your computer, start up Microsoft Visual Studio 2008 and .NET 3.5, follow the examples provided in this book, and quickly become familiar with WPF graphics programming. For those of you who are already experienced WPF developers, I believe this book has much to offer as well. There is a great deal of information in this book about graphics programming not available in other WPF tutorial and reference books. In addition, most of the example programs in this book can be used directly in your own real-world application development. This book will provide you with a level of detail, explanation, instruction, and sample program code that will enable you to do just about anything WPF graphics-related.

The majority of the example programs in this book can be used routinely by WPF developers and technical professionals. Throughout the book, I will emphasize the usefulness of WPF graphics programming to real-world applications. If you follow the instructions presented in this book closely, you will be able to easily develop various practical WPF graphics applications, from 2D graphics and charts to a sophisticated 3D model libraries. At the same time, I will not spend too much time discussing programming style, execution speed, and code optimization, because there is a plethora of books out there that already deal with such topics. Most of the example programs you will find in this book omit error handlings. This makes the code easier to understand by focusing only on the key concepts and practical applications.

How this Book Is Organized

This book is organized into fourteen chapters, each of which covers a different topic about WPF graphics programming. The following summaries of each chapter should give you an overview of the book?s content:

Chapter 1, Overview of WPF Programming.
This chapter introduces the basics of WPF and reviews some of the general aspects of WPF programming, including XAML files used to define user interfaces.

Chapter 2, WPF Graphics Basics in 2D.
This chapter reviews some fundamental concepts of the 2D graphics and the 2D drawing model in WPF. It introduces coordinate systems and basic 2D shapes.

Chapter 3, 2D Transformations.
This chapter covers the mathematical basics for 2D graphics programming. 2D vectors, matrices, and transformations in the homogeneous coordinate system, including translation, scaling, reflection, and rotation, are discussed. These 2D matrices and transformations allow WPF applications to perform a wide variety of graphical operations on graphics objects in a simple and consistent manner.

Chapter 4, Geometry and 2D Drawing.
This chapter introduces WPF?s Geometry classes and demonstrates why you need them to create complex 2D graphics objects. It also shows you how to create interactive 2D drawing programs and custom shapes.

Chapter 5, Colors and Brushes.
This chapter covers the color system and brushes that WPF uses to paint graphics objects. It introduces a variety of brushes and their transformations. You?ll learn how to create exotic visual effects using different brushes, including the gradient, tile, and image brushes.

Chapter 6, Animation.
This chapter describes WPF animation facilities, which allow most of the properties and transformations of the graphics objects (such as position, size, translation, rotation, etc.) to be animated. It also describes how to create a custom animation class that can be used in physics-based animation.

Chapter 7, Physics and Games in WPF.
This chapter covers topics related to real-world WPF applications. You?ll learn how to create and simulate physics models by solving ordinary differential equations with the Runge-Kutta method, and how to incorporate physics models into real-world games in WPF. This chapter discusses several physics models and games, including a pendulum, a coupled spring system, a golf ball (projectiles), ball collision, and fractals.

Chapter 8, Charts in WPF.
This chapter contains instructions on creating 2D line charts in WPF. It introduces basic chart elements including the chart canvas, text canvas, axes, title, labels, ticks, and legend. From this chapter, you?ll also learn how to put a 2D chart application into a custom user control and how to reuse this control in your WPF applications.

Chapter 9, 3D Transformations.
This chapter extends the concepts described in Chapter 3 into the third dimension. It explains how to define 3D graphics objects and how to translate, scale, reflect, and rotate these 3D objects. It also describes transformation matrices that represent projection and transformations, which allow you to view 3D graphics objects on a 2D screen. You?ll also learn how WPF defines 3D vectors, matrices, and projections.

Chapter 10, WPF Graphics Basics in 3D.
This chapter explores the basics of 3D models in WPF. It introduces Viewport3D, the 3D geometry and the mesh model, lighting, camera, etc. You?ll also learn how to create basic 3D shapes directly in WPF.

Chapter 11, Custom 3D Geometries.
This chapter explains how to create custom geometries for various 3D shapes. These custom geometry classes can be used as resources in XAML files, and these resources can be used in your markup with a data binding.

Chapter 12, Custom 3D Shapes.
This chapter shows how to implement custom 3D shape classes. Unlike custom 3D geometry classes, which can only be used as shareable resources, these custom 3D shape classes can be used directly in your XAML files in the same way as the 2D shapes, such as the Line, Rectangle, and Ellipse. You can use these custom 3D shape classes to create a powerful 3D model library.

Chapter 13, 3D Surfaces.
This chapter explains how to create various 3D surfaces, from simple surfaces to complex surfaces, using rectangular meshes and different techniques, including parametric, extrusion, and revolution approaches. It also describes how to add lighting and shading effects to these surfaces.

Chapter 14, 3D Model Manipulation.
This chapter covers broad array of topics on manipulating 3D models in WPF. It describes how to create various 3D special effects using different materials, different light sources, and texture maps. It also explains how to place interactive 2D elements on 3D surfaces and how to rotate 3D graphics objects with the mouse using the virtual trackball method.