From ec46de75b99a375b190b662a21cb0b5cd3dc3da4 Mon Sep 17 00:00:00 2001 From: Konstantin S Date: Sun, 5 Mar 2023 21:19:28 +0400 Subject: [PATCH] refactor: Refactored System.Drawing and SkiaSharp code. --- .../H.NotifyIcon.Shared.projitems | 9 ++++---- .../ImageExtensions.SystemDrawing.cs | 15 ------------ .../Utilities/ImageExtensions.cs | 2 +- .../Utilities/PngToIcoConverter.cs | 12 ++++------ .../SkiaSharp/StreamExtensions.SkiaSharp.cs | 23 +++++++++++++++++++ .../{ => SkiaSharp}/ToSkiaSharpExtensions.cs | 0 .../FromSystemDrawingExtensions.cs | 0 .../StreamExtensions.SystemDrawing.cs | 22 ++++++++++++++++++ .../ToSystemDrawingExtensions.cs | 0 .../H.NotifyIcon.Wpf/H.NotifyIcon.Wpf.csproj | 1 + 10 files changed, 57 insertions(+), 27 deletions(-) delete mode 100644 src/libs/H.NotifyIcon.Shared/Utilities/ImageExtensions.SystemDrawing.cs create mode 100644 src/libs/H.NotifyIcon.Shared/Utilities/SkiaSharp/StreamExtensions.SkiaSharp.cs rename src/libs/H.NotifyIcon.Shared/Utilities/{ => SkiaSharp}/ToSkiaSharpExtensions.cs (100%) rename src/libs/H.NotifyIcon.Shared/Utilities/{ => System.Drawing}/FromSystemDrawingExtensions.cs (100%) create mode 100644 src/libs/H.NotifyIcon.Shared/Utilities/System.Drawing/StreamExtensions.SystemDrawing.cs rename src/libs/H.NotifyIcon.Shared/Utilities/{ => System.Drawing}/ToSystemDrawingExtensions.cs (100%) diff --git a/src/libs/H.NotifyIcon.Shared/H.NotifyIcon.Shared.projitems b/src/libs/H.NotifyIcon.Shared/H.NotifyIcon.Shared.projitems index 14c0d7a..dd4e1ce 100644 --- a/src/libs/H.NotifyIcon.Shared/H.NotifyIcon.Shared.projitems +++ b/src/libs/H.NotifyIcon.Shared/H.NotifyIcon.Shared.projitems @@ -22,11 +22,12 @@ - - - - + + + + + diff --git a/src/libs/H.NotifyIcon.Shared/Utilities/ImageExtensions.SystemDrawing.cs b/src/libs/H.NotifyIcon.Shared/Utilities/ImageExtensions.SystemDrawing.cs deleted file mode 100644 index c2564d9..0000000 --- a/src/libs/H.NotifyIcon.Shared/Utilities/ImageExtensions.SystemDrawing.cs +++ /dev/null @@ -1,15 +0,0 @@ -#if HAS_SYSTEM_DRAWING -using H.NotifyIcon.Interop; - -namespace H.NotifyIcon; - -internal static partial class ImageExtensions -{ - internal static System.Drawing.Icon ToSmallIcon(this Stream stream) - { - var iconSize = IconUtilities.GetRequiredCustomIconSize(largeIcon: false).ScaleWithDpi(); - - return new System.Drawing.Icon(stream, iconSize); - } -} -#endif diff --git a/src/libs/H.NotifyIcon.Shared/Utilities/ImageExtensions.cs b/src/libs/H.NotifyIcon.Shared/Utilities/ImageExtensions.cs index 7f1f92a..792603e 100644 --- a/src/libs/H.NotifyIcon.Shared/Utilities/ImageExtensions.cs +++ b/src/libs/H.NotifyIcon.Shared/Utilities/ImageExtensions.cs @@ -2,7 +2,7 @@ namespace H.NotifyIcon; -internal static partial class ImageExtensions +internal static class ImageExtensions { #if HAS_WPF internal static Stream ToStream(this Uri uri) diff --git a/src/libs/H.NotifyIcon.Shared/Utilities/PngToIcoConverter.cs b/src/libs/H.NotifyIcon.Shared/Utilities/PngToIcoConverter.cs index f8c54a2..2fd1a2f 100644 --- a/src/libs/H.NotifyIcon.Shared/Utilities/PngToIcoConverter.cs +++ b/src/libs/H.NotifyIcon.Shared/Utilities/PngToIcoConverter.cs @@ -1,12 +1,11 @@ -#if HAS_SYSTEM_DRAWING -namespace H.NotifyIcon; +namespace H.NotifyIcon; internal static class PngToIcoConverter { public static byte[] ConvertPngToIco(this byte[] data) { using var inStream = new MemoryStream(data); - using var source = System.Drawing.Image.FromStream(inStream); + var metadata = inStream.GetMetadata(); using var outStream = new MemoryStream(); // Header @@ -25,9 +24,9 @@ public static byte[] ConvertPngToIco(this byte[] data) // Image entry { // Width - outStream.WriteByte((byte)source.Width); + outStream.WriteByte((byte)metadata.Width); // Height - outStream.WriteByte((byte)source.Height); + outStream.WriteByte((byte)metadata.Height); // Number of colors (0 = No palette) outStream.WriteByte(0); // Reserved @@ -36,7 +35,7 @@ public static byte[] ConvertPngToIco(this byte[] data) outStream.WriteByte(1); outStream.WriteByte(0); // Bits per pixel - var bppAsLittle = IntToLittle2(System.Drawing.Image.GetPixelFormatSize(source.PixelFormat)); + var bppAsLittle = IntToLittle2(metadata.BitsPerPixel); outStream.Write(bppAsLittle, 0, 2); // Size of data in bytes var byteCountAsLittle = IntToLittle4(data.Length); @@ -69,4 +68,3 @@ private static byte[] IntToLittle4(int input) return b; } } -#endif diff --git a/src/libs/H.NotifyIcon.Shared/Utilities/SkiaSharp/StreamExtensions.SkiaSharp.cs b/src/libs/H.NotifyIcon.Shared/Utilities/SkiaSharp/StreamExtensions.SkiaSharp.cs new file mode 100644 index 0000000..62b5ab2 --- /dev/null +++ b/src/libs/H.NotifyIcon.Shared/Utilities/SkiaSharp/StreamExtensions.SkiaSharp.cs @@ -0,0 +1,23 @@ +#if HAS_SKIA_SHARP +using H.NotifyIcon.Interop; +using SkiaSharp; + +namespace H.NotifyIcon; + +internal static class StreamExtensions +{ + internal static Icon ToSmallIcon(this Stream stream) + { + var iconSize = IconUtilities.GetRequiredCustomIconSize(largeIcon: false).ScaleWithDpi(); + + return Icon.Decode(stream, new SKImageInfo(width: iconSize.Width, height: iconSize.Height)); + } + + internal static (int Width, int Height, int BitsPerPixel) GetMetadata(this Stream stream) + { + using var image = SKBitmap.Decode(stream); + + return (image.Width, image.Height, image.BytesPerPixel * 8); + } +} +#endif diff --git a/src/libs/H.NotifyIcon.Shared/Utilities/ToSkiaSharpExtensions.cs b/src/libs/H.NotifyIcon.Shared/Utilities/SkiaSharp/ToSkiaSharpExtensions.cs similarity index 100% rename from src/libs/H.NotifyIcon.Shared/Utilities/ToSkiaSharpExtensions.cs rename to src/libs/H.NotifyIcon.Shared/Utilities/SkiaSharp/ToSkiaSharpExtensions.cs diff --git a/src/libs/H.NotifyIcon.Shared/Utilities/FromSystemDrawingExtensions.cs b/src/libs/H.NotifyIcon.Shared/Utilities/System.Drawing/FromSystemDrawingExtensions.cs similarity index 100% rename from src/libs/H.NotifyIcon.Shared/Utilities/FromSystemDrawingExtensions.cs rename to src/libs/H.NotifyIcon.Shared/Utilities/System.Drawing/FromSystemDrawingExtensions.cs diff --git a/src/libs/H.NotifyIcon.Shared/Utilities/System.Drawing/StreamExtensions.SystemDrawing.cs b/src/libs/H.NotifyIcon.Shared/Utilities/System.Drawing/StreamExtensions.SystemDrawing.cs new file mode 100644 index 0000000..83b7779 --- /dev/null +++ b/src/libs/H.NotifyIcon.Shared/Utilities/System.Drawing/StreamExtensions.SystemDrawing.cs @@ -0,0 +1,22 @@ +#if HAS_SYSTEM_DRAWING +using H.NotifyIcon.Interop; + +namespace H.NotifyIcon; + +internal static class StreamExtensions +{ + internal static Icon ToSmallIcon(this Stream stream) + { + var iconSize = IconUtilities.GetRequiredCustomIconSize(largeIcon: false).ScaleWithDpi(); + + return new Icon(stream, iconSize); + } + + internal static (int Width, int Height, int BitsPerPixel) GetMetadata(this Stream stream) + { + using var image = System.Drawing.Image.FromStream(stream); + + return (image.Width, image.Height, System.Drawing.Image.GetPixelFormatSize(image.PixelFormat)); + } +} +#endif diff --git a/src/libs/H.NotifyIcon.Shared/Utilities/ToSystemDrawingExtensions.cs b/src/libs/H.NotifyIcon.Shared/Utilities/System.Drawing/ToSystemDrawingExtensions.cs similarity index 100% rename from src/libs/H.NotifyIcon.Shared/Utilities/ToSystemDrawingExtensions.cs rename to src/libs/H.NotifyIcon.Shared/Utilities/System.Drawing/ToSystemDrawingExtensions.cs diff --git a/src/libs/H.NotifyIcon.Wpf/H.NotifyIcon.Wpf.csproj b/src/libs/H.NotifyIcon.Wpf/H.NotifyIcon.Wpf.csproj index 5b36855..bd67ec3 100644 --- a/src/libs/H.NotifyIcon.Wpf/H.NotifyIcon.Wpf.csproj +++ b/src/libs/H.NotifyIcon.Wpf/H.NotifyIcon.Wpf.csproj @@ -53,6 +53,7 @@ popups, context menus, and balloon messages. It can be used directly in code or all runtime; build; native; contentfiles; analyzers; buildtransitive +