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 OpenNETCF;
  7. using OpenNETCF.Win32;
  8. using Cowburn.Imaging;
  9. namespace CamCapturePPC
  10. {
  11. /// <summary>
  12. /// Summary description for Form1.
  13. /// </summary>
  14. public class MainForm : System.Windows.Forms.Form
  15. {
  16. private System.Windows.Forms.MainMenu mainMenu1;
  17. private System.Windows.Forms.Button GetImageBtn;
  18. private System.Windows.Forms.PictureBox camImage;
  19. private System.Windows.Forms.Panel hr;
  20. // Create a field for the HtcCamera instance.
  21. private Cowburn.Imaging.HtcCamera camera;
  22. public MainForm()
  23. {
  24. //
  25. // Required for Windows Form Designer support
  26. //
  27. InitializeComponent();
  28. // 
  29. // Create an new instance of the HtcCamera class
  30. // and hook up the CaptureCompleted event hander;
  31. //
  32. camera = new HtcCamera();
  33. camera.CaptureCompleted += new CameraEventHandler(camera_CaptureCompleted);
  34. camera.Orientation = Cowburn.Imaging.Orientation.Portrait;
  35. }
  36. /// <summary>
  37. /// Clean up any resources being used.
  38. /// </summary>
  39. protected override void Dispose( bool disposing )
  40. {
  41. base.Dispose( disposing );
  42. }
  43. #region Windows Form Designer generated code
  44. /// <summary>
  45. /// Required method for Designer support - do not modify
  46. /// the contents of this method with the code editor.
  47. /// </summary>
  48. private void InitializeComponent()
  49. {
  50. this.mainMenu1 = new System.Windows.Forms.MainMenu();
  51. this.GetImageBtn = new System.Windows.Forms.Button();
  52. this.camImage = new System.Windows.Forms.PictureBox();
  53. this.hr = new System.Windows.Forms.Panel();
  54. // 
  55. // GetImageBtn
  56. // 
  57. this.GetImageBtn.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular);
  58. this.GetImageBtn.Location = new System.Drawing.Point(4, 196);
  59. this.GetImageBtn.Size = new System.Drawing.Size(232, 68);
  60. this.GetImageBtn.Text = "Get New Image";
  61. this.GetImageBtn.Click += new System.EventHandler(this.GetImageBtn_Click);
  62. // 
  63. // camImage
  64. // 
  65. this.camImage.Location = new System.Drawing.Point(53, 4);
  66. this.camImage.Size = new System.Drawing.Size(135, 180);
  67. // 
  68. // hr
  69. // 
  70. this.hr.BackColor = System.Drawing.Color.RoyalBlue;
  71. this.hr.Location = new System.Drawing.Point(0, 188);
  72. this.hr.Size = new System.Drawing.Size(240, 2);
  73. // 
  74. // MainForm
  75. // 
  76. this.BackColor = System.Drawing.Color.Silver;
  77. this.Controls.Add(this.hr);
  78. this.Controls.Add(this.camImage);
  79. this.Controls.Add(this.GetImageBtn);
  80. this.Menu = this.mainMenu1;
  81. this.MinimizeBox = false;
  82. this.Text = "Camera Capture";
  83. }
  84. #endregion
  85. /// <summary>
  86. /// The main entry point for the application.
  87. /// </summary>
  88. static void Main() 
  89. {
  90. Application.Run(new MainForm());
  91. }
  92. private void GetImageBtn_Click(object sender, System.EventArgs e)
  93. {
  94. // Instruct the camera to make a capture to file
  95. if(camera != null)
  96. camera.Capture(@"My Photos",CaptureType.File);
  97. }
  98. private void camera_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. // HACK:
  112. Win32Window.SetForegroundWindow(Win32Window.FindWindow(null,this.Text));
  113. }
  114. }
  115. }