WPF calls the camera

Source: Internet
Author: User

Add assembly: WPFMediaKit.dll

The more critical code is as follows:

The interface design code is as follows:

<Windowx:class= "camera called." MainWindow "xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"Title= "MainWindow"Height= "All"Width= "$"Xmlns:wpfmedia= "Clr-namespace:wpfmediakit.directshow.controls;assembly=wpfmediakit"windowstartuplocation= "Centerscreen" >    <Grid>        <StackPanelOrientation= "Vertical">            <StackPanelOrientation= "Horizontal">                <ComboBoxName= "CB"selectionchanged= "Cb_selectionchanged"Width= "+" ></ComboBox>                <ButtonWidth= "a"Content= "Pat"Name= "Btncapture"Click= "Btncapture_click"Margin= "200,0,0,0"></Button>                <ButtonWidth= "a"Content= "Re-pat"Name= "Btnrestart"Click= "Restart_click"></Button>            </StackPanel>            <wpfmedia:videocaptureelementHeight= "434"x:name= "VCE"Stretch= "Fill"Margin= "172,30,185,0"Rendertransformorigin= "0.5,0.5"  >                <Wpfmedia:VideoCaptureElement.RenderTransform>                    <TransformGroup>                        <ScaleTransform/>                        <SkewTransform/>                        <RotateTransformAngle= " -90"/>                        <TranslateTransform/>                    </TransformGroup>                </Wpfmedia:VideoCaptureElement.RenderTransform>            </wpfmedia:videocaptureelement>        </StackPanel>    </Grid></Window>

Handling Event Codes

usingSystem;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.IO;usingWPFMediaKit.DirectShow.Controls;namespaceCamera Call {/// <summary>    ///the interactive logic of MainWindow.xaml/// </summary>     Public Partial classMainwindow:window { PublicMainWindow () {InitializeComponent (); Cb. ItemsSource= Multimediautil.videoinputnames;//Get all Cameras            if(MultimediaUtil.VideoInputNames.Length >0) {cb. SelectedIndex=0;//No. 0 Camera for default camera            }            Else{MessageBox.Show ("The computer does not have any available cameras installed"); }        }        Private voidBtncapture_click (Objectsender, RoutedEventArgs e)//Take Pictures{rendertargetbitmap bmp=NewRenderTargetBitmap ((int) VCE. ActualWidth, (int) VCE. ActualHeight,//VCE is the name of the foreground Wpfmedia control                 the, the, Pixelformats.default); //To avoid scratching, you need to call measure, Arrange before render//in order to avoid videocaptureelement display, it is necessary to//Videocaptureelement's stretch= "Fill"VCE. Measure (VCE.            Rendersize); Vce. Arrange (NewRect (VCE.            Rendersize)); Bmp.            Render (VCE); Bitmapencoder Encoder=NewJpegbitmapencoder (); Encoder.            Frames.add (Bitmapframe.create (BMP)); using(MemoryStream ms =NewMemoryStream ()) {Encoder.                Save (MS); byte[] Capturedata =Ms.                ToArray (); File.writeallbytes (@"./photo/"+ Guid.NewGuid (). ToString (). Substring (0,5) +". jpg", Capturedata); } VCE.        Pause (); }        //re-shoot        Private voidRestart_click (Objectsender, RoutedEventArgs e) {VCE.        Play (); }        Private voidCb_selectionchanged (Objectsender, SelectionChangedEventArgs e)//Selection events for ComboBox Controls{VCE. Videocapturesource= (string) CB. SelectedItem;//VCE is the name of the foreground Wpfmedia control        }    }}

WPF calls the camera

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.