Skip to content

Commit 15e109e

Browse files
committed
path and gif changes
1 parent 078a759 commit 15e109e

6 files changed

Lines changed: 139 additions & 87 deletions

File tree

LXtory/Gif.cs

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ internal class Gif : BindableBase
3030
private readonly string cachedir;
3131
private readonly ObservableCollection<GifFrame> frames;
3232
private int encodingProgress;
33-
private readonly string datePattern;
33+
//private readonly string datePattern;
34+
private readonly string filepath;
35+
private readonly string filename;
3436

35-
public Gif(int framerate, int duration, int w, int h, int x, int y, string datePattern)
37+
//public Gif(int framerate, int duration, int w, int h, int x, int y, string datePattern)
38+
public Gif(int framerate, int duration, int w, int h, int x, int y, string filepath, string filename)
3639
{
37-
this.datePattern = datePattern;
40+
//this.datePattern = datePattern;
41+
this.filepath = filepath;
42+
this.filename = filename;
3843
this.width = w;
3944
this.height = h;
4045
this.posX = x;
@@ -43,7 +48,8 @@ public Gif(int framerate, int duration, int w, int h, int x, int y, string dateP
4348
this.delay = 1000 / framerate;
4449
this.fileindex = 0;
4550
this.encodingProgress = 0;
46-
this.cachedir = Path.Combine(Properties.Settings.Default.filePath, "gif_frames");
51+
//this.cachedir = Path.Combine(Properties.Settings.Default.filePath, "gif_frames");
52+
this.cachedir = Path.Combine(filepath, "gif_frames");
4753
this.frames = new ObservableCollection<GifFrame>();
4854
ImageBuffer = new BlockingCollection<Image>();
4955
}
@@ -171,20 +177,21 @@ private Task BufferToDisk()
171177
});
172178
}
173179

174-
public Task<string> EncodeGif(GifProgressNotification gpn)
180+
//public Task<string> EncodeGif(GifProgressNotification gpn)
181+
public Task<bool> EncodeGif(GifProgressNotification gpn)
175182
{
176183
return Task.Run(() =>
177184
{
178-
string date = DateTime.Now.ToString(datePattern);
179-
int count = 1;
180-
string gifname = $"gif_{date}.gif";
181-
182-
while (File.Exists(Path.Combine(Properties.Settings.Default.filePath, gifname)))
183-
{
184-
gifname = $"gif_{date}({count}).gif";
185-
count++;
186-
}
185+
//string date = DateTime.Now.ToString(datePattern);
186+
//int count = 1;
187+
//string gifname = $"gif_{date}.gif";
187188

189+
//while (File.Exists(Path.Combine(Properties.Settings.Default.filePath, gifname)))
190+
//{
191+
// gifname = $"gif_{date}({count}).gif";
192+
// count++;
193+
//}
194+
bool success = true;
188195
List<string> filePaths = new List<string>();
189196
foreach (GifFrame frame in frames)
190197
{
@@ -196,32 +203,35 @@ public Task<string> EncodeGif(GifProgressNotification gpn)
196203

197204
try
198205
{
199-
using (var gif = File.OpenWrite(Path.Combine(Properties.Settings.Default.filePath, gifname)))
206+
//using (var gif = File.OpenWrite(Path.Combine(Properties.Settings.Default.filePath, gifname)))
207+
using (var gif = File.OpenWrite(Path.Combine(this.filepath, this.filename)))
200208
{
201209
using (var encoder = new GifEncoder(gif))
202210
{
203211
var quantizer = new WuQuantizer();
204-
var histogram = new Histogram();
212+
//var histogram = new Histogram();
205213

206214
for (int i = 0; i < filePaths.Count; i++)
207215
{
208216
if (gpn.Cancelled)
209217
{
210218
break;
211219
}
212-
213-
using (var image = new Bitmap(Image.FromStream(new MemoryStream(File.ReadAllBytes(filePaths[i])))))
220+
214221
//using (var image = Image.FromStream(new MemoryStream(File.ReadAllBytes(filePaths[i]))))
222+
using (var image = new Bitmap(Image.FromStream(new MemoryStream(File.ReadAllBytes(filePaths[i])))))
215223
{
216-
//using (var quantImage = quantizer.QuantizeImage(image))
217-
using (var quantImage = quantizer.QuantizeImage(image, 10, 70, histogram, 256))
224+
225+
//using (var quantImage = quantizer.QuantizeImage(image, 10, 70, histogram, 256))
218226
//using (var quantImage = quantizer.QuantizeImage(new Bitmap(image)))
227+
using (var quantImage = quantizer.QuantizeImage(image))
219228
{
220229
encoder.AddFrame(quantImage, 0, 0, new TimeSpan(0, 0, 0, 0, delay));
221230
}
222231
}
223232
EncodingProgress = (int)(((i + 1.0) / filePaths.Count) * 100.0);
224233
}
234+
quantizer = null;
225235
filePaths.Clear();
226236
}
227237
}
@@ -234,10 +244,12 @@ public Task<string> EncodeGif(GifProgressNotification gpn)
234244

235245
if (gpn.Cancelled)
236246
{
237-
File.Delete(Path.Combine(Properties.Settings.Default.filePath, gifname));
238-
gifname = string.Empty;
247+
File.Delete(Path.Combine(this.filepath, this.filename));
248+
//gifname = string.Empty;
249+
success = false;
239250
}
240-
return gifname;
251+
//return gifname;
252+
return success;
241253
});
242254
}
243255
}

LXtory/MainLogic.cs

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -286,27 +286,6 @@ private static void AddToQueue(XImage x)
286286
}
287287
}
288288

