@@ -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 )
0 commit comments