在 C# 中绘制视频
我正在制作一个应用程序,允许用户应用某些工具来分析视频和视频。图像。我需要帮助来了解如何在表单中加载到 Windows Media Player 中的视频上实际绘制/写入并能够将其保存。它需要能够让用户徒手绘制并在其上绘制形状。 提前致谢,
克里斯:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 winforms 中的 wmp 控件来完成这项任务,即使不是不可能,也是不平凡的。
我不知道有什么方法可以在 wmp 上实际绘制,但您可以在覆盖在 wmp 上的透明面板上绘制。如果视频正在播放,这将不起作用,但您可以在暂停时显示绘图。我已经使用这种技术来绘制与 wmp 类似的第 3 方视频控件。(编辑 - 这似乎不适用于 wmp 控件)
但是,因为真正的透明面板也相当棘手在 winforms 中,另一种方法是从视频中抓取图像并在覆盖的图像上绘制。同样,只有当它暂停时。
此商业控件确实可以在视频上绘图。它有一个事件,可以触发您可以用来绘图的每一帧。但最大的缺点是,您实际上无法做任何太花哨的事情,因为您的绘图例程需要在绘制下一帧之前完成。
我强烈鼓励您使用 WPF(即使它是 winforms 应用程序中托管的 wpf 控件)来显示您的视频。在wpf中在视频上绘图(包括播放视频)要容易得多。
编辑
我刚刚测试了使用透明面板在 wmp 上绘图,它的行为与我的第 3 方控件的行为不同,所以我建议您在 WPF 和 将其托管在您的 winforms 应用程序中。 (我刚刚也使用@Callums inkcanvas 建议进行了测试,它的效果就像一个魅力)
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)
如果您使用的是 WPF,请尝试 放置 an InkCanvas 位于视频顶部并设置
背景
透明。然后,您可以保存并加载用户在视频上绘制的形状。用图片而不是视频进行一些概念验证:
不过,我怀疑您可能正在使用 WinForms,这可能会更加困难。如果是这样,这是学习 WPF 的好借口!
编辑:使用WinForms,您必须制作自己的自定义控件,充当透明覆盖层并向其添加画笔描边。很好地实现是非常困难的(透明背景,这与
WinForms)。如果您仍处于可以更改应用程序 UI 的阶段,我建议您使用 WPF。 WPF 适用于 XP 及更高版本。
编辑2:谷歌搜索后,有一些InkCanvas 等效项,但我不知道它们有多好,并且可能不支持透明背景。
您始终可以在新的 WPF 窗口中添加想要注释的视频,并在 WinForms 中添加应用程序的其余部分。
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:
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.
我已经找到了如何做到这一点。
这是 WPF 中使用 Canvas 的一种方法,
然后为 Canvas 创建鼠标事件并用它进行绘制
I have found how to do this.
Here is one way in WPF using Canvas
then create mouse events for Canvas and draw with it
这可以在 WinForms 中完成,但这并不容易。 WinForms 中提供带有 alpha 混合的透明表单支持。将以下 CreateParams 用于透明覆盖形式:WS_EX_LAYERED、WS_EX_TRANSPARENT。检查此类窗口的 MSDN 参考: http://msdn.microsoft.com /en-us/library/ms997507.aspx,http://msdn.microsoft.com/en-us/library/ms632599%28VS.85%29.aspx#layered。
在视频控件上方放置一个透明表单,您可以在上面绘制任何您想要的内容。移动和调整大小事件需要在视频窗口与其上方的透明窗体之间进行协调。重绘overlay需要使用user32.dll中的UpdateLayeredWindow()。
我从这个例子中学到了很多东西:http://www.codeproject。 com/Articles/13558/AlphaGradientPanel-an-extended-panel。
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.
您可以查看 Microsoft 的 XNA (www.xna.com)。它是为 C# 等托管语言而设计的,应该 支持视频。
我只用它在 C# 中绘图,但它可以完成工作。
我还应该指出,XNA 将作为常规 Windows 窗体应用程序的一部分运行。不管怎样,我还用 Flash 制作了类似的原型; Flash 允许您将电影文件的每一帧导入到编辑器中并创建可以响应用户交互的 SWF。
然而,如果你需要实时更新电影,这种方法就没用了。 Flash(我上次检查过)只能在设计时导入影片。
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.
好的,到目前为止,最好的方法是使用 Silverlight。 Silverlight 支持所有主要的流格式,并且还提供对帧缓冲区的完整访问。
简单的 :-)
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 :-)