Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8899

C# - PCのWebカメラで静止画を撮影する / C#(csc.exe)からWinRTを使う (Windows10環境)

$
0
0

参考サイト

ソースコードは2個目のサイトからほぼ持ってきたのですが、今見たらアクセスできなくなってました。。

コンパイルバッチ

csc.exeでコンパイルするのはあまり情報が落ちていないので苦労するところ。
※デフォルト環境ではWindows Kitsないかも。

csc /r:C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Runtime.WindowsRuntime\v4.0_4.0.0.0__b77a5c561934e089\system.runtime.windowsruntime.dll ^
/r:C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Runtime.InteropServices.WindowsRuntime\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Runtime.InteropServices.WindowsRuntime.dll ^
/r:C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Runtime\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Runtime.dll ^
"/r:C:\Program Files (x86)\Windows Kits\8.1\References\CommonConfiguration\Neutral\Annotated\Windows.winmd" %*

ソースコード

起動するといきなり撮影するので注意。

usingSystem;usingSystem.Drawing;usingSystem.Linq;usingSystem.Windows.Forms;usingWindows.Devices.Enumeration;usingWindows.Media.Capture;usingWindows.Media.MediaProperties;usingWindows.Storage.Streams;namespaceCSFormsCamera{publicpartialclassForm1:Form{PictureBoxpictureBox1;ListBoxlistBox1;publicForm1(){this.Width=600;this.Height=600;varpanel1=newSystem.Windows.Forms.Panel();panel1.Width=100;panel1.Dock=DockStyle.Left;this.Controls.Add(panel1);Labellabel1=newLabel();label1.Text="カメラ一覧";panel1.Controls.Add(label1);listBox1=newListBox();listBox1.Width=100;listBox1.Top=label1.Height;listBox1.DisplayMember="Name";listBox1.SelectedIndexChanged+=(s,e)=>{CaptureImage();};panel1.Controls.Add(listBox1);Buttonbutton1=newButton();button1.Text="Capture";button1.Top=listBox1.Top+listBox1.Height+5;button1.AutoSize=true;button1.Click+=(s,e)=>{CaptureImage();};panel1.Controls.Add(button1);pictureBox1=newPictureBox();pictureBox1.Dock=DockStyle.Fill;pictureBox1.BackColor=Color.White;this.Controls.Add(pictureBox1);this.Shown+=Form1_Shown;}asyncvoidForm1_Shown(objectsender,EventArgse){//利用可能なビデオデバイス(カメラ)を探すvardevices=awaitDeviceInformation.FindAllAsync(DeviceClass.VideoCapture);if(devices.Count==0){MessageBox.Show("カメラが見つかりませんでした","Error",MessageBoxButtons.OK);return;}this.listBox1.DataSource=devices.ToArray();}asyncvoidCaptureImage(){this.Enabled=false;try{vardi=(DeviceInformation)listBox1.SelectedItem;if(di!=null){using(MediaCapturemediaCapture=newMediaCapture()){mediaCapture.Failed+=(s,e)=>{MessageBox.Show("キャプチャできませんでした:"+e.Message,"Error",MessageBoxButtons.OK);};MediaCaptureInitializationSettingssetting=newMediaCaptureInitializationSettings();setting.VideoDeviceId=di.Id;//カメラ選択setting.StreamingCaptureMode=StreamingCaptureMode.Video;awaitmediaCapture.InitializeAsync(setting);//調整しないと暗い場合があるのでvarvcon=mediaCapture.VideoDeviceController;doublemax=vcon.Brightness.Capabilities.Max;Console.WriteLine(max);boolbrightnessAutoResult=vcon.Brightness.TrySetAuto(true);if(!brightnessAutoResult){Console.WriteLine("Brightness is manually set.");Console.WriteLine(vcon.Brightness.TrySetValue(max));}Console.WriteLine(vcon.Contrast.TrySetAuto(true));varpngProperties=ImageEncodingProperties.CreatePng();pngProperties.Width=(uint)pictureBox1.Width;pngProperties.Height=(uint)pictureBox1.Height;using(varrandomAccessStream=newInMemoryRandomAccessStream()){awaitmediaCapture.CapturePhotoToStreamAsync(pngProperties,randomAccessStream);randomAccessStream.Seek(0);//ビットマップにして表示System.IO.Streamstream=System.IO.WindowsRuntimeStreamExtensions.AsStream(randomAccessStream);varimg=System.Drawing.Image.FromStream(stream);this.pictureBox1.Image=img;}}}}catch(Exceptionex){MessageBox.Show(ex.Message);}this.Enabled=true;}[STAThread]staticvoidMain(){Application.Run(newForm1());}}}

Viewing all articles
Browse latest Browse all 8899

Trending Articles