Video in WPF -- (3)

Source: Internet
Author: User
If you want Snapshoot Save as an image or Image It is not impossible to display the control content.

Go directlyCodeFirst, createMediaelementControl,SourceAttribute to set the video path andPositionAttribute.Seek.

<Mediaelement name = "video" loadedbehavior = "pause" opacity = "1" scrubbingenabled = "true"

Source = "C:" users "public" videos "sample Videos" bear. wmv" Position = "0: 5"/>

InButtonOfClickIn the response function,

Private void button_click (Object sender, routedeventargs E)

{

Filestream stream = file. Open ("bear.png", filemode. Create );

Rendertargetbitmap BMP = new rendertargetbitmap (INT) This. Video. actualwidth,

(INT) This. Video. actualheight, 96, 96, pixelformats. pbgra32 );

BMP. Render (this. Video );

Pngbitmapencoder coder = new pngbitmapencoder ();

Coder. interlace = pnginterlaceoption. off;

Coder. frames. Add (bitmapframe. Create (BMP ));

Coder. Save (Stream );

Stream. Close ();

}

We useRendertargetbitmapOfRenderMethod, SetVisualConvert the object to a bitmap before usingXxxbitmapencoderSave as an image file, or directlyRendertargetbitmapAssignedImageControl.

 

If we are not usingMediaelementControl, but useMediaplayer, We need to use (2) To createDrawingvisualObject, useDrawingcontextOfDrawvideoMethod To play the video.DrawingvisualConverts an object to a bitmap.

The approximate code is as follows:

Void windowreceivloaded (Object sender, routedeventargs E)

{

_ Visual = new drawingvisual ();

Drawingcontext Dc = _ visual. renderopen ();

Mediaplayer player = new mediaplayer ();

Player. Open (New uri (@ "C:" users "public" videos "sample Videos" bear. wmv "));

Player. Position = timespan. fromseconds (5 );

Player. scrubbingenabled = true;

Player. Pause ();

DC. drawvideo (player, new rect (0, 0, _ desiresize. Width, _ desiresize. Height ));

DC. Close ();

}

 

Private void button_click (Object sender, routedeventargs E)

{

Filestream stream = file. Open ("bear.png", filemode. Create );

Rendertargetbitmap BMP = new rendertargetbitmap (INT) _ desiresize. Width,

(INT) _ desiresize. Height, 96, 96, pixelformats. pbgra32 );

BMP. Render (_ visual );

Pngbitmapencoder coder = new pngbitmapencoder ();

Coder. interlace = pnginterlaceoption. off;

Coder. frames. Add (bitmapframe. Create (BMP ));

Coder. Save (Stream );

Stream. Close ();

}

 

Drawingvisual _ visual = NULL;

Size _ desiresize = new size (400,300 );

the reason why the drawingvisual object is in loaded the event is created because you need to load the video in advance. If click when an event is created, the saved bitmap is black because the video has not been loaded successfully.

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.