Manipulating a bitmap programmatically
Bitmaps are used in one form or another mainly to display existing images. These typically are placed as a source of an Image
element. When a static image is added to a project and set to the Image.Source
property, a BitmapImage
is created behind the scenes. This simple class provides a read-only view to the bitmap's bits.
Sometime it's desirable to manipulate a bitmap's bit values dynamically based on a runtime algorithm. WPF provides the WriteableBitmap
that allows us to do just that. Let's look at how to use this capability.
Getting ready
Make sure Visual Studio is up and running.
How to do it...
We'll create an application that displays the famous Mandelbrot set, requiring calculating colors for each pixel in the resulting image. This will show a typical usage of WriteableBitmap
.
Create a new WPF application named
CH09.MandelbrotSet
.Open
MainWindow.xaml
. Add the followingImage
element inside the existingGrid
:<Image Width="600" Height="600" x:Name...