A recent project on Windows,Customer requirements to do the "flip" a little,so,Dynamic window background is always essential content (such as mobile phones qq login screen, right?
I thought this is a very easy to implement functionality so keen on the idea down ...... yes,In winform in,Not so easy to achieve the effect you want the。
Of course,We think that if you want to play video,Then the system comes with media player,Just add a control that can handle - the result is either a button to play video blocked,Or it is always first,The button block ......
So the,Want to achieve play video,Or to use honest pictureBox 。
Then this is to use so-called mciSendString Technique,For example, this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
private class LibWrap { [DllImport(("winmm.dll"), EntryPoint = "mciSendString", CharSet = CharSet.Auto)] public static extern int mciSendString(string lpszCommand, string lpszReturnString, uint cchReturn, IntPtr hwndCallback); } private void PlayViedo() { PictureBox PlayScreen = new PictureBox(); PlayScreen = this.pictureBox; string mciCommand; string path = string.Empty; path = System.Windows.Forms.Application.StartupPath + @"\Background"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } path += @"\bg.wmv"; mciCommand = "open " + path + " alias MyAVI"; mciCommand = mciCommand + " parent " + PlayScreen.Handle.ToInt32() + " style child"; LibWrap.mciSendString(mciCommand, null, 0, IntPtr.Zero); Rectangle r = PlayScreen.ClientRectangle; mciCommand = "put MyAVI window at 0 0 " + r.Width + " " + r.Height; LibWrap.mciSendString(mciCommand, null, 0, IntPtr.Zero); // LibWrap.mciSendString("play MyAVI", null, 0, IntPtr.Zero); LibWrap.mciSendString("play MyAVI repeat", null, 0, IntPtr.Zero); } |
But this can not be used,Even if the official code copied,You can not get the right result - just do not play your temper?
Eventually,I still use OpenCV To achieve this effect,of course,To use C #,We have to find the OpenCV .net package,That is EmguCV。
Here I will look at the implementation process roughly record,First you go to the library to download Emgu install it,Address here:https://sourceforge.net/projects/emgucv/files/emgucv/3.2/libemgucv-windesktop-3.2.0.2682.exe/download
You should pay attention to the downloaded version,If a newer version is,Please download the new version。
principle
In fact, the principle of using emgu and play with pictureBox video above should be consistent,The video frames are acquired in time,Then show a picture Bale。Another benefit of using emgu is that it can be made so that any video,For instance, your mp4,Or streaming address。
Environment Configuration
After installing the download,The default installation location for the: C:\Emgu\emgucv-windesktop 3.2.0.2682\bin Specific version numbers may change。Now we open vs,Import emgu in it:
-
- Point to open your program project,The midpoint of the right mouse button in the "Design" mode on the left side of the "toolbox",Select "option…」;
- In the dialog box below the mid-point "Browse",Find the installation location of emgu,Select Emgu.CV.UI.dll、 Zedgraph.dll Both were open;
- VS will automatically import controls and check these emgu,Be determined;
- In the "Solution Explorer" on the right side of the project,Find the "reference",In the inside right click select "Add Reference";
- On the left side of the pop-up dialog box select "Browse",And then manually import Emgu.CV.UI.dll、 Zedgraph.dll 、 Emgu.CV.UI.GL.dll 、 Emgu.CV.World.dll These four documents。
Such,Environment has configured the。
Using controls
This time you can use the drag controls from the toolbox in the emgu,Here we use the emgu ImageBox ,After dragged named it VideoBox spare。
Here we will use the class variable,Note that the statement in the class:
1 2 3 |
VideoCapture camCapter = null; Mat frame; System.Windows.Forms.Timer PlayerTimer = new System.Windows.Forms.Timer(); |
When you want to play video,To do so:
1 2 3 4 5 6 7 8 |
camCapter = new VideoCapture(videoUrl); VideoBox.Dock = DockStyle.Fill; camCapter.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.PosFrames, 2241); var tmp = (camCapter.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps) > 0) ? (camCapter.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps)) : 24; int delay = (int)(1000 / tmp); PlayerTimer.Interval = delay; PlayerTimer.Tick += VideoController; PlayerTimer.Start(); |
There are two points to note:
- The first 4 Line acquired the video frame rate,Not all videos can get to the right,So if you can not get,We gave a default 24 fps,of course,You can also default 25 or 30;
- The first 8 Note that the line must call starts,Otherwise, the video will not play,The timer does not start automatically。
Now let us look at video Controller Realization,Here is the most important,Because the timer polls the controller at a specified time,Specific display operation is performed by the controller of this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
void VideoController(object sender, EventArgs e) { if (camCapter == null) { return; } if (frame != null) { frame.Dispose(); } if (VideoPlayNeedsStop) { frame = null; PlayerTimer.Stop(); } frame = camCapter.QueryFrame(); if (frame == null) { Console.WriteLine("Video End"); //camCapter = new VideoCapture(videoUrl); //PlayerTimer.Stop(); return; } VideoBox.Image = frame; } |
Note that if you want to play continuously (repeat loop),Then re-initialize a highlight in a row VideoCapture Can。
Join transparent background controls
of course,Finally, we also added controls,For example, a Button ,What you need to have a set of transparent png as a button,Then do the following configuration:
1 2 3 4 5 6 7 8 |
this.ll.Text = ""; this.ll.Image = ScreenController.ScreenController.shared.GetImageFrom("ui_images\\liulan.png"); this.ll.BackgroundImageLayout = ImageLayout.None; this.ll.FlatAppearance.BorderSize = 0; this.ll.FlatAppearance.MouseDownBackColor = Color.Transparent; this.ll.FlatAppearance.MouseOverBackColor = Color.Transparent; this.ll.FlatStyle = FlatStyle.Flat; this.ll.BackColor = Color.Transparent; |
Here ll Is one of my Button ,In short,After such a configuration,The button is transparent,But you will find that although it is overlaid on top of video,But clearly transparent to the background or the window itself instead of displaying the contents of its next video。
1 |
this.ll.Parent = VideoBox; |
Then we can manually adjust the button of the parent for this our VideoBox ,Such,It can display the contents of the video。
dynamic link database
At last,You also need to rely on run-time dynamic link library copied to the run directory of the program,This is divided into 32-bit and 64-bit,If you run debug,To copy files in a directory 32: C:\Emgu\emgucv-windesktop 3.2.0.2682\bin\x86
References
- OpenCV
- Emgu CV: OpenCV in .NET (C#, VB, C++ and more)
- How to use PictureBox to play video
- Studies with [] Emgu EmguCV (a) configuration and using
- VS2010 + C # + EmguCV read and record video
- [emguCV]play video—Temporary notes (unfinished)
Original article written by LogStudio:R0uter's Blog » winform using video as a window background
Reproduced Please keep the source and description link:https://www.logcg.com/archives/2893.html