289-
private static bool TokenNeedsRefresh(UploadSite site)
290-
{
291-
switch (site)
292-
{
293-
case UploadSite.Imgur:
294-
{
295-
TimeSpan diff = DateTime.Now.Subtract(settings.lastRefreshTime);
296-
//return diff.TotalSeconds >= settings.imgurTokenExpire;
297-
return diff.TotalSeconds >= 3600; // BECAUSE IMGUR FAILS
298-
}
299-
300-
case UploadSite.GoogleDrive:
301-
{
302-
TimeSpan diff = DateTime.Now.Subtract(settings.gdriveRefreshTime);
303-
return diff.TotalSeconds >= settings.gdriveTokenExpire;
304-
}
305-
default:
306-
return true;
307-
}
308-
}
309-
310289
private static bool CheckFileSizeLimit(long fileSize, int maxMB)
311290
{
312291
return ((fileSize / 1024L) / 1024L) > maxMB;
@@ -347,7 +326,7 @@ private async void Upload()
347326
}
348327

349328
// refresh imgur token if using account
350-
if (TokenNeedsRefresh(UploadSite.Imgur) && settings.anonUpload == false)
329+
if (OAuthHelpers.TokenNeedsRefresh(UploadSite.Imgur) && settings.anonUpload == false)
351330
{
352331
SetStatusBarText("Refreshing Imgur login..");
353332
BalloonMessage.SetIcon("R");
@@ -454,7 +433,7 @@ private async void Upload()
454433
continue;
455434
}
456435

457-
if (TokenNeedsRefresh(UploadSite.GoogleDrive))
436+
if (OAuthHelpers.TokenNeedsRefresh(UploadSite.GoogleDrive))
458437
{
459438
SetStatusBarText("Refreshing GDrive login..");
460439
BalloonMessage.SetIcon("R");
@@ -612,11 +591,11 @@ public void D3DCapPrimaryScreen()
612591
{
613592
if (!settings.d3dAllScreens)
614593
{
615-
ImageManager(DesktopDuplication.DuplicatePrimaryScreen(), title);
594+
SaveAndUploadImage(DesktopDuplication.DuplicatePrimaryScreen(), title);
616595
}
617596
else
618597
{
619-
ImageManager(DesktopDuplication.DuplicateAllScreens(), title);
598+
SaveAndUploadImage(DesktopDuplication.DuplicateAllScreens(), title);
620599
}
621600
}
622601
catch (Exception e)
@@ -632,7 +611,7 @@ public void CaptureFullscreen()
632611
var left = SystemParameters.VirtualScreenLeft;
633612
var w = SystemParameters.VirtualScreenWidth;
634613
var h = SystemParameters.VirtualScreenHeight;
635-
ImageManager(EncodeImage(ScreenCapture.CaptureArea((int)w, (int)h, (int)left, (int)top, false)), "fullscreen");
614+
SaveAndUploadImage(EncodeImage(ScreenCapture.CaptureArea((int)w, (int)h, (int)left, (int)top, false)), "fullscreen");
636615
}
637616

