Skip to content

Commit

Permalink
v1.2.2.16
Browse files Browse the repository at this point in the history
  • Loading branch information
zdy1988 committed Feb 16, 2023
1 parent cdf804c commit 586ad84
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/AboutWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</StackPanel.Resources>
<StackPanel Orientation="Horizontal" Margin="0 0 0 12">
<Image Source="favicon.ico" Width="24" Height="24" Margin="0 0 4 0"/>
<TextBlock Text="{Binding Path=AppName2, Source={x:Static Properties:Settings.Default}}" FontSize="24" Margin="0" VerticalAlignment="Center"/>
<TextBlock x:Name="TextBlock_AppName" Text="ZDY" FontSize="24" Margin="0" VerticalAlignment="Center"/>
</StackPanel>

<TextBlock Margin="0 0 0 7" FontSize="11" VerticalAlignment="Bottom">
Expand Down
1 change: 1 addition & 0 deletions src/AboutWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public AboutWindow()
Assembly assembly = Assembly.GetExecutingAssembly();

this.TextBlock_ApplicationIntPtrSize.Text = Application.Current.GetIntPtrSize().ToString();
this.TextBlock_AppName.Text = Properties.Settings.Default.AppName.Substring(1);
this.TextBlock_Name.Text = assembly.GetProduct();
this.TextBlock_Copyright.Text = $"2022 - {DateTime.Now.Year}";
this.TextBlock_Version.Text = assembly.GetFileVersion();
Expand Down
3 changes: 0 additions & 3 deletions src/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
<setting name="AppName" serializeAs="String">
<value>ZDY ' PICTURE</value>
</setting>
<setting name="AppName2" serializeAs="String">
<value>DY ' PICTURE</value>
</setting>
</Zhai.PictureView.Properties.Settings>
</userSettings>
</configuration>
46 changes: 45 additions & 1 deletion src/ImageDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
Expand Down Expand Up @@ -144,7 +145,7 @@ internal static async Task<BitmapSource> GetBitmapSourceAsync(string filename)
catch (Exception e)
{
#if DEBUG
Debug.WriteLine("RenderToBitmapSource returned " + filename + " null, \n" + e.Message);
Trace.WriteLine($"{nameof(GetBitmapSourceAsync)} {filename} exception, \n {e.Message}");
#endif
return null;
}
Expand Down Expand Up @@ -429,5 +430,48 @@ internal static bool TryGetExifValue(IExifValue value, out string name, out stri
}

#endregion

