55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using System.Reflection;
|
|
|
|
namespace Pilz.UI.WinForms.Symbols;
|
|
|
|
public abstract class SymbolFactory<TSymbols> : ISymbolFactory<TSymbols>
|
|
{
|
|
public abstract string GetImageRessourcePath(TSymbols svgImage);
|
|
public abstract Assembly GetImageResourceAssembly();
|
|
|
|
protected virtual Size ResolveCommonSize(SymbolSize size)
|
|
{
|
|
return size switch
|
|
{
|
|
SymbolSize.Default => Size.Empty,
|
|
SymbolSize.Small => new Size(16, 16),
|
|
SymbolSize.Medium => new Size(20, 20),
|
|
SymbolSize.Large => new Size(32, 32),
|
|
_ => new Size((int)size, (int)size),
|
|
};
|
|
}
|
|
|
|
public virtual Stream? GetImageRessourceStream(TSymbols svgImage)
|
|
{
|
|
var asm = GetImageResourceAssembly();
|
|
var path = GetImageRessourcePath(svgImage);
|
|
return asm.GetManifestResourceStream(path);
|
|
}
|
|
|
|
public virtual Image? GetImage(TSymbols svgImage, SymbolSize size)
|
|
{
|
|
return GetImage(svgImage, ResolveCommonSize(size));
|
|
}
|
|
|
|
public virtual Image? GetImage(TSymbols svgImage, Size size)
|
|
{
|
|
using var stream = GetImageRessourceStream(svgImage);
|
|
|
|
if (stream is null)
|
|
return null;
|
|
|
|
var img = Image.FromStream(stream);
|
|
|
|
if (!size.IsEmpty)
|
|
{
|
|
var img2 = new Bitmap(size.Width, size.Height);
|
|
using var g = Graphics.FromImage(img2);
|
|
g.DrawImage(img2, 0, 0, size.Width, size.Height);
|
|
img.Dispose();
|
|
img = img2;
|
|
}
|
|
|
|
return img;
|
|
}
|
|
}
|