In WPF, how do I use image APIs for rendering instead of XAML?

Source: Internet
Author: User
Tags drawtext

--------------------------------------------------------------------------------
Reprinted with the following information:
Da Keshan[MSN: a3news (AT) hotmail.com]
Online image processing of http://www.zpxp.com radish rat
--------------------------------------------------------------------------------

First, since WPF does not have graphics objects like GDI +, you cannot use graphics for plotting. Instead, it is replaced by drawingcontext. Similarly, onpaint in GDI + has been replaced by onrender.
Second, uielement has an onrendar method, which is defined:
Protected virtual void onrender (
Drawingcontext
)
However, we cannot directly call the onrender method or directly create a drawingcontext instance, but we can use drawinggroup. Open and drawingvisual. renderopen.
Here are two examples:
(1) custom canvas:
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Windows. controls;
Using system. Windows. Media;
Using system. windows;
Using system. Globalization;

Namespace brawdraw. com. Test
{
Class canvascustompaint: canvas
{
Protected override void onrender (drawingcontext DC)
{
Base. onrender (DC );
// Draw a rectangle
DC. drawrectangle (brushes. Red, new pen (brushes. Blue, 1 ),
New rect (new point (20,20), new size (100,100 )));
// Draw text
DC. drawtext (New formattedtext ("Hello, world! ", Cultureinfo. currentculture,
Flowdirection. lefttoright, new typeface ("Arial"), 40, brushes. Orange ),
New Point (50, 60 ));
}
}
}
(2) Save the image to a file:
Protected void savephoto (string filename)
{
Drawingvisual = new drawingvisual ();
Drawingcontext = drawingvisual. renderopen ();
// Draw a rectangle
Rect = new rect (new point (160,100), new size (320, 80 ));
Drawingcontext. drawrectangle (brushes. lightblue, (PEN) null, rect );
// Draw text
Drawingcontext. drawtext (
New formattedtext ("Hello, world ",
Cultureinfo. getcultureinfo ("En-us "),
Flowdirection. lefttoright,
New typeface ("verdana "),
36, brushes. Black ),
New Point (100, 60 ));

Drawingcontext. Close ();

// Use the rendertargetbitmap object to save the image
Rendertargetbitmap renderbitmap = new rendertargetbitmap (INT) This. Width, (INT) This. Height, 96, 96, pixelformats. pbgra32 );
Renderbitmap. Render (drawingvisual );

// Use bitmapencoder to encode the image for saving
Required bitmapencoder encoder = new required bitmapencoder ();
Encoder. frames. Add (bitmapframe. Create (renderbitmap ));
// Save the file
Filestream = new filestream (filename, filemode. Create, fileaccess. readwrite );
Encoder. Save (filestream );
// Close the file stream
Filestream. Close ();
}

The last paragraph (http://blogs.msdn.com/timothyc/archive/2006/06/16/634638.aspx) attached here, in addition to the key text highlighted in orange, to provide as is:
Adding seemingly simple tweaks (e.g., clipping, bitmap effects) to our scene causes us to fall back to software, and software Rending in WPF is slower than GDI + software rendering.

First,The WPF software rendering code is derived from the GDI + codebase. There are certain limits to what can be accomplished in hardware, andWe have to work around und what the hardware vendors give us. As graphics hardware evolves, those limits are likely to become better over time. If at leastSomePortion of your scene is rendered in hardware, the costRenderingIs already going to be faster than it was in GDI +. Finally, we shipped a tool at the PDC called 'aggregator' to help identify where software rendering occurs.

(Note the orange text part)

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.