638617
// Capture current selected window
@@ -652,7 +631,7 @@ public void CaptureWindow()
652631
int height = rect.Bottom - rect.Top;
653632

654633
var title = GetActiveWindowTitle();
655-
ImageManager(EncodeImage(ScreenCapture.CaptureArea(width, height, rect.Left, rect.Top, false)), title);
634+
SaveAndUploadImage(EncodeImage(ScreenCapture.CaptureArea(width, height, rect.Left, rect.Top, false)), title);
656635
}
657636

658637
public void CaptureWindowFromPoint(int x, int y)
@@ -678,7 +657,7 @@ public void CaptureWindowFromPoint(int x, int y)
678657
title = buff.ToString();
679658
}
680659

681-
ImageManager(EncodeImage(ScreenCapture.CaptureArea(width, height, rect.Left, rect.Top, false)), title);
660+
SaveAndUploadImage(EncodeImage(ScreenCapture.CaptureArea(width, height, rect.Left, rect.Top, false)), title);
682661
}
683662

684663
// Create an overlay, draw a rectangle on the overlay to cap that area
@@ -705,7 +684,7 @@ public void CaptureArea()
705684
Matrix m = PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformToDevice;
706685
var works = NativeMethods.SetForegroundWindow(foregroundWindow);
707686
Console.WriteLine(works);
708-
ImageManager(
687+
SaveAndUploadImage(
709688
EncodeImage(
710689
ScreenCapture.CaptureArea(Convert.ToInt32(notification.Rect.Width * m.M22),
711690
Convert.ToInt32(notification.Rect.Height * m.M11),
@@ -740,7 +719,10 @@ public async void CaptureGif()
740719
int f = notification.GifFramerate;
741720
int d = notification.GifDuration;
742721
string datePattern = string.Empty != settings.dateTimeString ? settings.dateTimeString : defaultDateTimePattern;
743-
Gif gif = new Gif(f, d, w, h, x, y, datePattern);
722+
string path = GetImageSavePath();
723+
string filename = FilenameExistsCheck(path, $"gif_{DateTime.Now.ToString(datePattern)}", ".gif");
724+
//Gif gif = new Gif(f, d, w, h, x, y, datePattern);
725+
Gif gif = new Gif(f, d, w, h, x, y, path, filename);
744726
if (!notification.LoadCache)
745727
{
746728
await gif.StartCapture();
@@ -775,22 +757,23 @@ public async void CaptureGif()
775757
this.GifProgressRequest.Raise(gpn);
776758
if (gpn != null && gpn.Confirmed)
777759
{
778-
var filename = gpn.Name;
779-
if (filename != string.Empty)
760+
//var filename = gpn.Name;
761+
//if (filename != string.Empty)
762+
//{
763+
var img = CreateXImage(filename, Path.Combine(path, filename));
764+
if (settings.gifUpload)
780765
{
781-
var img = CreateXImage(filename, Path.Combine(settings.filePath, filename));
782-
if (settings.gifUpload)
783-
{
784-
AddToQueue(img);
785-
}
786-
else
787-
{
788-
AddXimageToList(img, "", "");
789-
}
766+
AddToQueue(img);
790767
}
768+
else
769+
{
770+
AddXimageToList(img, "", "");
771+
}
772+
//}
791773
}
792774
}
793775
}
776+
gif = null;
794777
}
795778
gifCapturing = false;
796779
}
@@ -831,21 +814,50 @@ private static string MakeValidFileName(string name)
831814
return builder.ToString();
832815
}
833816

