C#控制音量、实现摄像头

来源:互联网 发布:帝国cms 模型id 编辑:程序博客网 时间:2024/04/30 06:58

作者:kutekute

转自:http://blog.csdn.net/kutekute/article/details/8037795


api控制音量很简单,就是两个方法:

[csharp] view plaincopy
  1. [DllImport("Winmm.dll")]  
  2. private static extern int waveOutSetVolume(int hwo, System.UInt32 pdwVolume))//设置音量  

[csharp] view plaincopy
  1. [DllImport("Winmm.dll")]   
  2. private static extern uint waveOutGetVolume(int    hwo,out    System.UInt32    pdwVolume); //获取音量  

有些地方在两个参数中用的都是long类型,那样会引发错误,因为WinAPI的long类型是32位的,而C#的long是64位的,这就导致堆栈不对称。

第一个参数是波形文件输出设备标识符,可以用0,表示使用首选设备;第二个参数为音量大小,是一个32位的整数,低16位表示左声道的音量,高16位表示右声道的音量,范围从0x0000~0xFFFF。

注意之前还要使用命名空间:

[csharp] view plaincopy
  1. using      System.Runtime.InteropSerices;  


现在直接调用这两个方法就可以实现音量的设置和获取了,是不是很简单呢?

不过我们现在是要吧音量控制和c#的trackBar控件联系起来。

看一下下面的方法吧:

//设置音量

   

[csharp] view plaincopy
  1. System.UInt32 Value = (System.UInt32)((double)0xffff * (double)trackBar1.Value / (double)(trackBar1.Maximum - trackBar1.Minimum));//先把trackbar的value值映射到0x0000~0xFFFF范围  
  2.    //限制value的取值范围  
  3.    if (Value < 0) Value = 0;  
  4.    if (Value > 0xffff) Value = 0xffff;  
  5.    System.UInt32 left = (System.UInt32)Value;//左声道音量  
  6.    System.UInt32 right = (System.UInt32)Value;//右  
  7.    waveOutSetVolume(0, left << 16 | right); //"<<"左移,“|”逻辑或运算  



//获取音量(基本上像是设置音量的逆,就不多解释了。)

  
[csharp] view plaincopy
  1. uint v;  
  2.   IntPtr p = new IntPtr(0);  
  3.   uint i = waveOutGetVolume(p, out    v);  
  4.   uint vleft = v & 0xFFFF;  
  5.   uint vright = (v & 0xFFFF0000) >> 16;  
  6.   trackBar1.Value = (int.Parse(vleft.ToString()) | int.Parse(vright.ToString())) * (this.trackBar1.Maximum - this.trackBar1.Minimum) / 0xFFFF;  

转自:http://blog.csdn.net/mchp/article/details/3995970




C#实现摄像头

[csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Drawing.Imaging;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Threading;  
  10.   
  11. using AForge;  
  12. using AForge.Video;  
  13. using AForge.Video.DirectShow;  
  14. using AForge.Imaging;  
  15. using AForge.Imaging.Filters;  
  16.   
  17. namespace Camera  
  18. {  
  19.     public partial class Form1 : Form  
  20.     {  
  21.         private FilterInfoCollection videoDevices;  
  22.   
  23.         public Form1()  
  24.         {  
  25.             InitializeComponent();  
  26.         }  
  27.   
  28.         private void Form1_Load(object sender, EventArgs e)  
  29.         {  
  30.             try  
  31.             {  
  32.                 // 枚举所有视频输入设备  
  33.                 videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);  
  34.   
  35.                 if (videoDevices.Count == 0)  
  36.                     throw new ApplicationException();  
  37.   
  38.                 foreach (FilterInfo device in videoDevices)  
  39.                 {  
  40.                     tscbxCameras.Items.Add(device.Name);  
  41.                 }  
  42.   
  43.                 tscbxCameras.SelectedIndex = 0;  
  44.             }  
  45.             catch (ApplicationException)  
  46.             {  
  47.                 tscbxCameras.Items.Add("No local capture devices");  
  48.                 videoDevices = null;  
  49.             }  
  50.         }  
  51.   
  52.         private void toolStripButton1_Click(object sender, EventArgs e)  
  53.         {  
  54.             CameraConn();  
  55.         }  
  56.   
  57.         private void CameraConn()  
  58.         {  
  59.             VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedIndex].MonikerString);  
  60.             videoSource.DesiredFrameSize = new Size(320, 240);  
  61.             videoSource.DesiredFrameRate = 1;  
  62.   
  63.             videPlayer.VideoSource = videoSource;  
  64.             videPlayer.Start();  
  65.         }  
  66.   
  67.         private void toolStripButton2_Click(object sender, EventArgs e)  
  68.         {  
  69.             videPlayer.SignalToStop();  
  70.             videPlayer.WaitForStop();  
  71.         }  
  72.   
  73.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
  74.         {  
  75.             toolStripButton2_Click(nullnull);  
  76.         }  
  77.   
  78.   
  79.     }  
  80. }  


0 0