WPF tips-convert Canvas to a bitmap

Source: Internet
Author: User

In WPF, we can regard Canvas as a Canvas, and regard controls in Canvas as elements to convert them into bitmap files:

Effect:

 

Fig 1.1

You can set the width, height, and color of the Canvas to generate any image you want. Display the effect you set in real time.

This includes creating special effects such as shadows.

 

WPF provides the RenderTargetBitmap class to render any container control into a bitmap.

 

Create a new WPF project and create a CANVAS on the page, as shown below:

1 <Canvas x: Name = "Screen" Width = "700" Height = "200" Background = "# F0CC0000">
2 <TextBlock Canvas. Left = "200" Canvas. Top = "50" x: Name = "VSSize" text = "Canvs convert to image"> </TextBlock>
3 </Canvas>

 

Process in CS code:

1 RenderTargetBitmap bmp = new RenderTargetBitmap (this. Screen. Width, this. Screen. Height, 96, 96, PixelFormats. Pbgra32 );
2 bmp. Render (this. Screen );
3 string file = @ "c: \ xxx.jpg ";
4 string Extension = System. IO. Path. GetExtension (file). ToLower ();
5 BitmapEncoder encoder = new BitmapEncoder ();
6 encoder. Frames. Add (BitmapFrame. Create (bmp ));
7 using (Stream stm = File. Create (file ))
8 {
9 encoder. Save (stm );
10}

 

In this way, the CANVAS is converted into a 1.1 effect;

If you have higher definition requirements for the generated image, you can set the QualityLevel attribute of encoder to change the JPEG quality value, or generate a PNG Image with higher quality, such

 

1 encoder = new PngBitmapEncoder ();

 

 

We changed some attributes of CANVAS, put a CANVAS named SCREEN in another CANVAS, and set the offset of 50 as follows:

 

Code

<Canvas>
<Canvas. Top = "50" x: Name = "Screen" Width = "700" Height = "200" Background = "# F0CC0000">
<TextBlock Canvas. Left = "200" Canvas. Top = "50" x: Name = "VSSize" text = "Canvs convert to image"> </TextBlock>
</Canvas>
</Canvas>

 

The CS code remains unchanged in the background;

The effect is as follows:

A black block is displayed on the image. If you place the image in PHOTOSHOP, you can see that the offset of 50 is a transparent block, which proves that any offset of the attribute will affect the CANVAS diagram.

 

Can I directly create a CANVAS in the background CS file to generate a bitmap directly? As follows:

1 Canvas cvs = new Canvas ();
2 cvs. Width = 700;
3 cvs. Height = 200;
4 Label lb = new Label ();
5 lb. content = "Convert Canvas to image ";
6 Canvas. SetTop (lb, 50 );
7 Canvas. Setleft (lb, 200 );
8 cvs. child. add (lb );
9 RenderTargetBitmap bmp = new RenderTargetBitmap (cvs. Width, cvs. Height, 96, 96, PixelFormats. Pbgra32 );
10 bmp. Render (cvs );
11 string file = @ "c: \ xxx.jpg ";
12 string Extension = System. IO. Path. GetExtension (file). ToLower ();
13 BitmapEncoder encoder = new BitmapEncoder ();
14 encoder. Frames. Add (BitmapFrame. Create (bmp ));
15 using (Stream stm = File. Create (file ))
16 {
17 encoder. Save (stm );
18}

 

Run the code to save the saved image as a 700*200 blank image, which means that the container directly constructed in the background cannot be converted into a bitmap directly.

Solution:

The RenderTargetBitmap. Render object is a Visual object, and all interface elements inherit from Visual objects.

We can create a virtual canvas object, such as DrawingVisual drawingVisual = new DrawingVisual ();

Based on this, use the DrawingContext object to recompose the Canvas and its Child objects on the DrawingVisual virtual Canvas. Then Render DrawingVisual can generate a bitmap.

Example:

 

Code

Canvas cvs = new Canvas ();
Cvs. Width = 700;
Cvs. Height = 200;
Label lb = new Label ();
Lb. content = "Convert Canvas to image ";
Canvas. SetTop (lb, 50 );
Canvas. Setleft (lb, 200 );
Cvs. child. add (lb );

DrawingVisual drawingVisual = new DrawingVisual ();
DrawingContext drawingContext = drawingVisual. RenderOpen ();

// Construct a rectangle
Rect rect = new Rect (new System. Windows. Point (0, 0), new System. Windows. Point (cvs. ActualWidth, cvs. ActualHeight ));
// Draw a rectangle
DrawingContext. DrawRectangle (cvs. Background, new System. Windows. Media. Pen (), rect );
// Draw text
DrawingContext. DrawText (new FormattedText (), new System. Windows. Point (Canvas. GetLeft (lb), Canvas. GetTop (lb )));

DrawingContext. Close ();

RenderTargetBitmap bmp = new RenderTargetBitmap (cvs. Width, cvs. Height, 96, 96, PixelFormats. Pbgra32 );
// Render DrawingVisual
Bmp. Render (drawingVisual );
String file = @ "c: \ xxx.jpg ";
String Extension = System. IO. Path. GetExtension (file). ToLower ();
BitmapEncoder encoder = new canonical BitmapEncoder ();
Encoder. Frames. Add (BitmapFrame. Create (bmp ));
Using (Stream stm = File. Create (file ))
{
Encoder. Save (stm );
}

 

 

 

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.