-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
189 additions
and
66 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
using Microsoft.WindowsAPICodePack.Shell; | ||
using System.Drawing; | ||
using System.Drawing.Imaging; | ||
using System.IO; | ||
using System.Net.Cache; | ||
using System.Windows.Media.Imaging; | ||
|
||
namespace Sucrose.Portal.Extension | ||
{ | ||
internal class ThumbnailLoader : IDisposable | ||
{ | ||
public string SourcePath { get; set; } = null; | ||
|
||
public Uri SourceUri { get; set; } = null; | ||
|
||
public BitmapImage Load(string SourcePath) | ||
{ | ||
BitmapImage Image; | ||
|
||
if (File.Exists(SourcePath)) | ||
{ | ||
this.SourcePath = SourcePath; | ||
SourceUri = new(SourcePath); | ||
|
||
try | ||
{ | ||
ShellFile Shell = ShellFile.FromFilePath(SourcePath); | ||
Bitmap Thumbnail = Shell.Thumbnail.ExtraLargeBitmap; | ||
|
||
Image = Convert(Thumbnail); | ||
|
||
Dispose(); | ||
} | ||
catch | ||
{ | ||
Image = Default(); | ||
|
||
Dispose(); | ||
} | ||
} | ||
else | ||
{ | ||
Image = Default(); | ||
|
||
Dispose(); | ||
} | ||
|
||
return Image; | ||
} | ||
|
||
public async Task<BitmapImage> LoadAsync(string ImagePath) | ||
{ | ||
return await Task.Run(() => Load(ImagePath)); | ||
} | ||
|
||
private BitmapImage Default() | ||
{ | ||
BitmapImage Image = new(); | ||
|
||
Image.BeginInit(); | ||
|
||
Image.UriCachePolicy = new(RequestCacheLevel.BypassCache); | ||
Image.CacheOption = BitmapCacheOption.OnLoad; | ||
|
||
Image.UriSource = new Uri("pack://application:,,,/Assets/Theme/Default.jpg", UriKind.RelativeOrAbsolute); | ||
|
||
SourcePath = null; | ||
|
||
Image.EndInit(); | ||
|
||
return Image; | ||
} | ||
|
||
private BitmapImage Convert(Bitmap Bitmap) | ||
{ | ||
using MemoryStream Stream = new(); | ||
|
||
Bitmap.Save(Stream, ImageFormat.Jpeg); | ||
|
||
Stream.Seek(0, SeekOrigin.Begin); | ||
|
||
BitmapImage Image = new(); | ||
|
||
Image.BeginInit(); | ||
|
||
Image.UriCachePolicy = new(RequestCacheLevel.BypassCache); | ||
Image.CacheOption = BitmapCacheOption.OnLoad; | ||
|
||
Image.StreamSource = Stream; | ||
|
||
Image.EndInit(); | ||
|
||
Image.Freeze(); | ||
|
||
Image.StreamSource.Flush(); | ||
|
||
Stream.Flush(); | ||
Stream.Close(); | ||
Stream.Dispose(); | ||
|
||
return Image; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
GC.Collect(); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
public async Task DisposeAsync() | ||
{ | ||
await Task.Run(Dispose); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters