Drawing on Video within C#

2019-04-04 22:49发布

I am making an application that will allow users to apply certain tools to analyse videos & images. I need help with how i actaully draw/write on the video loaded into windows media player within my form and being able to save it on. It needs to be able to lert the user draw freehand and shapes on it. Thanks in Advance,

Chris :)

6条回答
来,给爷笑一个
2楼-- · 2019-04-04 23:25

This is a non-trivial, if not impossible task to accomplish with the wmp control in winforms.

I don't know of any way to actually draw on the wmp but you could draw on a transparent panel overlaid over the wmp. This will not work will the video is playing but you can show the drawing while it is paused. I have used this technique to draw over a 3rd party video control that works similarly to wmp.(Edit - this does not seem to work with the wmp control)

However, as real transparent panels are also rather tricky in winforms, another way would be to grab an image from the video and draw on the overlaid image. Again, only when it is paused.

This commercial control does enable drawing over the video. It has an event that fires every frame that you can use to do the drawing. The big downside, though is that you can't really do anything too fancy as your drawing routine needs to finish before the next frame is drawn.

I would strongly encourage you to use WPF(even if its a wpf control hosted within a winforms app) to show your video. It is a whole lot easier to draw on video(including playing video) in wpf.

EDIT

I just tested drawing over the wmp using a transparent panel and its doesn't behave as my 3rd party control did,so I suggest you do the video playing bit in WPF and host that in your winforms app. (I just tested that too using @Callums inkcanvas suggestion and it works like a charm)

查看更多
相关推荐>>
3楼-- · 2019-04-04 23:32

If you are using WPF, try placing an InkCanvas on top of your video and setting the Background to transparent. You can then save and load up the shapes the users draw on top of the video.

A little proof-of-concept with a picture instead of a video:

alt text

I suspect you may be using WinForms though, where this may be more difficult. If so, a good excuse to learn WPF!


EDIT: With WinForms, you would have to make your own custom control that acts as a transparent overlay and add brush strokes to it. It would be extremely hard to implement well (with transparent background, which doesn't play well with WinForms). I would recommend using WPF if you are still at a stage you can change your application's UI. WPF works on XP and up.


EDIT2: After googling, there are some InkCanvas equivalents that people have made for WinForms, but I have no idea how good they are and may not support transparent backgrounds.

You could always have the video that you want annotated in a new WPF window and the rest of your application in WinForms.

查看更多
叛逆
4楼-- · 2019-04-04 23:32

This can be done in WinForms but it is not easy. There is transparent form support with alpha blending in WinForms. Use the following CreateParams for the transparent overlay form: WS_EX_LAYERED, WS_EX_TRANSPARENT. Check the MSDN references for this type of window: http://msdn.microsoft.com/en-us/library/ms997507.aspx, http://msdn.microsoft.com/en-us/library/ms632599%28VS.85%29.aspx#layered.

Put a transparent form above your video control and you can draw anything you want on it. Move and resize events need to be coordinated between your video window and the transparent form above it. Redrawing the overlay needs to use UpdateLayeredWindow() in user32.dll.

I learned quite a bit from this example: http://www.codeproject.com/Articles/13558/AlphaGradientPanel-an-extended-panel.

查看更多
三岁会撩人
5楼-- · 2019-04-04 23:38

I have found how to do this.
Here is one way in WPF using Canvas

private void buttonPlayVideo_Click(object sender, RoutedEventArgs e)
{
    Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
    dlg.Filter = "All Files|*.*";
    Nullable<bool> result = dlg.ShowDialog();
    if (result == true) {
        MediaPlayer mp = new MediaPlayer();
        mp.Open(new Uri(filename));
        VideoDrawing vd = new VideoDrawing();
        vd.Player = mp;
        vd.Rect = new Rect(0, 0, 960, 540);
        DrawingBrush db = new DrawingBrush(vd);
        canvas.Background = db;
        mp.Play();
    }
}

then create mouse events for Canvas and draw with it

Point startPoint, endPoint;
private void canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
    startPoint = e.GetPosition(canvas);
}
private void canvas_MouseUp(object sender, MouseButtonEventArgs e)
{
    endPoint = e.GetPosition(canvas);

    Line myLine = new Line();
    myLine.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
    myLine.X1 = startPoint.X;
    myLine.Y1 = startPoint.Y;
    myLine.X2 = endPoint.X;
    myLine.Y2 = endPoint.Y;
    myLine.HorizontalAlignment = HorizontalAlignment.Left;
    myLine.VerticalAlignment = VerticalAlignment.Center;
    myLine.StrokeThickness = 2;
    canvas.Children.Add(myLine);
}
查看更多
smile是对你的礼貌
6楼-- · 2019-04-04 23:47

Ok, by far and away the best way of doing this is to use Silverlight. Silverlight supports all of the major streaming formats and also provides complete access to the framebuffer.

Easy :-)

查看更多
你好瞎i
7楼-- · 2019-04-04 23:48

You might look at XNA (www.xna.com) from Microsoft. It is made for managed languages like c# and should support video.

I've only used it for drawing in c#, but it gets the job done.

I should also note that XNA will function as part of a regular Windows Forms app. For what it's worth, I have also prototyped something like this with Flash; Flash allows you to import each frame of the movie file into the editor and create a SWF that can respond to user interaction.

However, this approach is useless if you need to update the movie in real-time. Flash (last I checked) could only import the movie at design time.

查看更多
登录 后发表回答