最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# wpf Saving images of cropped canvas in video takes much time - Stack Overflow

programmeradmin2浏览0评论

I attached video to canvas. On canvas user can draw line, rectangle etc. and these drawn object are hidden or visible according to time slice. Then user can save this video with that drawn object. I used the following code for this purpose. The issue is when I save it takes time almost equal to video length. How can I reduce this saving time or is there any other fast way save cropped canvas with drawn object

I used OpenCVSharp lib to create video from cropped canvas

Application.Current.Dispatcher.Invoke(() =>
{
    canvas.VideoPlayer.mp1.Clock.Controller.SeekAlignedToLastTick(timeSpan1, System.Windows.Media.Animation.TimeSeekOrigin.BeginTime);
});

var writer = new VideoWriter(fileName, OpenCvSharp.FourCC.DIVX, frameRate, new OpenCvSharp.Size(vidSize.Width, vidSize.Height));

for (int i = startFrame; i < endFrame; i++)
{
    CroppedBitmap croppedImage = new();

    Application.Current.Dispatcher.Invoke(() =>
    {
        canvas.VideoPlayer.mp1.Position.Add(frameTimeInterval);
        canvas.VideoPlayer.ShowHideToolsSaveAs(frame);
        // --------> some time here it shows insufficient memeory when video is around 2 mins
        RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Default);
        rtb.Render(canvas);

        // create cropped image
        croppedImage = new CroppedBitmap(rtb, vidSize);
    });

    if (croppedImage != null)
    {
        System.Drawing.Bitmap bmp = new(width, height);
        byte[] byteArray = new byte[1];

        Application.Current.Dispatcher.Invoke(() =>
        {
            bmp = BitmapFromSource(croppedImage);
            byteArray = BitmapToByteArray(bmp);
        });

        Mat mat = new();
        mat = BitmapConverter.ToMat(bmp);
        writer.Write(mat);
    }
发布评论

评论列表(0)

  1. 暂无评论