1
1
using System ;
2
2
using System . Threading . Tasks ;
3
3
using System . IO ;
4
+ using System . Drawing . Imaging ;
4
5
5
6
namespace GifRecorder . Services
6
7
{
@@ -18,9 +19,12 @@ public GifRecorder(Stream stream, Action<int> action)
18
19
this . stepAction = action ;
19
20
}
20
21
21
- public async Task Start ( int seconds , int ax , int ay , int bx , int by , int timeInterval )
22
+ public async Task Start ( int seconds , int ax , int ay , int bx , int by , int timeInterval , int format = 1 )
22
23
{
23
- await this . captureScreenSequence ( seconds , ax , ay , bx , by , timeInterval ) ;
24
+ if ( format == 1 )
25
+ await this . captureScreenSequenceApng ( seconds , ax , ay , bx , by , timeInterval ) ;
26
+ else
27
+ await this . captureScreenSequence ( seconds , ax , ay , bx , by , timeInterval ) ;
24
28
}
25
29
26
30
private async Task captureScreenSequence ( int seconds , int ax , int ay , int bx , int by , int timeInterval )
@@ -50,5 +54,52 @@ await Task.Delay((int)(time)).ContinueWith( (t) =>
50
54
IsRunning = false ;
51
55
}
52
56
}
57
+
58
+ private async Task captureScreenSequenceApng ( int seconds , int ax , int ay , int bx , int by , int timeInterval )
59
+ {
60
+ using ( var pngWriter = new PngWriter ( this . stream , bx , by , timeInterval , 0 ) )
61
+ {
62
+ var imageChangeAnalyser = new ImageChangeAnalyser ( ) ;
63
+ Cancel = false ;
64
+ IsRunning = true ;
65
+ var imageCount = seconds * 1000 / timeInterval ;
66
+ var time = 0L ;
67
+ this . stepAction . Invoke ( 3 ) ;
68
+ for ( int i = 0 ; i < imageCount ; i ++ )
69
+ {
70
+ if ( Cancel )
71
+ break ;
72
+ time = timeInterval - time < 0 ? 0 : timeInterval - time ;
73
+ await Task . Delay ( ( int ) ( time ) ) . ContinueWith ( ( t ) =>
74
+ {
75
+ stepAction . Invoke ( time == 0 ? 0 : 1 ) ;
76
+ var time2 = DateTime . Now . Ticks / 10000 ;
77
+ var image = ScreenShotCreator . CaptureScreen ( true , ax , ay , bx , by ) ;
78
+ if ( false ) //for later use
79
+ {
80
+ var changes = imageChangeAnalyser . GetChanges ( image ) ;
81
+ if ( changes . SizeX == 0 || changes . SizeY == 0 )
82
+ {
83
+ changes . SizeX = 2 ;
84
+ changes . SizeY = 2 ;
85
+ changes . OffsetX = changes . OffsetX > 4 ? changes . OffsetX - 2 : 2 ;
86
+ changes . OffsetY = changes . OffsetY > 4 ? changes . OffsetY - 2 : 2 ;
87
+ }
88
+ var newImage = imageChangeAnalyser . GetPartialImage ( image , changes ) ;
89
+ pngWriter . WriteFrame ( newImage , changes . OffsetX , changes . OffsetY ) ;
90
+ }
91
+ else
92
+ {
93
+ pngWriter . WriteFrame ( image ) ;
94
+ }
95
+
96
+ time = DateTime . Now . Ticks / 10000 - time2 ;
97
+ } ) ;
98
+ }
99
+ this . stepAction . Invoke ( - 1 ) ;
100
+ IsRunning = false ;
101
+ }
102
+ this . stream . Close ( ) ;
103
+ }
53
104
}
54
105
}
0 commit comments