WPF display video, Image control splash screen, using the WinForm control to implement

Source: Internet
Author: User

Using C # call MinGW's dynamic library implementation of video recognition software, the program through the C + + call OpenCV Open Video, the original data of the image is rgb24 way to the C # end, C # by constructing an Image object to assign values to the control display picture.

To start with the WPF control Image:

1  while(true)2 {3S_img simg = Mingw.display ();//Call fetch image function4     intSize = SIMG.W * Simg.h *3;5     byte[] data =New byte[size];6Marshal.Copy (simg.pbuf, data,0, size);7Dispatcher.invoke ((EventHandler)Delegate8     {9WriteableBitmap bitmap =NewWriteableBitmap (SIMG.W, Simg.h, -, -, Pixelformats.rgb24,NULL);TenBitmap. WritePixels (NewInt32Rect (0,0, SIMG.W, simg.h), data, SIMG.W *3,0); OneImg1. Source =bitmap; ABitmapencoder encoder =NewJpegbitmapencoder (); - Encoder. Frames.add (Bitmapframe.create (bitmap)); -Encoder. Save (NewSystem.IO.FileStream (pics. ToString () +". jpg", System.IO.FileMode.Create)); thepics++; -},New Object[2]); -}

Where IMG1 is an image control.

When this method displays the video, if the frame rate is too high (an AVI file is opened during testing and there is no lag when reading the frame), the image will flash black screen. The first suspicion is the C + + part of the program multithreading synchronization problem, and then added the 12th to 15th line of code, the C # received images saved, the result picture is absolutely no problem, that is, the image control appears when the black screen situation. After debugging is not valid, you decide to use the WinForm image control.

First quoted in the project:

System.Windows.Froms

Windowsformsintegration

Then add the following in XAML:

Xmlns:wfi= "Clr-namespace:system.windows.forms.integration;assembly=windowsformsintegration"

xmlns:wf= "Clr-namespace:system.windows.forms;assembly=system.windows.forms"

Add at Control:

<windowsformshost name= "WindowsFormsHost1" grid.row= "1" grid.column= "0" >
<wf:picturebox name= "PictureBox1"/>
</WindowsFormsHost>

Add in C # code:

Public System.Windows.Forms.PictureBox PICB1;

Add when initializing:

PICB1 = Windowsformshost1.child as System.Windows.Forms.PictureBox;

Display code:

1  while(true)2 {3S_img simg = Mingw.display ();//Call fetch image function4Dispatcher.invoke ((EventHandler)Delegate5     {6System.Drawing.Bitmap bitmap=NewBitmap (simg.w,simg.h,simg.w*3, system.drawing.imaging.pixelformat.format24bpprgb,simg.pbuf);7PICB1. Image=bitmap;8},New Object[2]);9}

No flash, and the code is much fresher. However, the image Pixel format of WPF and WinForm is reversed, and it is unclear which RGB is the BGR.

WPF display video, Image control splash screen, using the WinForm control to implement

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.