PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/source/library/Interlace.UserInterface/ScreenShooting/ScreenShotControl.cs

https://bitbucket.org/VahidN/interlace
C# | 202 lines | 139 code | 38 blank | 25 comment | 23 complexity | 7b4200eb6c0822f4a3202242dacec292 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections.Generic;
  29. using System.ComponentModel;
  30. using System.Data;
  31. using System.Drawing;
  32. using System.Drawing.Imaging;
  33. using System.Security.AccessControl;
  34. using System.Text;
  35. using System.Windows.Forms;
  36. #endregion
  37. namespace Interlace.ScreenShooting
  38. {
  39. public partial class ScreenShotControl : Control
  40. {
  41. ScreenShot _screenShot;
  42. ScreenShotWindow _currentWindow = null;
  43. Region _currentWindowRegion = null;
  44. Region _currentHitRegion = null;
  45. Bitmap _capturedBitmap;
  46. public event EventHandler BitmapCaptured;
  47. public ScreenShot ScreenShot
  48. {
  49. get { return _screenShot; }
  50. set
  51. {
  52. Invalidate();
  53. _screenShot = value;
  54. }
  55. }
  56. public ScreenShotControl()
  57. {
  58. InitializeComponent();
  59. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  60. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  61. }
  62. void DisposeNonDesigner()
  63. {
  64. if (_currentHitRegion != null)
  65. {
  66. _currentHitRegion.Dispose();
  67. _currentHitRegion = null;
  68. }
  69. if (_currentWindowRegion != null)
  70. {
  71. _currentWindowRegion.Dispose();
  72. _currentWindowRegion = null;
  73. }
  74. if (_capturedBitmap != null)
  75. {
  76. _capturedBitmap.Dispose();
  77. _capturedBitmap = null;
  78. }
  79. }
  80. protected override void OnPaint(PaintEventArgs e)
  81. {
  82. ColorMatrix matrix = new ColorMatrix(
  83. new float[][] {
  84. new float[] {0.3f, 0.3f, 0.3f, 0.0f, 0.0f},
  85. new float[] {0.59f, 0.59f, 0.59f, 0.0f, 0.0f},
  86. new float[] {0.11f, 0.11f, 0.11f, 0.0f, 0.0f},
  87. new float[] {0.0f, 0.0f, 0.0f, 1.0f, 0.0f},
  88. new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
  89. });
  90. using (ImageAttributes imageAttributes = new ImageAttributes())
  91. {
  92. imageAttributes.SetColorMatrix(matrix);
  93. e.Graphics.DrawImage(_screenShot.CaptureBitmap,
  94. new Rectangle(new Point(0, 0), _screenShot.CaptureBitmap.Size), 0, 0,
  95. _screenShot.CaptureBitmap.Size.Width, _screenShot.CaptureBitmap.Size.Height, GraphicsUnit.Pixel, imageAttributes);
  96. }
  97. if (_currentWindowRegion != null)
  98. {
  99. using (SolidBrush brush = new SolidBrush(Color.FromArgb(128, Color.Red)))
  100. {
  101. e.Graphics.FillRegion(brush, _currentWindowRegion);
  102. }
  103. }
  104. }
  105. private void ScreenShotControl_MouseMove(object sender, MouseEventArgs e)
  106. {
  107. Point screenPoint = PointToScreen(e.Location);
  108. if (_currentHitRegion == null || !_currentHitRegion.IsVisible(screenPoint))
  109. {
  110. ScreenShotWindow window = ScreenShotWindow.FindWindowByPoint(_screenShot.ScreenWindow, screenPoint);
  111. if (window != _currentWindow)
  112. {
  113. if (_currentWindowRegion != null) _currentWindowRegion.Dispose();
  114. if (_currentHitRegion != null) _currentHitRegion.Dispose();
  115. _currentWindow = window;
  116. _currentWindowRegion = ScreenShotWindow.FindWindowVisibleRegion(_screenShot.ScreenWindow, window, true);
  117. _currentHitRegion = ScreenShotWindow.FindWindowVisibleRegion(_screenShot.ScreenWindow, window, false);
  118. Invalidate();
  119. }
  120. }
  121. }
  122. public Bitmap GetAndClearCapturedBitmap()
  123. {
  124. if (_capturedBitmap == null) throw new InvalidOperationException();
  125. Bitmap toReturn = _capturedBitmap;
  126. _capturedBitmap = null;
  127. return toReturn;
  128. }
  129. private void ScreenShotControl_Click(object sender, EventArgs e)
  130. {
  131. if (_currentWindowRegion == null) return;
  132. Rectangle fromBounds;
  133. using (Graphics g = Graphics.FromImage(_screenShot.CaptureBitmap))
  134. {
  135. fromBounds = Rectangle.Ceiling(_currentWindowRegion.GetBounds(g));
  136. }
  137. Bitmap bitmap = new Bitmap(fromBounds.Width, fromBounds.Height);
  138. try
  139. {
  140. bitmap.MakeTransparent();
  141. using (Graphics g = Graphics.FromImage(bitmap))
  142. {
  143. using (Region clipRegion = new Region())
  144. {
  145. clipRegion.Union(_currentWindowRegion);
  146. clipRegion.Translate(-fromBounds.Left, -fromBounds.Top);
  147. Region oldClip = g.Clip;
  148. g.Clip = clipRegion;
  149. Rectangle destinationBounds = new Rectangle(new Point(0, 0), fromBounds.Size);
  150. g.DrawImage(_screenShot.CaptureBitmap, destinationBounds, fromBounds, GraphicsUnit.Pixel);
  151. g.Clip = oldClip;
  152. }
  153. }
  154. _capturedBitmap = bitmap;
  155. if (BitmapCaptured != null) BitmapCaptured(this, EventArgs.Empty);
  156. }
  157. catch (Exception)
  158. {
  159. bitmap.Dispose();
  160. throw;
  161. }
  162. }
  163. }
  164. }