最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin5PV0评论

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);
    }
Post a comment

comment list (0)

  1. No comments so far