Skip to content
This repository was archived by the owner on Jul 18, 2020. It is now read-only.

Commit 1b88133

Browse files
Marc RousavyMarc Rousavy
authored andcommitted
R: Fixed some GIF Recording Bugs
1 parent 065d1d4 commit 1b88133

File tree

14 files changed

+135
-99
lines changed

14 files changed

+135
-99
lines changed

Downloads/ImgurSniper.zip

4.97 KB
Binary file not shown.

Downloads/ImgurSniperSetup.zip

5.34 KB
Binary file not shown.

ImgurSniper.UI/Properties/strings.Designer.cs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ImgurSniper.UI/Properties/strings.de.resx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ Falls dieses Problem öfters auftritt, probieren Sie folgendes:
391391
• Ändern Sie das Bild format von .PNG auf .JPEG
392392
• Starten Sie Ihren Computer neu
393393
• Stellen Sie sicher, dass Sie Zugriff auf den Ordner Dokumente/ImgurSniper haben
394+
• Achten Sie darauf, dass ein Fehler auftreten kann wenn das GIF zu lang, zu groß, oder zu viele FPS hat!
394395
Tritt der Fehler immer noch auf, ist Ihre Internet Verbindung möglicherweise
395396
zu langsam für den Imgur Dienst.</value>
396397
</data>

ImgurSniper.UI/Properties/strings.resx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ If this problem occurs more often, try the following:
391391
• Change the Image Format from .PNG to .JPEG
392392
• Restart your Computer
393393
• Make sure that you have Write Permissions to Documents/ImgurSniper
394+
• Make sure that your GIF is not too long, too big, or has too many FPS!
394395
Does the Error still occur? Maybe your Internet Connection
395396
is too slow for the Imgur Service.</value>
396397
</data>

