(1 item) |
|
(1 item) |
|
(5 items) |
|
(1 item) |
|
(1 item) |
|
(2 items) |
|
(2 items) |
|
(4 items) |
|
(1 item) |
|
(6 items) |
|
(2 items) |
|
(4 items) |
|
(1 item) |
|
(4 items) |
|
(2 items) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(2 items) |
|
(2 items) |
|
(5 items) |
|
(3 items) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(3 items) |
|
(1 item) |
|
(1 item) |
|
(2 items) |
|
(8 items) |
|
(2 items) |
|
(7 items) |
|
(2 items) |
|
(2 items) |
|
(1 item) |
|
(2 items) |
|
(1 item) |
|
(2 items) |
|
(4 items) |
|
(1 item) |
|
(5 items) |
|
(1 item) |
|
(3 items) |
|
(2 items) |
|
(2 items) |
|
(8 items) |
|
(7 items) |
|
(3 items) |
|
(7 items) |
|
(6 items) |
|
(1 item) |
|
(2 items) |
|
(5 items) |
|
(5 items) |
|
(7 items) |
|
(3 items) |
|
(7 items) |
|
(16 items) |
|
(10 items) |
|
(27 items) |
|
(15 items) |
|
(15 items) |
|
(13 items) |
|
(16 items) |
|
(15 items) |
One of the restrictions I've often run into with GDI+ is that it makes life awkward
if you want to work with raw pixel values. (E.g., for generating images from scratch
or performing image processing.) It's possible, your two options being (1) the
GetPixel
and SetPixel
methods, or (2) LockBits
and
UnlockBits
. The first option is abominably slow if you're dealing with large
volumes of pixels - you call the methods for every single pixel. The second approach requires unsafe code, which rules out
partial trust scenarios, and leaves me with an uneasy tense feeling - I prefer to write verifiable code whenever possible.
In WPF, things are better.
WPF lets you pass in a big array of pixel values to create a new
BitmapSource
. It also offers CopyPixel
methods that copies
pixel data out of the bitmap into an array.
This means you can write verifiable code that generates or process pixel data efficiently. Here's a very simple example:
double dpi = 96; int width = 128; int height = 128; byte[] pixelData = new byte[width*height]; for (int y = 0; y < height; ++y) { int yIndex = y * width; for (int x = 0; x < width; ++x) { pixelData[x + yIndex] = (byte) (x + y); } } BitmapSource bmpSource = BitmapSource.Create(width, height, dpi, dpi, PixelFormats.Gray8, null, pixelData, width);
This creates a BitmapSource
of a linear gradient fill from black to white
from top left to bottom right. Yes, there are much easier ways to do that in WPF, but
the point here is to illustrate how you can take raw pixel data and turn it into
a BitmapSource
.
Once you've got a BitmapSource
you can then use it, say, to set the Source
property of an Image
element in order to display the bitmap:
<Image x:Name="imageDisplay" /> ... imageDisplay.Source = bmpSource;
And of course you can use with an ImageBrush
, or do all the other things
you can do with bitmaps in WPF.