WPF Tip-canvas to bitmap

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/tmywu/archive/2010/09/14/1825650.html

In WPF, we can think of canvas as a canvas, with controls in the canvas as elements, and turning them into bitmap files:

The following effects

Figure 1.1

You can set the canvas's width, height, and color type to create any image you want. Real-time rendering of the styles you've set, among other effects.

This includes creating some special effects such as shadows.

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

Create a new WPF project, creating a canvas in the page, as follows:

1 <canvas x:name= "screen" width= "height=" background= "#F0CC0000" >
2 <textblock canvas.left= "canvas.top=" x:name= "Vssize" text= "Canvs convert to Pictures" ></TextBlock>
3 </Canvas>

Do the processing in CS code:

RenderTargetBitmap BMP =NewRenderTargetBitmap ( This. Screen.width, This. Screen.height, the, the, PIXELFORMATS.PBGRA32); Bmp. Render ( This.     screen); stringFile =@"c:\xxx.jpg"; stringExtension =System.IO.Path.GetExtension (file).     ToLower (); Bitmapencoder Encoder=NewJpegbitmapencoder (); Encoder.     Frames.add (Bitmapframe.create (BMP)); using(Stream stm =file.create (File)) {Encoder.    Save (STM); }

This transforms the canvas into the effect of Figure 1.1;

If you have higher clarity requirements for the generated images, you can set the Qualitylevel property of the encoder to change the JPEG quality value, or produce a higher-quality PNG image, such as

1 encoder = new Pngbitmapencoder ();

Let's change some of the properties of the canvas, place a canvas named screen in another canvas and set the offset 50, set as follows:


<Canvas>
<canvas canvas.top= "" "x:name=" screen "width=" "height=" "background=" #F0CC0000 ">
<textblock canvas.left= "canvas.top=" x:name= "Vssize" text= "Canvs convert to Pictures" ></TextBlock>
</Canvas>
</Canvas>

The background CS code does not change;

The effect is as follows:

A black block appears on the picture, and the image is placed in Photoshop to see that the offset 50 is a transparent block, proving that any attribute offsets will affect the composition of the canvas.

Then directly in the background CS file to build a canvas directly generated bitmap is it possible? As follows:

Canvas CVS = new canvas ();Cvs. Width = the; CVs. Height= $; Label lb=NewLabel (); Lb.content="Canvas converted to picture"; Canvas.settop (LB, -); Canvas.setleft (LB, $); Cvs.child.add (LB); RenderTargetBitmap BMP=NewRenderTargetBitmap (CVS). Width, CVS. Height, the, the, PIXELFORMATS.PBGRA32); Bmp.     Render (CVS); stringFile =@"c:\xxx.jpg"; stringExtension =System.IO.Path.GetExtension (file).     ToLower (); Bitmapencoder Encoder=NewJpegbitmapencoder (); Encoder.     Frames.add (Bitmapframe.create (BMP)); using(Stream stm =file.create (File)) {Encoder.    Save (STM); }

Run the code, save the image as an empty map of 700*200, indicating that the container directly in the background cannot be converted directly to the bitmap.

Workaround:

the Rendertargetbitmap.render object is a visual object, and interface elements are inherited from the visual object.

We can build a virtual canvas object, such as drawingvisual drawingvisual = new DrawingVisual ();

Based on this, the DrawingContext object is used to re-compose the objects in the canvas and its child on the drawingvisual virtual canvas, and then render drawingvisual to generate a bitmap.

Example:

Canvas CVS =NewCanvas (); CVs. Width= the; CVs. Height= $; Label lb=NewLabel (); Lb.content="Canvas converted to picture"; Canvas.settop (LB, -); Canvas.setleft (LB, $); Cvs.child.add (lb);D rawingvisual drawingvisual=Newdrawingvisual ();D rawingcontext drawingcontext=Drawingvisual.renderopen ();//Constructs a rectangleRect rect =NewRect (NewSystem.Windows.Point (0,0),NewSystem.Windows.Point (CVS). ActualWidth, CVS. ActualHeight));//Draw a rectangleDrawingcontext.drawrectangle (CVS). Background,NewSystem.Windows.Media.Pen (), rect);//Drawing TextDrawingcontext.drawtext (NewFormattedText (),NewSystem.Windows.Point (Canvas.getleft (LB), canvas.gettop (lb)));d rawingcontext.close (); RenderTargetBitmap BMP=NewRenderTargetBitmap (CVS). Width, CVS. Height, the, the, PIXELFORMATS.PBGRA32); //Render drawingvisualbmp.     Render (drawingvisual); stringFile =@"c:\xxx.jpg"; stringExtension =System.IO.Path.GetExtension (file).     ToLower (); Bitmapencoder Encoder=NewJpegbitmapencoder (); Encoder.     Frames.add (Bitmapframe.create (BMP)); using(Stream stm =file.create (File)) {Encoder.    Save (STM); }

WPF Tip-canvas to bitmap

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.