27
27
using System . Text ;
28
28
using System . IO ;
29
29
using System . Security . Cryptography ;
30
+ using System . Windows . Forms ;
30
31
31
32
namespace AutoScreenCapture
32
33
{
@@ -110,6 +111,71 @@ internal struct WINDOWPLACEMENT
110
111
[ DllImport ( "user32.dll" ) ]
111
112
private static extern bool ShowWindowAsync ( IntPtr hWnd , int nCmdShow ) ;
112
113
114
+ public const int ENUM_CURRENT_SETTINGS = - 1 ;
115
+
116
+ [ DllImport ( "user32.dll" ) ]
117
+ public static extern bool EnumDisplaySettings ( string lpszDeviceName , int iModeNum , ref DEVMODE lpDevMode ) ;
118
+
119
+ [ StructLayout ( LayoutKind . Sequential ) ]
120
+ public struct DEVMODE
121
+ {
122
+ private const int CCHDEVICENAME = 0x20 ;
123
+ private const int CCHFORMNAME = 0x20 ;
124
+ [ MarshalAs ( UnmanagedType . ByValTStr , SizeConst = 0x20 ) ]
125
+ public string dmDeviceName ;
126
+ public short dmSpecVersion ;
127
+ public short dmDriverVersion ;
128
+ public short dmSize ;
129
+ public short dmDriverExtra ;
130
+ public int dmFields ;
131
+ public int dmPositionX ;
132
+ public int dmPositionY ;
133
+ public ScreenOrientation dmDisplayOrientation ;
134
+ public int dmDisplayFixedOutput ;
135
+ public short dmColor ;
136
+ public short dmDuplex ;
137
+ public short dmYResolution ;
138
+ public short dmTTOption ;
139
+ public short dmCollate ;
140
+ [ MarshalAs ( UnmanagedType . ByValTStr , SizeConst = 0x20 ) ]
141
+ public string dmFormName ;
142
+ public short dmLogPixels ;
143
+ public int dmBitsPerPel ;
144
+ public int dmPelsWidth ;
145
+ public int dmPelsHeight ;
146
+ public int dmDisplayFlags ;
147
+ public int dmDisplayFrequency ;
148
+ public int dmICMMethod ;
149
+ public int dmICMIntent ;
150
+ public int dmMediaType ;
151
+ public int dmDitherType ;
152
+ public int dmReserved1 ;
153
+ public int dmReserved2 ;
154
+ public int dmPanningWidth ;
155
+ public int dmPanningHeight ;
156
+ }
157
+
158
+ /// <summary>
159
+ /// A struct containing a Windows Forms screen object, display device width, and display device height.
160
+ /// </summary>
161
+ public struct DeviceResolution
162
+ {
163
+ /// <summary>
164
+ /// A Windows Forms screen object.
165
+ /// </summary>
166
+ public System . Windows . Forms . Screen screen ;
167
+
168
+ /// <summary>
169
+ /// The width of the display device.
170
+ /// </summary>
171
+ public int width ;
172
+
173
+ /// <summary>
174
+ /// The height of the display device.
175
+ /// </summary>
176
+ public int height ;
177
+ }
178
+
113
179
/// <summary>
114
180
/// The minimum capture limit.
115
181
/// </summary>
@@ -220,6 +286,86 @@ internal struct WINDOWPLACEMENT
220
286
/// </summary>
221
287
public bool CaptureNow { get ; set ; }
222
288
289
+ private ImageCodecInfo GetEncoderInfo ( string mimeType )
290
+ {
291
+ var encoders = ImageCodecInfo . GetImageEncoders ( ) ;
292
+ return encoders . FirstOrDefault ( t => t . MimeType == mimeType ) ;
293
+ }
294
+
295
+ private static string GetMD5Hash ( Bitmap bitmap , ImageFormat format )
296
+ {
297
+ byte [ ] bytes = null ;
298
+
299
+ using ( MemoryStream ms = new MemoryStream ( ) )
300
+ {
301
+ bitmap . Save ( ms , format . Format ) ;
302
+ bytes = ms . ToArray ( ) ;
303
+ }
304
+
305
+ MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider ( ) ;
306
+
307
+ byte [ ] hash = md5 . ComputeHash ( bytes ) ;
308
+
309
+ StringBuilder sb = new StringBuilder ( ) ;
310
+
311
+ foreach ( byte b in hash )
312
+ {
313
+ sb . Append ( b . ToString ( "x2" ) . ToLower ( ) ) ;
314
+ }
315
+
316
+ return sb . ToString ( ) ;
317
+ }
318
+
319
+ private void SaveToFile ( string path , ImageFormat format , int jpegQuality , Bitmap bitmap )
320
+ {
321
+ try
322
+ {
323
+ if ( bitmap != null && format != null && ! string . IsNullOrEmpty ( path ) )
324
+ {
325
+ if ( format . Name . Equals ( ImageFormatSpec . NAME_JPEG ) )
326
+ {
327
+ var encoderParams = new EncoderParameters ( 1 ) ;
328
+ encoderParams . Param [ 0 ] = new EncoderParameter ( System . Drawing . Imaging . Encoder . Quality , jpegQuality ) ;
329
+
330
+ var encoderInfo = GetEncoderInfo ( "image/jpeg" ) ;
331
+
332
+ bitmap . Save ( path , encoderInfo , encoderParams ) ;
333
+ }
334
+ else
335
+ {
336
+ bitmap . Save ( path , format . Format ) ;
337
+ }
338
+
339
+ Log . WriteMessage ( "Screenshot saved to file at path \" " + path + "\" " ) ;
340
+ }
341
+ }
342
+ catch ( Exception ex )
343
+ {
344
+ Log . WriteExceptionMessage ( "ScreenCapture::SaveToFile" , ex ) ;
345
+ }
346
+ }
347
+
348
+ /// <summary>
349
+ /// Gets the resolution of the display device associated with the screen.
350
+ /// </summary>
351
+ /// <param name="screen">The Windows Forms screen object associated with the display device.</param>
352
+ /// <returns>A struct having the screen, display device width, and display device height.</returns>
353
+ public static DeviceResolution GetDeviceResolution ( System . Windows . Forms . Screen screen )
354
+ {
355
+ DEVMODE dm = new DEVMODE ( ) ;
356
+ dm . dmSize = ( short ) Marshal . SizeOf ( typeof ( DEVMODE ) ) ;
357
+ EnumDisplaySettings ( screen . DeviceName , ENUM_CURRENT_SETTINGS , ref dm ) ;
358
+
359
+ DeviceResolution deviceResolution = new DeviceResolution ( )
360
+ {
361
+ screen = screen ,
362
+ width = dm . dmPelsWidth ,
363
+ height = dm . dmPelsHeight
364
+ } ;
365
+
366
+ return deviceResolution ;
367
+ }
368
+
223
369
/// <summary>
224
370
/// Gets the screenshot image from an image file. This is used when we want to show screenshot images.
225
371
/// </summary>
@@ -507,7 +653,7 @@ public bool GetScreenImages(int component, int x, int y, int width, int height,
507
653
/// <param name="screenshotCollection">A collection of screenshot objects.</param>
508
654
/// <returns>A boolean to determine if we successfully saved the screenshot.</returns>
509
655
public bool SaveScreenshot ( string path , ImageFormat format , int component , ScreenshotType screenshotType , int jpegQuality ,
510
- Guid viewId , Bitmap bitmap , string label , string windowTitle , string processName , ScreenshotCollection screenshotCollection , string applicationFocus )
656
+ Guid viewId , Bitmap bitmap , string label , string windowTitle , string processName , ScreenshotCollection screenshotCollection )
511
657
{
512
658
try
513
659
{
@@ -522,26 +668,6 @@ public bool SaveScreenshot(string path, ImageFormat format, int component, Scree
522
668
path = path . Substring ( 0 , filepathLengthLimit ) ;
523
669
}
524
670
525
- if ( ! string . IsNullOrEmpty ( applicationFocus ) )
526
- {
527
- Process [ ] process = Process . GetProcessesByName ( applicationFocus ) ;
528
-
529
- foreach ( var item in process )
530
- {
531
- var proc = Process . GetProcessById ( item . Id ) ;
532
-
533
- IntPtr handle = proc . MainWindowHandle ;
534
- SetForegroundWindow ( handle ) ;
535
-
536
- var placement = GetPlacement ( proc . MainWindowHandle ) ;
537
-
538
- if ( placement . showCmd == ShowWindowCommands . Minimized )
539
- {
540
- ShowWindowAsync ( proc . MainWindowHandle , ( int ) ShowWindowCommands . Normal ) ;
541
- }
542
- }
543
- }
544
-
545
671
Log . WriteMessage ( "Attempting to write image to file at path \" " + path + "\" " ) ;
546
672
547
673
// This is a normal path used in Windows (such as "C:\screenshots\").
@@ -701,63 +827,30 @@ public bool SaveScreenshot(string path, ImageFormat format, int component, Scree
701
827
}
702
828
}
703
829
704
- private void SaveToFile ( string path , ImageFormat format , int jpegQuality , Bitmap bitmap )
705
- {
706
- try
707
- {
708
- if ( bitmap != null && format != null && ! string . IsNullOrEmpty ( path ) )
709
- {
710
- if ( format . Name . Equals ( ImageFormatSpec . NAME_JPEG ) )
711
- {
712
- var encoderParams = new EncoderParameters ( 1 ) ;
713
- encoderParams . Param [ 0 ] = new EncoderParameter ( System . Drawing . Imaging . Encoder . Quality , jpegQuality ) ;
714
-
715
- var encoderInfo = GetEncoderInfo ( "image/jpeg" ) ;
716
-
717
- bitmap . Save ( path , encoderInfo , encoderParams ) ;
718
- }
719
- else
720
- {
721
- bitmap . Save ( path , format . Format ) ;
722
- }
723
-
724
- Log . WriteMessage ( "Screenshot saved to file at path \" " + path + "\" " ) ;
725
- }
726
- }
727
- catch ( Exception ex )
728
- {
729
- Log . WriteExceptionMessage ( "ScreenCapture::SaveToFile" , ex ) ;
730
- }
731
- }
732
-
733
- private ImageCodecInfo GetEncoderInfo ( string mimeType )
830
+ /// <summary>
831
+ /// Sets the application focus to the defined application using a given process name.
832
+ /// </summary>
833
+ /// <param name="applicationFocus">The name of the process of the application to focus.</param>
834
+ public static void SetApplicationFocus ( string applicationFocus )
734
835
{
735
- var encoders = ImageCodecInfo . GetImageEncoders ( ) ;
736
- return encoders . FirstOrDefault ( t => t . MimeType == mimeType ) ;
737
- }
836
+ if ( string . IsNullOrEmpty ( applicationFocus ) ) return ;
738
837
739
- private static string GetMD5Hash ( Bitmap bitmap , ImageFormat format )
740
- {
741
- byte [ ] bytes = null ;
838
+ Process [ ] process = Process . GetProcessesByName ( applicationFocus ) ;
742
839
743
- using ( MemoryStream ms = new MemoryStream ( ) )
840
+ foreach ( var item in process )
744
841
{
745
- bitmap . Save ( ms , format . Format ) ;
746
- bytes = ms . ToArray ( ) ;
747
- }
842
+ var proc = Process . GetProcessById ( item . Id ) ;
748
843
749
- MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider ( ) ;
844
+ IntPtr handle = proc . MainWindowHandle ;
845
+ SetForegroundWindow ( handle ) ;
750
846
751
- byte [ ] hash = md5 . ComputeHash ( bytes ) ;
847
+ var placement = GetPlacement ( proc . MainWindowHandle ) ;
752
848
753
- StringBuilder sb = new StringBuilder ( ) ;
754
-
755
- foreach ( byte b in hash )
756
- {
757
- sb . Append ( b . ToString ( "x2" ) . ToLower ( ) ) ;
849
+ if ( placement . showCmd == ShowWindowCommands . Minimized )
850
+ {
851
+ ShowWindowAsync ( proc . MainWindowHandle , ( int ) ShowWindowCommands . Normal ) ;
852
+ }
758
853
}
759
-
760
- return sb . ToString ( ) ;
761
854
}
762
855
}
763
856
}
0 commit comments