none
WPF中,MediaElement控件截取视频的图像,怎么是空白的? RRS feed

  • 问题

  •  MediaElement ME            
                string path =  System.Windows.Forms.Application.StartupPath + System.DateTime.Now.ToString("yyyyMMdd")+ ".png";              
                using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write))
                 {
                        int marg = int.Parse(ME.Margin.Left.ToString());
                        int m_width = (int)ME.ActualWidth;
                        int m_height = (int)ME.ActualHeight;
                        RenderTargetBitmap rtb = new RenderTargetBitmap(m_width, m_height, 96, 96, PixelFormats.Pbgra32);
                        rtb.Render(ME);
                        PngBitmapEncoder encoder = new PngBitmapEncoder();
                        encoder.Interlace = PngInterlaceOption.Off; 
                        encoder.Frames.Add(BitmapFrame.Create(rtb));
                        encoder.Save(file);
                        file.Close();
                  }
    使用这个 RenderTargetBitmap 怎么获取的图像时空白的???怎么截取MediaElement控件的当前播放视频的图像啊
    2014年10月23日 9:34

答案

  • 你好,

    >>使用这个 RenderTargetBitmap 怎么获取的图像时空白的???怎么截取MediaElement控件的当前播放视频的图像啊

    你的问题出在这一行代码:rtb.Render(ME);

    为了得到正确的视频图像,需要使用VisualBrush 创建 DrawingVisual 对象,然后再用RenderTargetBitmap.Render() 渲染, 否则就会是空白图像,可见这篇博文:http://blogs.msdn.com/b/jaimer/archive/2009/07/03/rendertargetbitmap-tips.aspx 

    以下是示例代码:

    扩展方法:

    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)));
                }
                renderTarget.Render(drawingVisual);
    
                JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
                jpgEncoder.QualityLevel = quality;
                jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
    
                Byte[] imageArray;
    
                using (MemoryStream outputStream = new MemoryStream())
                {
                    jpgEncoder.Save(outputStream);
                    imageArray = outputStream.ToArray();
                }
                return imageArray;
            }
    } 

    使用此扩展方法:

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


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年10月24日 9:41
    版主