1
1
Эх сурвалжийг харах

Fix dragging, when image from web

Ruben 5 жил өмнө
parent
commit
a8f353943c

+ 6 - 1
PicView/ChangeImage/Navigation.cs

@@ -145,13 +145,18 @@ namespace PicView.ChangeImage
                     return;
                 }
             }
-            else
+            else if(File.Exists(Pics[x]))
             {
                 /// Add "pic" as local variable used for the image.
                 /// Use the Load() function load image from memory if available
                 /// if not, it will be null
                 pic = Preloader.Load(Pics[x]);
             }
+            else
+            {
+                Unload();
+                return;
+            }
 
             if (pic == null)
             {

+ 2 - 1
PicView/ImageHandling/ImageDecoder.cs

@@ -23,7 +23,6 @@ namespace PicView.ImageHandling
         /// <returns></returns>
         internal static async Task<BitmapSource> RenderToBitmapSource(string file)
         {
-            using var filestream = new FileStream(file, FileMode.Open, FileAccess.Read);
             var ext = Path.GetExtension(file).ToLower(CultureInfo.CurrentCulture);
             switch (ext)
             {
@@ -45,6 +44,7 @@ namespace PicView.ImageHandling
 
                     try
                     {
+                        using var filestream = new FileStream(file, FileMode.Open, FileAccess.Read);
                         using var memStream = new MemoryStream();
                         // Have to wait for it, or it will produce an unfinished image
                         await filestream.CopyToAsync(memStream).ConfigureAwait(true);
@@ -77,6 +77,7 @@ namespace PicView.ImageHandling
 
                         try
                         {
+                            using var filestream = new FileStream(file, FileMode.Open, FileAccess.Read);
                             magick.Read(filestream, mrs);
                         }
                         catch (MagickException e)

+ 43 - 7
PicView/UI/DragAndDrop.cs

@@ -1,5 +1,6 @@
 using PicView.ChangeImage;
 using PicView.FileHandling;
+using System;
 using System.Collections.Specialized;
 using System.Diagnostics;
 using System.IO;
@@ -99,7 +100,12 @@ namespace PicView.UI
             }
 
             // Load from preloader or thumbnails
-            TheMainWindow.MainImage.Source = Preloader.Contains(files[0]) ? Preloader.Load(files[0]) : GetBitmapSourceThumb(files[0]);
+            var thumb = Preloader.Contains(files[0]) ? Preloader.Load(files[0]) : GetBitmapSourceThumb(files[0]);
+
+            if (thumb != null)
+            {
+                TheMainWindow.MainImage.Source = thumb;
+            }
         }
 
         /// <summary>
@@ -112,11 +118,8 @@ namespace PicView.UI
             // TODO fix base64 image not returning to normal
 
             // Switch to previous image if available
-            if (!CanNavigate)
-            {
-                TheMainWindow.MainImage.Source = null;
-            }
-            else if (prevPicResource != null)
+
+            if (prevPicResource != null)
             {
                 TheMainWindow.MainImage.Source = prevPicResource;
             }
@@ -212,9 +215,42 @@ namespace PicView.UI
                 return;
             }
 
+            string file;
+
+            if (Pics.Count == 0)
+            {
+                string url = Library.Utilities.GetURL(TheMainWindow.Bar.Text);
+                if (Uri.IsWellFormedUriString(url, UriKind.Absolute)) // Check if from web
+                {
+                    // Create temp directory
+                    var tempPath = Path.GetTempPath();
+                    var fileName = Path.GetFileName(url);
+
+                    // Download to it
+                    using var webClient = new System.Net.WebClient();
+                    Directory.CreateDirectory(tempPath);
+                    webClient.DownloadFileAsync(new Uri(url), tempPath + fileName);
+
+                    file = tempPath + fileName;
+                }
+                else
+                {
+                    return;
+                }
+                    
+            }
+            else if (Pics.Count < FolderIndex)
+            {
+                file = Pics[FolderIndex];
+            }
+            else
+            {
+                return;
+            }
+
             FrameworkElement senderElement = sender as FrameworkElement;
             DataObject dragObj = new DataObject();
-            dragObj.SetFileDropList(new StringCollection() { Pics[FolderIndex] });
+            dragObj.SetFileDropList(new StringCollection() { file });
             DragDrop.DoDragDrop(senderElement, dragObj, DragDropEffects.Copy);
         }
     }

+ 19 - 3
PicView/UI/SetTitle.cs

@@ -1,4 +1,5 @@
-using System.IO;
+using PicView.ChangeImage;
+using System.IO;
 using System.Text;
 using static PicView.ChangeImage.Navigation;
 using static PicView.FileHandling.FileFunctions;
@@ -19,9 +20,19 @@ namespace PicView.UI
         /// <returns></returns>
         private static string[] TitleString(int width, int height, int index)
         {
+            FileInfo fileInfo;
+            try
+            {
+                fileInfo = new FileInfo(Pics[index]);
+            }
+            catch (System.Exception)
+            {
+                return null;
+            }
+
             var s1 = new StringBuilder(90);
-            s1.Append(Path.GetFileName(Pics[index])).Append(" ").Append(index + 1).Append("/").Append(Pics.Count).Append(" files")
-                    .Append(" (").Append(width).Append(" x ").Append(height).Append(StringAspect(width, height)).Append(GetSizeReadable(new FileInfo(Pics[index]).Length));
+            s1.Append(fileInfo.Name).Append(" ").Append(index + 1).Append("/").Append(Pics.Count).Append(" files")
+                    .Append(" (").Append(width).Append(" x ").Append(height).Append(StringAspect(width, height)).Append(GetSizeReadable(fileInfo.Length));
 
             if (!string.IsNullOrEmpty(ZoomPercentage))
             {
@@ -50,6 +61,11 @@ namespace PicView.UI
         internal static void SetTitleString(int width, int height, int index)
         {
             var titleString = TitleString(width, height, index);
+            if (titleString == null)
+            {
+                return;
+            }
+
             TheMainWindow.Title = titleString[0];
             TheMainWindow.Bar.Text = titleString[1];
             TheMainWindow.Bar.ToolTip = titleString[2];