.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Capture ScreenShot From Media Element In WPF

Posted By:Diptimaya Patra       Posted Date: April 26, 2010    Points: 25    Category: WPF    URL: http://www.dotnetspark.com  

Capture ScreenShot From Media Element In WPF. In this article we will see how can we take the screenshot of the Media (Video) we are playing.


In the previous article about Media Element in WPF, we saw some of the basic functionalities. In this article we will see how can we take the screenshot of the Media (Video) we are playing.

Crating WPF Application Project

Fire up Visual Studio 2008 and Create a WPF Application and name the project as MediaSampleWPF.


I am just extending the feature that we experienced in our last application titled "Media Element In WPF".

We will just add another Button to take ScreenShot. See following figure.


Now we will add an extension method that would help us capturing the Screen Shot.

Create a static class and add the following method to it.

public static class ScreenShot
    public static byte[] GetScreenShot(this UIElement source, double scale, int quality)
        double actualHeight = source.RenderSize.Height;
        double actualWidth = source.RenderSize.Width;
        double renderHeight = actualHeight * scale;
        double renderWidth = actualWidth * scale;

        RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)renderWidth,
            (int)renderHeight, 96, 96, PixelFormats.Pbgra32);
        VisualBrush sourceBrush = new VisualBrush(source);

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

        using (drawingContext)
            drawingContext.PushTransform(new ScaleTransform(scale, scale));
            drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0),
                new Point(actualWidth, actualHeight)));

        JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
        jpgEncoder.QualityLevel = quality;

        Byte[] imageArray;

        using (MemoryStream outputStream = new MemoryStream())
            imageArray = outputStream.ToArray();
        return imageArray;

Now in the Button click event handler add the following code to use the extension method and save the file.

#region Capture Screenshot
private void btnScreenShot_Click(object sender, RoutedEventArgs e)
    byte[] screenshot = MediaEL.GetScreenShot(1, 90);
    FileStream fileStream = new FileStream(@"Capture.jpg", FileMode.Create, FileAccess.ReadWrite);
    BinaryWriter binaryWriter = new BinaryWriter(fileStream);

Now that our application is ready to go. Run the application.


And when we capture it saves in the output directory (Bin folder of the project).


You can also download sample application used in above example.

Hope this article helps.

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend