MainForm.cs
上传用户:chengzheng
上传日期:2013-08-05
资源大小:38k
文件大小:4k
源码类别:

Windows Mobile

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.Windows.Forms;
  5. using System.Data;
  6. using Cowburn.Imaging;
  7. namespace CamCaptureSP
  8. {
  9. /// <summary>
  10. /// Summary description for MainForm.
  11. /// </summary>
  12. public class MainForm : System.Windows.Forms.Form
  13. {
  14. private System.Windows.Forms.MenuItem menuItem1;
  15. private System.Windows.Forms.MainMenu mainMenu1;
  16. private System.Windows.Forms.MenuItem menuItem3;
  17. private System.Windows.Forms.PictureBox camImage;
  18. // Create a field for the HtcCamera instance.
  19. private Cowburn.Imaging.HtcCamera camera;
  20. public MainForm()
  21. {
  22. //
  23. // Required for Windows Form Designer support
  24. //
  25. InitializeComponent();
  26. // 
  27. // Create an new instance of the HtcCamera class
  28. // and hook up the CaptureCompleted event hander;
  29. //
  30. camera = new HtcCamera();
  31. camera.CaptureCompleted += new CameraEventHandler(Form1_CaptureCompleted);
  32. }
  33. /// <summary>
  34. /// Clean up any resources being used.
  35. /// </summary>
  36. protected override void Dispose( bool disposing )
  37. {
  38. base.Dispose( disposing );
  39. }
  40. #region Windows Form Designer generated code
  41. /// <summary>
  42. /// Required method for Designer support - do not modify
  43. /// the contents of this method with the code editor.
  44. /// </summary>
  45. private void InitializeComponent()
  46. {
  47. this.mainMenu1 = new System.Windows.Forms.MainMenu();
  48. this.menuItem1 = new System.Windows.Forms.MenuItem();
  49. this.menuItem3 = new System.Windows.Forms.MenuItem();
  50. this.camImage = new System.Windows.Forms.PictureBox();
  51. // 
  52. // mainMenu1
  53. // 
  54. this.mainMenu1.MenuItems.Add(this.menuItem1);
  55. this.mainMenu1.MenuItems.Add(this.menuItem3);
  56. // 
  57. // menuItem1
  58. // 
  59. this.menuItem1.Text = "Done";
  60. this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
  61. // 
  62. // menuItem3
  63. // 
  64. this.menuItem3.Text = "Get Image";
  65. this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
  66. // 
  67. // camImage
  68. // 
  69. this.camImage.Location = new System.Drawing.Point(8, 32);
  70. this.camImage.Size = new System.Drawing.Size(160, 120);
  71. // 
  72. // MainForm
  73. // 
  74. this.BackColor = System.Drawing.Color.Silver;
  75. this.Controls.Add(this.camImage);
  76. this.Menu = this.mainMenu1;
  77. this.Text = "Cam Capture";
  78. }
  79. #endregion
  80. /// <summary>
  81. /// The main entry point for the application.
  82. /// </summary>
  83. static void Main(string[] args) 
  84. {
  85. Application.Run(new MainForm());
  86. }
  87. private void menuItem1_Click(object sender, System.EventArgs e)
  88. {
  89. Application.Exit();
  90. }
  91. private void menuItem3_Click(object sender, System.EventArgs e)
  92. {
  93. // Change the resolution from the default (VGA) to QVGA
  94. camera.Resolution = Resolution.VGA;
  95. // Make a call to the camera to go ahead with the capture
  96. camera.Capture(string.Empty, CaptureType.File);
  97. }
  98. private void Form1_CaptureCompleted(object sender, CameraEventArgs e)
  99. {
  100. if(e.Path != string.Empty)
  101. {
  102. Bitmap image = new Bitmap(e.Path);
  103. // Create a white canvas to paint the image onto
  104. Bitmap tmp = new Bitmap(camImage.Width,camImage.Height);
  105. Graphics g = Graphics.FromImage(tmp);
  106. g.FillRectangle(new SolidBrush(Color.White),new Rectangle(0,0,tmp.Width, tmp.Height));
  107. // Resize the captured image to fit the picturebox control
  108. g.DrawImage(image,new Rectangle(0,0,tmp.Width,tmp.Height),new Rectangle(0,0,image.Width,image.Height),GraphicsUnit.Pixel);
  109. camImage.Image = tmp;
  110. }
  111. }
  112. }
  113. }