public static async Task<bool> SaveImageAsync(Stream stream, string targetPath, int? quality = null, int? width = null, int? height = null)
{
try
{
using MagickImage magickImage = new();

if (stream is not null)
{
await magickImage.ReadAsync(stream);
}
else
{
return false;
}

if (quality is not null)
{
magickImage.Quality = quality.Value;
}

if (width is not null)
{
magickImage.Resize(width.Value, 0);
}
else if (height is not null)
{
magickImage.Resize(0, height.Value);
}

await magickImage.WriteAsync(targetPath);
}
catch (Exception e)
{
#if DEBUG
Trace.WriteLine($"{nameof(SaveImageAsync)} {targetPath} exception, \n {e.Message}");
#endif

return false;
}

return true;
}
}
}
7 changes: 4 additions & 3 deletions src/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,15 @@
<ZDY:ContextMenu>
<MenuItem Icon="{ZDY:Icon Kind=VuesaxOutlineDocumentCopy}" Header="复制图片" Command="{Binding ExecuteCopyCurrentPictureSourceCommand}"/>
<MenuItem Icon="{ZDY:Icon Kind=Copy2}" Header="复制图片地址" Command="{Binding ExecuteCopyCurrentPicturePathCommand}"/>
<MenuItem Icon="{ZDY:Icon Kind=Save}" Header="保存"/>
<MenuItem Icon="{ZDY:Icon Kind=SaveAs}" Header="另存为..."/>
<MenuItem Icon="{ZDY:Icon Kind=MessageEdit}" Header="重命名"/>
<MenuItem Icon="{ZDY:Icon Kind=Save}" Header="保存" Command="{Binding ExecuteSaveImageCommand}"/>
<MenuItem Icon="{ZDY:Icon Kind=SaveAs}" Header="另存为..." Command="{Binding ExecuteSaveAsImageCommand}"/>
<MenuItem Icon="{ZDY:Icon Kind=Delete}" Header="删除" Click="DeleteButton_Click"/>
<Separator/>
<MenuItem Icon="{ZDY:Icon Kind=GalleryEdit}" Header="打开效果面板" Command="{Binding ExecuteTogglePictureEffectsViewCommand}" Visibility="{Binding IsShowPictureEffectsView, Converter={ZDY:BoolToInverseVisibilityConverter}}"/>
<MenuItem Icon="{ZDY:Icon Kind=GalleryEdit}" Header="关闭效果面板" Command="{Binding ExecuteTogglePictureEffectsViewCommand}" Visibility="{Binding IsShowPictureEffectsView, Converter={ZDY:BoolToVisibilityConverter}}"/>
<Separator/>
<MenuItem Icon="{ZDY:Icon Kind=Info}" Header="图片信息" Click="InfoButton_Click"/>
<MenuItem Icon="{ZDY:Icon Kind=Folder2}" Header="打开所在的文件夹" Command="{Binding ExecuteOpenTheFolderCommand}"/>
</ZDY:ContextMenu>
</Image.ContextMenu>
</Image>
Expand Down Expand Up @@ -440,5 +440,6 @@
</Style>
</Grid.Style>
</Grid>
<ZDY:Hint Text="{Binding NotificationMessage}" FontWeight="Normal" CornerRadius="4" Margin="0 -160 0 0" Background="{x:Static SystemColors.HighlightBrush}" Duration="0:0:5"/>
</Grid>
</ZDY:GlassesWindow>
57 changes: 43 additions & 14 deletions src/Picture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using SkiaSharp;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Media;
Expand Down Expand Up @@ -64,6 +65,27 @@ public PictureExif PictureExif
set => Set(() => PictureExif, ref pictureExif, value);
}

public Stream PictureStream
{
get
{
if (PictureSource == null) return null;

var ms = new MemoryStream();

BitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(PictureSource));
encoder.Save(ms);

if (ms.CanSeek)
{
ms.Seek(0, SeekOrigin.Begin);
}

return ms;
}
}

public bool IsAnimation
{
get
Expand All @@ -84,6 +106,14 @@ public bool IsLoaded
}
}

public string Extension
{
get
{
return Path.GetExtension(PicturePath);
}
}

public Picture(string filename)
{
var file = new FileInfo(filename);
Expand Down Expand Up @@ -183,6 +213,18 @@ public async Task<PictureExif> LoadExif()
return PictureExif;
}

public async Task<bool> SaveAsync(string? targetPath)
{
if (IsLoaded)
{
targetPath ??= PicturePath;

return await ImageDecoder.SaveImageAsync(PictureStream, targetPath);
}

return false;
}

public bool Delete()
{
try
Expand All @@ -197,19 +239,6 @@ public bool Delete()
}
}

public Stream ToStream()
{
if (PictureSource == null) return null;

var ms = new MemoryStream();

BitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(PictureSource));
encoder.Save(ms);

return ms;
}