834-
private string SaveImageToDisk(byte[] bmp, string filename)
817+
// Get folder for saving images
818+
public static string GetImageSavePath()
819+
{
820+
if (settings.filePath == "%exedir%")
821+
{
822+
return AppDomain.CurrentDomain.BaseDirectory;
823+
}
824+
return settings.filePath;
825+
}
826+
827+
// Check if file of filename exists in path. If true append a number to the filename
828+
private static string FilenameExistsCheck(string path, string filename, string extension)
835829
{
836830
int i = 1;
837-
filename = MakeValidFileName(filename);
838-
839-
string file = $"{filename}.png";
840-
string target = Path.Combine(settings.filePath, file);
841-
// if file with same name exists append a number to it
831+
string file = $"{filename}{extension}";
832+
string target = Path.Combine(path, file);
842833
while (File.Exists(target))
843834
{
844835
i++;
845-
file = $"{filename}({i}).png";
846-
target = Path.Combine(settings.filePath, file);
836+
file = $"{filename}({i}){extension}";
837+
target = Path.Combine(path, file);
847838
}
848839

840+
return file;
841+
}
842+
843+
private string SaveImageToDisk(byte[] bmp, string filename)
844+
{
845+
int i = 1;
846+
filename = MakeValidFileName(filename);
847+
848+
//string file = $"{filename}.png";
849+
string path = GetImageSavePath();
850+
string file = FilenameExistsCheck(path, filename, ".png");
851+
string target = Path.Combine(path, file);
852+
//string target = Path.Combine(settings.filePath, file);
853+
// if file with same name exists append a number to it
854+
//while (File.Exists(target))
855+
//{
856+
// i++;
857+
// file = $"{filename}({i}).png";
858+
// target = Path.Combine(settings.filePath, file);
859+
//}
860+
849861
try
850862
{
851863
File.WriteAllBytes(target, bmp);
@@ -892,7 +904,7 @@ private static XImage CreateXImage(string filename, string filepath)
892904
return x;
893905
}
894906

895-
private void ImageManager(byte[] image, string filename)
907+
private void SaveAndUploadImage(byte[] image, string filename)
896908
{
897909
var x = CreateXImage(image, filename);
898910
if (settings.saveLocal)

LXtory/OAuthHelpers.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ private static async Task<string> GetTokenResponse(string uri, NameValueCollecti
4141
return responseString;
4242
}
4343

44+
public static bool TokenNeedsRefresh(UploadSite site)
45+
{
46+
switch (site)
47+
{
48+
case UploadSite.Imgur:
49+
{
50+
TimeSpan diff = DateTime.Now.Subtract(Properties.Settings.Default.lastRefreshTime);
51+
//return diff.TotalSeconds >= settings.imgurTokenExpire;
52+
return diff.TotalSeconds >= 3600; // BECAUSE IMGUR FAILS
53+
}
54+
55+
case UploadSite.GoogleDrive:
56+
{
57+
TimeSpan diff = DateTime.Now.Subtract(Properties.Settings.Default.gdriveRefreshTime);
58+
return diff.TotalSeconds >= Properties.Settings.Default.gdriveTokenExpire;
59+
}
60+
default:
61+
return true;
62+
}
63+
}
64+
4465
public static async Task GetImgurToken(string code)
4566
{
4667
try

LXtory/ViewModels/GifProgressViewModel.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,17 @@ public GifProgressViewModel()
4040

4141
private async void StartEncode()
4242
{
43-
string name = await this.notification.Gif.EncodeGif(this.notification);
44-
if (name != String.Empty)
45-
{
46-
this.notification.Name = name;
47-
this.notification.Confirmed = true;
48-
}
49-
else
50-
{
51-
this.notification.Confirmed = false;
52-
}
43+
bool success = await this.notification.Gif.EncodeGif(this.notification);
44+
//if (name != String.Empty)
45+
//{
46+
// this.notification.Name = name;
47+
// this.notification.Confirmed = true;
48+
//}
49+
//else
50+
//{
51+
// this.notification.Confirmed = false;
52+
//}
53+
this.notification.Confirmed = success;
5354
this.FinishInteraction();
5455
}
5556

LXtory/ViewModels/MainViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ private static void ExitApplication()
154154

155155
private void OpenImageFolder()
156156
{
157-
Process.Start(Properties.Settings.Default.filePath);
157+
//Process.Start(Properties.Settings.Default.filePath);
158+
Process.Start(MainLogic.GetImageSavePath());
158159
}
159160

160161
public string WindowButtonText

0 commit comments

Comments
 (0)