Skip to content

Commit

Permalink
Improve notification icon rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
mntone committed Aug 7, 2020
1 parent 045d5ca commit f9c2105
Show file tree
Hide file tree
Showing 8 changed files with 681 additions and 115 deletions.
3 changes: 3 additions & 0 deletions source/SylphyHorn/ApplicationPreparation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ public TaskTrayIcon CreateTaskTrayIcon()
{
new TaskTrayIconItem(Resources.TaskTray_Menu_Settings, ShowSettings, () => Application.Args.CanSettings),
new TaskTrayIconItem(Resources.TaskTray_Menu_Exit, this._shutdownAction),
#if DEBUG
new TaskTrayIconItem("Tasktray Icon Test", () => new TaskTrayTestWindow().Show()),
#endif
};

this._taskTrayIcon = new TaskTrayIcon(icon, lightIcon, menus);
Expand Down
12 changes: 12 additions & 0 deletions source/SylphyHorn/Interop/ColorHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Windows.Media;

namespace SylphyHorn.Interop
{
public static class ColorHelper
{
public static System.Drawing.Color ToGDIColor(this Color color)
{
return System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);
}
}
}
68 changes: 60 additions & 8 deletions source/SylphyHorn/Interop/IconHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Drawing;
using System.Windows;
using System.IO;
using System.Windows.Media.Imaging;
using MetroRadiance.Interop;

namespace SylphyHorn.Interop
Expand All @@ -20,6 +21,35 @@ public static Icon GetIconFromResource(Uri uri)
}
}

public static RenderTargetBitmap ToRenderTargetBitmap(this System.Windows.Media.DrawingVisual drawingVisual, Size size, Dpi? dpi = null)
{
var imageDpi = dpi ?? new Dpi(96, 96);
var imageSize = ScaleSizeByDpi(size, imageDpi);
var renderTarget = new RenderTargetBitmap(imageSize.Width, imageSize.Height, imageDpi.X, imageDpi.Y, System.Windows.Media.PixelFormats.Default);
renderTarget.Render(drawingVisual);
return renderTarget;
}

public static Bitmap ToBitmap(this System.Windows.Media.DrawingVisual drawingVisual, Size size, Dpi? dpi = null, Color? transparentColor = null)
{
var renderTarget = drawingVisual.ToRenderTargetBitmap(size, dpi);
var encoder = new BmpBitmapEncoder();
var frame = BitmapFrame.Create(renderTarget);
encoder.Frames.Add(frame);

using (var stream = new MemoryStream())
{
encoder.Save(stream);

var bitmap = new Bitmap(stream, useIcm: true);
if (transparentColor.HasValue)
{
bitmap.MakeTransparent(transparentColor.Value);
}
return bitmap;
}
}

public static Icon ToIcon(this Bitmap bitmap)
{
var iconHandle = bitmap.GetHicon();
Expand All @@ -28,21 +58,43 @@ public static Icon ToIcon(this Bitmap bitmap)
return icon;
}

#if DEBUG
internal static BitmapSource ToBitmapSource(this Icon icon)
{
using (var bitmap = icon.ToBitmap())
using (var stream = new MemoryStream())
{
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Seek(0, SeekOrigin.Begin);

var bitmapSource = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
return bitmapSource;
}
}
#endif

private static Icon ScaleIconToDpi(Icon targetIcon)
{
var dpi = GetMonitorDpi();
var dpi = GetSystemDpi();

return new Icon(targetIcon, new System.Drawing.Size((int)(16 * dpi.ScaleX), (int)(16 * dpi.ScaleY)));
return new Icon(targetIcon, new Size((int)(16 * dpi.ScaleX), (int)(16 * dpi.ScaleY)));
}

public static System.Windows.Size GetIconSize()
public static Size GetIconSize()
{
var dpi = GetMonitorDpi();
return new Size(
(int)System.Windows.SystemParameters.SmallIconWidth,
(int)System.Windows.SystemParameters.SmallIconHeight);
}

return new System.Windows.Size(SystemParameters.SmallIconWidth * dpi.ScaleX, SystemParameters.SmallIconHeight * dpi.ScaleY);
private static Size ScaleSizeByDpi(Size size, Dpi dpi)
{
return new Size(
(int)Math.Round(size.Width * dpi.ScaleX),
(int)Math.Round(size.Height * dpi.ScaleY));
}
private static Dpi GetMonitorDpi()

public static Dpi GetSystemDpi()
{
return PerMonitorDpi.GetDpi(IntPtr.Zero);
}
Expand Down
8 changes: 8 additions & 0 deletions source/SylphyHorn/SylphyHorn.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@
<Compile Include="Annotations\LightAttribute.cs" />
<Compile Include="ApplicationPreparation.cs" />
<Compile Include="CommandLineArgs.cs" />
<Compile Include="Interop\ColorHelper.cs" />
<Compile Include="Interop\DotnetVersion.cs" />
<Compile Include="Interop\IconHelper.cs" />
<Compile Include="Interop\IDesktopWallpaper.cs" />
Expand Down Expand Up @@ -282,6 +283,9 @@
<DependentUpon>SettingsWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Interop\UwpBridge.cs" />
<Compile Include="UI\TaskTrayTestWindow.xaml.cs" Condition="'$(Configuration)' == 'Debug'">
<DependentUpon>TaskTrayTestWindow.xaml</DependentUpon>
</Compile>
<Page Include="Styles\Controls.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -324,6 +328,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="UI\TaskTrayTestWindow.xaml" Condition="'$(Configuration)' == 'Debug'">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Compile Include="Services\ShortcutKeyDetector.cs" />
Expand Down
Loading

0 comments on commit f9c2105

Please sign in to comment.