Skip to content

Commit

Permalink
refactor: Refactored System.Drawing and SkiaSharp code.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Mar 5, 2023
1 parent 8b96f31 commit ec46de7
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 27 deletions.
9 changes: 5 additions & 4 deletions src/libs/H.NotifyIcon.Shared/H.NotifyIcon.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
<Compile Include="$(MSBuildThisFileDirectory)TaskbarIcon.AttachedProperties.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TaskbarIcon.MouseEvents.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\DpiUtilities.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\FromSystemDrawingExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\ImageExtensions.SystemDrawing.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\ToSkiaSharpExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\ToSystemDrawingExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\PngToIcoConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\SkiaSharp\StreamExtensions.SkiaSharp.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\SkiaSharp\ToSkiaSharpExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\System.Drawing\FromSystemDrawingExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\System.Drawing\StreamExtensions.SystemDrawing.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\System.Drawing\ToSystemDrawingExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TaskbarIcon.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TaskbarIcon.Properties.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\ImageExtensions.cs" />
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion src/libs/H.NotifyIcon.Shared/Utilities/ImageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 5 additions & 7 deletions src/libs/H.NotifyIcon.Shared/Utilities/PngToIcoConverter.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -69,4 +68,3 @@ private static byte[] IntToLittle4(int input)
return b;
}
}
#endif
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/libs/H.NotifyIcon.Wpf/H.NotifyIcon.Wpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ popups, context menus, and balloon messages. It can be used directly in code or
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.ValueTuple" Version="4.5.0" Condition="'$(TargetFramework)' == 'net4.6'" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit ec46de7

Please sign in to comment.