public override void Cleanup()
{
base.Cleanup();
Expand Down
70 changes: 42 additions & 28 deletions src/PictureSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,59 @@ namespace Zhai.PictureView
{
internal static class PictureSupport
{
internal static string[] All { get; } = new string[] {

// Standards
".jpg", ".jpeg", ".jpe", ".png", ".bmp", ".tif", ".tiff", ".gif", ".ico", ".jfif", ".webp", ".wbmp",
internal static string[] JPEG { get; } = new string[] {
".jpg", ".jpeg"
};

// Non-standards
internal static string[] PortableNetworkGraphic { get; } = new string[] {
".png"
};

// Photoshop
".psd", ".psb",
internal static string[] GraphicsInterchangeFormat { get; } = new string[] {
".gif"
};

// Pfim
".tga", ".dds",
internal static string[] Icon { get; } = new string[] {
".ico"
};

// Vector
".svg", // Maybe add svgz at some point
".xcf",
internal static string[] Photoshop { get; } = new string[] {
".psd", ".psb"
};

// Camera
".3fr", ".arw", ".cr2", ".crw", ".dcr", ".dng", ".erf", ".kdc", ".mdc", ".mef", ".mos", ".mrw", ".nef", ".nrw", ".orf",
".pef", ".raf", ".raw", ".rw2", ".srf", ".x3f",
internal static string[] Vector { get; } = new string[] {
".svg", ".svgz"
};

// Others
".pgm", ".hdr", ".cut", ".exr", ".dib", ".heic", ".emf", ".wmf", ".wpg", ".pcx", ".xbm", ".xpm"
internal static string[] Camera { get; } = new string[] {
".3fr", ".arw", ".cr2", ".cr3", ".crw", ".dcr", ".dng", ".erf", ".kdc", ".mdc", ".mef", ".mos", ".mrw",
".nef", ".nrw", ".orf", ".pef", ".raf", ".raw", ".rw2", ".srf", ".x3f",
};

internal static string[] Others { get; } = new string[] {
".jpe", ".bmp", ".jfif", ".webp", ".wbmp",
".tif", ".tiff", ".dds", ".tga", ".heic", ".heif", ".hdr", ".xcf", ".jxl", ".jp2",
".b64",
".pgm", ".ppm", ".cut", ".exr", ".dib", ".emf", ".wmf", ".wpg", ".pcx", ".xbm", ".xpm",
};

internal static string AllSupported = String.Join(";", All.Select(t => $"*{t}"));
internal static string[] All { get; } = (new List<IEnumerable<string>> { JPEG, PortableNetworkGraphic, GraphicsInterchangeFormat, Icon, Photoshop, Vector, Camera, Others }).Aggregate((x, y) => x.Concat(y)).ToArray();

internal static string ToFilter(this IEnumerable<string> strings)
{
return String.Join(";", strings.Select(t => $"*{t}"));
}

internal static string Filter { get; } =
$@"All Supported ({AllSupported})|{AllSupported}|
JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|
Portable Network Graphic (*.png)|*.png|
Graphics Interchange Format (*.gif)|*.gif|
Icon (*.ico)|*.ico|
Photoshop (*.psd;*.psb)|*.psd;*.psb|
Pfim (*.tga;*.dds)|*.tga;*.dds|
Vector (*.svg;*.xcf)|*.svg;*.xcf|
Camera (*.3fr;*.arw;*.cr2;*.crw;*.dcr;*.dng;*.erf;*.kdc;*.mdc;*.mef;*.mos;*.mrw;*.nef;*.nrw;*.orf;*.pef;*.raf;*.raw;*.rw2;*.srf;*.x3f)|*.3fr;*.arw;*.cr2;*.crw;*.dcr;*.dng;*.erf;*.kdc;*.mdc;*.mef;*.mos;*.mrw;*.nef;*.nrw;*.orf;*.pef;*.raf;*.raw;*.rw2;*.srf;*.x3f|
Other (*.jpe;*.tif;*.jfif;*.webp;*.wbmp;*.tiff;*.wmf;*.pgm;*.hdr;*.cut;*.exr;*.dib;*.heic;*.emf;*.wpg;*.pcx;*.xbm;*.xpm)|*.jpe;*.tif;*.jfif;*.webp;*.wbmp;*.tiff;*.wmf;*.pgm;*.hdr;*.cut;*.exr;*.dib;*.heic;*.emf;*.wpg;*.pcx;*.xbm;*.xpm|
$@"All Supported ({All.ToFilter()})|{All.ToFilter()}|
JPEG ({GraphicsInterchangeFormat.ToFilter()})|{Icon.ToFilter()}|
Portable Network Graphic ({PortableNetworkGraphic.ToFilter()})|{PortableNetworkGraphic.ToFilter()}|
Graphics Interchange Format ({GraphicsInterchangeFormat.ToFilter()})|{GraphicsInterchangeFormat.ToFilter()}|
Icon ({Icon.ToFilter()})|{Icon.ToFilter()}|
Photoshop ({Photoshop.ToFilter()})|{Photoshop.ToFilter()}|
Vector ({Vector.ToFilter()})|{Vector.ToFilter()}|
Camera ({Camera.ToFilter()})|{Camera.ToFilter()}|
Others ({Others.ToFilter()})|{Others.ToFilter()}|
All Files (*.*)|*.*";

internal static bool IsSupported(string filename) => All.Contains(Path.GetExtension(filename).ToLower());
Expand Down
28 changes: 28 additions & 0 deletions src/PictureWindowNotification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zhai.Famil.Common.Mvvm;

namespace Zhai.PictureView
{
internal partial class PictureWindowViewModel : ViewModelBase
{
public void SendNotificationMessage(string message)
{
if (!String.IsNullOrWhiteSpace(message))
{
NotificationMessage = "";
NotificationMessage = message;
}
}

private string notificationMessage;
public string NotificationMessage
{
get { return notificationMessage; }
set { Set(() => NotificationMessage, ref notificationMessage, value); }
}
}
}
38 changes: 37 additions & 1 deletion src/PictureWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
Expand All @@ -11,10 +12,11 @@
using System.Windows.Threading;
using Zhai.Famil.Common.Mvvm;
using Zhai.Famil.Common.Mvvm.Command;
using Zhai.Famil.Controls;

namespace Zhai.PictureView
{
internal class PictureWindowViewModel : ViewModelBase
internal partial class PictureWindowViewModel : ViewModelBase
{
private Folder folder;
public Folder Folder
Expand Down Expand Up @@ -171,12 +173,46 @@ public PictureWindowViewModel()

}, () => CurrentPicture != null && CurrentPicture.IsLoaded)).Value;

public RelayCommand ExecuteSaveImageCommand => new Lazy<RelayCommand>(() => new RelayCommand(() =>
{


}, () => CurrentPicture != null && CurrentPicture.IsLoaded)).Value;

public RelayCommand ExecuteSaveAsImageCommand => new Lazy<RelayCommand>(() => new RelayCommand(async () =>
{
if (Famil.Win32.CommonDialog.SaveFileDialog(Folder.Current.FullName, "", "另存为...", "", CurrentPicture.Extension, default, out string filename))
{
var isSuccess = await CurrentPicture.SaveAsync(filename);

if (isSuccess)
{
SendNotificationMessage("保存成功!");
}
else
{
SendNotificationMessage("保存失败!");
}
}

}, () => CurrentPicture != null && CurrentPicture.IsLoaded)).Value;

public RelayCommand ExecuteCopyCurrentPicturePathCommand => new Lazy<RelayCommand>(() => new RelayCommand(() =>
{
System.Windows.Clipboard.SetText(CurrentPicture.PicturePath);

})).Value;

public RelayCommand ExecuteOpenTheFolderCommand => new Lazy<RelayCommand>(() => new RelayCommand(() =>
{
var info = new System.Diagnostics.ProcessStartInfo("Explorer.exe")
{
Arguments = $"/select,{CurrentPicture.PicturePath}"
};
System.Diagnostics.Process.Start(info);

}, () => CurrentPicture != null)).Value;

#endregion


Expand Down
Loading

0 comments on commit 586ad84

Please sign in to comment.