Files
Libation/Source/LibationWinForms/WinFormsUtil.cs
T
Cursor Agentandrmcrackan 7b0bd603a6 Draw WinForms Liberate icons from the shared generator
Delete the 24 pre-baked stoplight PNGs and their resx/designer entries, and
register the dark-mode probe the shared generator needs to pick a palette.

The shared renderings are supersampled, so DrawButtonImage takes the scale they
were rendered at and keeps drawing them at their logical size.

Co-authored-by: rmcrackan <rmcrackan@gmail.com>
2026-08-13 05:49:53 +00:00

49 lines
1.2 KiB
C#

using Dinah.Core.WindowsDesktop.Drawing;
using LibationFileManager;
using System.Drawing;
using System.Windows.Forms;
namespace LibationWinForms;
internal static class WinFormsUtil
{
private const float BaseDpi = 96;
private static Bitmap? defaultImage;
public static Image TryLoadImageOrDefault(byte[]? picture, PictureSize defaultSize = PictureSize.Native)
{
if (picture?.Length is null or 0)
return getDefaultImage();
try
{
return ImageReader.ToImage(picture);
}
catch
{
return getDefaultImage();
}
Image getDefaultImage()
{
if (defaultImage is null)
{
using var ms = new System.IO.MemoryStream(PictureStorage.GetDefaultImage(defaultSize));
defaultImage = new Bitmap(ms);
}
return defaultImage;
}
}
public static int DpiScale(this Control control, int value, float additionalScaleFactor = 1)
=> (int)float.Round(control.DeviceDpi / BaseDpi * value * additionalScaleFactor);
public static int DpiUnscale(this Control control, int value)
=> (int)float.Round(BaseDpi / control.DeviceDpi * value);
public static float ScaleX(this Graphics control, float value)
=> control.DpiX / BaseDpi * value;
public static float ScaleY(this Graphics control, float value)
=> control.DpiY / BaseDpi * value;
}