WPF and Silverlight Learning notes (28): Use of basic graphics (3) operation of Graphics

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.