followed by an article that operates on a graphic in WPF, using two of the ImageSource (BitmapSource) in the previous article:
1, use BitmapImage to load Picture 2, use RenderTargetBitmap to create picture 3, use RenderTargetBitmap to modify picture
This article continues
4, use WriteableBitmap to modify the picture
In the use of RenderTargetBitmap to modify the picture, the original picture unchanged, only equivalent to the original picture based on adding a new section of content, and if the picture to make a big change rendertargetbitmap can not, I You can use WriteableBitmap to modify a picture, for example, to generate a backward bitmap:
1: <Window x:Class="WPF_28.Window1"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation& quot;
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
5: <Grid>
6: <Grid.RowDefinitions>
7: <RowDefinition Height="1*" />
8: <RowDefinition Height="1*" />
9: </Grid.RowDefinitions>
10: <Image Grid.Row="0" x:Name="sourceImage" Source="http://images.cnblogs.com/logo.gif" />
11: <Image Grid.Row="1" x:Name="targetImage" />
12: </Grid>
13: </Window>
1:private void Buildimage ()
2: {
3:bitmapimage img = new BitmapImage ();
4:img. BeginInit ();
5:
6:img. UriSource = new Uri ("Http://images.cnblogs.com/logo.gif");
7:
8:img. downloadcompleted + + Delegate
9: {
10:bitmapsource Source = new Formatconvertedbitmap (
11:IMG, pixelformats.pbgra32, NULL, 0);
12:
13:writeablebitmap bmp = new WriteableBitmap (source);
14:
15:int width = bmp. Pixelwidth;
16:int height = bmp. Pixelheight;
17:
18:int[] Pixeldata = new int[width * height];
19:int widthinbytes = 4 * width;
20:
21:bmp. CopyPixels (Pixeldata, widthinbytes, 0);
22:
23:for (int i = 0; i < pixeldata.length; i++)
24: {
25:pixeldata[i] ^= 0x00ffffff;
26:}
27:
28:bmp. WritePixels (New Int32Rect (0, 0, width, height), pixeldata, widthinbytes, 0);
29:
30:targetimage.source = BMP;
31:};
32:
33:img. EndInit ();
34:}
35:
36:private void Window_Loaded (object sender, RoutedEventArgs e)
37: {
38:buildimage ();
39:}
The results of the execution are shown in the following illustration: