WPF implements camera mirroring rollover

Source: Internet
Author: User

Previous projects need to mirror the rollover camera video and use Aforge.net to process the video.

The first consideration was to read the image control directly from Aforge.net without a frame of video and then copy it to WPF, which was basically a card, so it gave up.

Considering that the picture returned by Aforge.net is bitmap, it is intended to embed WinForm PictureBox directly in WPF to implement the video.

"Note" If embedding the WinForm window in WPF is not transparent.

"Xaml"

<window x:class= "MainWindow"
Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
Title= "MainWindow" windowstate= "Normal" background= "Black"
Windowstyle= "None" borderthickness= "0"
left= "0" top= "0" windowstartuplocation= "Manual"
XMLNS:WFI = "Clr-namespace:system.windows.forms.integration;assembly=windowsformsintegration"
Xmlns:winform= "Clr-namespace:system.windows.forms;assembly=system.windows.forms"
>
<wfi:windowsformshost x:name= "Video" >
<winform:picturebox x:name= "PB" Sizemode= "StretchImage" backcolor= "Black" forecolor= "Black"/>
</wfi:WindowsFormsHost>
</Window>

"C # code"

#region << Field >>
Private Videocapturedevice VCD;
private static Double videoresolutionwidth = 1280;
#endregion
[DllImport ("user32.dll", EntryPoint = "Setwindowlonga", SetLastError = True)]
private static extern int SetWindowLong (INTPTR hwnd, int nIndex, int dwnewlong);

[System.Runtime.InteropServices.DllImport ("User32.dll")]
public static extern long GetWindowLong (IntPtr handle, int style);
public const INT Gwl_style =-16;
public const INT Gwl_exstyle =-20;
Public Const Long ws_caption = 0x00c00000l;
Public Const Long ws_caption_2 = 0x00c0000l;
private static int videoclipxpos = 0;
private static int videoclipwidth = 0;

public static System.Drawing.Bitmap Currentphoto = null;
Public MainWindow ()
{
InitializeComponent ();
This. Loaded + = mainwindow_loaded;
This. Closing + = mainwindow_closing;
}

void Mainwindow_closing (object sender, System.ComponentModel.CancelEventArgs e)
{
Stopcamera (TRUE);
}

private void Stopcamera (BOOL flag)
{
if (flag)
Vcd. Stop ();
Else
Vcd. Start ();
}

void Mainwindow_loaded (object sender, RoutedEventArgs e)
{
This. Width = 1080;
This. Height = 1920;
This.grid.Width = 809;
This.grid.Height = 1082;
Setphotoview ();
This.pb.Width = 3413;
this.pb.Height = 1920;
This.video.Margin = new Thickness (-666, 0, 0, 0);

This. left =-(3413-1080)/2;
var videodevices = new Filterinfocollection (filtercategory.videoinputdevice);

if (Videodevices.count! = 0)
{
VCD = new Videocapturedevice (Videodevices[0]. monikerstring);
foreach (var item in VCD. Videocapabilities)
{
if (item. Framesize.width = = videoresolutionwidth)
{
Vcd. Videoresolution = Item;
Vcd. Newframe + = new AForge.Video.NewFrameEventHandler (videosource1_newframe);
Vcd. Videosourceerror + = new AForge.Video.VideoSourceErrorEventHandler (videosource1_videosourceerror);
Vcd. Start ();
Break
}
}
}
Set Window No border
IntPtr windowhandle = new Windowinterophelper (this). Handle;
Long Oldstyle = GetWindowLong (WindowHandle, Gwl_style);
SetWindowLong (WindowHandle, Gwl_style, (int) (Oldstyle & (~ (Ws_caption | ws_caption_2)));
}

private void Setphotoview ()
{
Videoclipwidth = 536;
Videoclipxpos = (1280-videoclipwidth)/2;
}

void Videosource1_newframe (object sender, AForge.Video.NewFrameEventArgs EventArgs)
{
bool Isgood = true;
System.Drawing.Bitmap img = (System.Drawing.Bitmap) eventArgs.Frame.Clone (New System.Drawing.Rectangle ( Videoclipxpos, 0, Videoclipwidth,
EventArgs.Frame.Height),
System.Drawing.Imaging.PixelFormat.Undefined);

This. Dispatcher.begininvoke (new Action () =
{

Try
{
Img. RotateFlip (System.Drawing.RotateFlipType.RotateNoneFlipX);
}
catch (Exception e)
{
Isgood = false;
}

if (Isgood)
{
if (this.pb.Image! = null)
{
This.pb.Image.Dispose ();
This.pb.Image = null;
}
This.pb.Image = img;
Currentphoto = img;
}
Else
{
Img. Dispose ();
}

Gc. Collect ();
Gc. WaitForPendingFinalizers ();
Gc. Collect ();
}));
}

void Videosource1_videosourceerror (object sender, AForge.Video.VideoSourceErrorEventArgs EventArgs)
{

}
}

The appeal code is to achieve a specific area of the video interception, this method at 1080p, and 1920*2 1080 resolution of the card, (test machine is i5,4g memory)

In order to not card, certainly will drop frame, but still can accept.

In fact, the best way is to pass the form handle to C + + for video rendering. But in general, I think this method is enough.

WPF implements camera mirroring rollover

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.