ImgurSniper/GifRecorder.xaml.cs

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ public partial class GifRecorder {
1717
private bool _stopped = false;
1818
private readonly int _fps = FileIO.GifFps;
1919
private Timer _timer;
20+
private Rectangle _size;
21+
private TimeSpan _gifLength;
2022

2123
public byte[] Gif;
2224

2325
public GifRecorder(Rectangle size, TimeSpan gifLength) {
2426
InitializeComponent();
2527

26-
//Begin Recording
27-
Record(size, gifLength);
28+
_size = size;
29+
_gifLength = gifLength;
2830

2931
Left = size.Left - 2;
3032
Top = size.Top - 2;
@@ -39,25 +41,33 @@ public GifRecorder(Rectangle size, TimeSpan gifLength) {
3941
//Space for ProgressBar
4042
Height += 30;
4143

42-
BeginAnimation(OpacityProperty, Animations.FadeIn);
44+
Loaded += delegate {
45+
BeginAnimation(OpacityProperty, Animations.FadeIn);
46+
Record();
47+
};
4348
}
4449

50+
4551
private void FadeOut(bool result) {
4652
DoubleAnimation fadeOut = Animations.FadeOut;
4753
fadeOut.Completed += delegate {
48-
DialogResult = result;
54+
try {
55+
DialogResult = result;
56+
} catch {
57+
Close();
58+
}
4959
};
5060
BeginAnimation(OpacityProperty, fadeOut);
5161
}
5262

53-
private void Record(Rectangle size, TimeSpan duration) {
63+
private void Record() {
5464
try {
5565
//Each Frame with TimeStamp
5666
List<BitmapFrame> bitmapframes = new List<BitmapFrame>();
5767

5868
#region Method 1: Timer
5969
int currentFrames = 0;
60-
int totalFrames = (int)(_fps * (duration.TotalMilliseconds / 1000D));
70+
int totalFrames = (int)(_fps * (_gifLength.TotalMilliseconds / 1000D));
6171
MemoryStream stream;
6272
MemoryStream gifStream = new MemoryStream();
6373
// ReSharper disable once PossibleLossOfFraction
@@ -68,39 +78,46 @@ private void Record(Rectangle size, TimeSpan duration) {
6878
//Every Frame
6979
_timer.Elapsed += delegate {
7080
new Thread(() => {
71-
//Finish GIF
72-
if(_stopped || currentFrames >= totalFrames) {
73-
_timer.Stop();
81+
try {
82+
//Finish GIF
83+
if(_stopped || currentFrames >= totalFrames) {
84+
_timer.Stop();
7485

75-
GifBitmapEncoder encoder = new GifBitmapEncoder();
76-
foreach(BitmapFrame frame in bitmapframes)
77-
encoder.Frames.Add(frame);
86+
GifBitmapEncoder encoder = new GifBitmapEncoder();
87+
foreach(BitmapFrame frame in bitmapframes)
88+
encoder.Frames.Add(frame);
7889

79-
encoder.Save(gifStream);
90+
encoder.Save(gifStream);
8091

81-
Gif = gifStream.ToArray();
92+
Gif = gifStream.ToArray();
8293

83-
_timer.Dispose();
84-
return;
85-
}
94+
_timer.Dispose();
95+
return;
96+
}
8697

87-
//Add Frames
88-
stream = new MemoryStream();
98+
//Add Frames
99+
stream = new MemoryStream();
89100

90-
Screenshot.GetScreenshotWithMouse(size).Save(stream, ImageFormat.Gif);
101+
Screenshot.GetScreenshotWithMouse(_size).Save(stream, ImageFormat.Gif);
91102

92-
BitmapFrame bitmap = BitmapFrame.Create(
93-
stream,
94-
BitmapCreateOptions.PreservePixelFormat,
95-
BitmapCacheOption.OnLoad);
103+
BitmapFrame bitmap = BitmapFrame.Create(
104+
stream,
105+
BitmapCreateOptions.PreservePixelFormat,
106+
BitmapCacheOption.OnLoad);
96107

97-
bitmapframes.Add(bitmap);
108+
bitmapframes.Add(bitmap);
98109

99-
currentFrames++;
110+
currentFrames++;
100111

101-
Dispatcher.BeginInvoke(new Action(delegate {
102-
ProgressBar.Value = currentFrames;
103-
}));
112+
Dispatcher.BeginInvoke(new Action(delegate {
113+
ProgressBar.Value = currentFrames;
114+
}));
115+
} catch {
116+
Dispatcher.BeginInvoke(new Action(delegate {
117+
FadeOut(false);
118+
_timer.Stop();
119+
}));
120+
}
104121
}).Start();
105122
};
106123

ImgurSniper/GifWindow.xaml.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ private void Position(bool allMonitors) {
6464

6565
if(allMonitors) {
6666
Rect workArea = SystemParameters.WorkArea;
67-
toast.Margin = new Thickness(workArea.Left, workArea.Top, (SystemParameters.VirtualScreenWidth - workArea.Right), (SystemParameters.VirtualScreenHeight - SystemParameters.PrimaryScreenHeight));
67+
toast.Margin = new Thickness(
68+
workArea.Left,
69+
workArea.Top,
70+
(SystemParameters.VirtualScreenWidth - workArea.Right),
71+
(SystemParameters.VirtualScreenHeight - SystemParameters.PrimaryScreenHeight));
6872
}
6973
}
7074

@@ -441,21 +445,20 @@ private void FinishRectangle() {
441445

442446
//Fade out window and shoot cropped screenshot
443447
private void Complete(int fromX, int fromY, int toX, int toY) {
444-
DoubleAnimation anim = new DoubleAnimation(0, TimeSpan.FromSeconds(0.15));
448+
DoubleAnimation fadeOut = Animations.FadeOut;
449+
450+
fadeOut.Completed += async delegate {
451+
grid.IsEnabled = false;
452+
grid.Visibility = Visibility.Collapsed;
445453

446-
anim.Completed += async delegate {
447-
grid.Opacity = 0;
448454
//For render complete
449455
Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle, null);
450456
await Task.Delay(50);
451457

452458
Crop(fromX, fromY, toX, toY);
453459
};
454460

455-
anim.From = grid.Opacity;
456-
anim.To = 0;
457-
458-
grid.BeginAnimation(OpacityProperty, anim);
461+
grid.BeginAnimation(OpacityProperty, fadeOut);
459462
}
460463

461464
//Make Image from custom Coords
@@ -471,23 +474,22 @@ private void Crop(int fromX, int fromY, int toX, int toY) {
471474
int w = toX - fromX;
472475
int h = toY - fromY;
473476

474-
//The super strange Microsoft Expression Encoder Library only allows numbers dividable by 4
475-
//(and only between 4 and 4096)
476-
while(w % 4 != 0)
477-
w += 1;
478-
while(h % 4 != 0)
479-
h += 1;
477+
//Uncomment if using Microsoft Expression Encoder:
478+
////The super strange Microsoft Expression Encoder Library only allows numbers dividable by 4
479+
////(and only between 4 and 4096)
480+
//while(w % 4 != 0)
481+
// w += 1;
482+
//while(h % 4 != 0)
483+
// h += 1;
480484

481485
StartGif(new Rectangle(fromX, fromY, w, h));
482486
}
483487

484488
//"Crop" Rectangle
485489
private void StartGif(Rectangle size) {
486-
int gifLength = FileIO.GifLength;
487-
488490
//Stop recording after 10 Secs
489491
//CHANGE THIS IF YOU WANT LONGER CAPTURES
490-
TimeSpan recordingLength = TimeSpan.FromMilliseconds(5000);
492+
TimeSpan recordingLength = TimeSpan.FromMilliseconds(FileIO.GifLength);
491493

492494
GifRecorder recorder = new GifRecorder(size, recordingLength);
493495
bool? result = recorder.ShowDialog();

ImgurSniper/Notification.xaml.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ public Notification(string text, NotificationType type, bool autoHide, Action on
4444
if(onClick != null) {
4545
NotificationContent.Cursor = Cursors.Hand;
4646
NotificationContent.MouseDown += delegate {
47-
onClick.Invoke();
47+
try {
48+
onClick.Invoke();
49+
} catch { }
4850
FadeOut();
4951
};
5052
}

ImgurSniper/Properties/strings.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ImgurSniper/Properties/strings.de.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,7 @@ Wollen Sie eine detaillierte Fehlermeldung sehen?
203203
<data name="gif" xml:space="preserve">
204204
<value>GIF</value>
205205
</data>
206+
<data name="uploadingErrorGif" xml:space="preserve">
207+
<value>Beim hochladen des GIFs ist ein Fehler aufgetreten!</value>
208+
</data>
206209
</root>

0 commit comments

Comments
 (0)