rework SymbolFactory
- rename & move to Pilz.UI..Telerik.Symbols.RadSymbolFactory - make default implementaiton available at Pilz.UI.Symbols.SymbolFactory - add interfaces
This commit is contained in:
53
Pilz.UI/Symbols/SymbolFactory.cs
Normal file
53
Pilz.UI/Symbols/SymbolFactory.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace Pilz.UI.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.Small => new Size(16, 16),
|
||||
SymbolSize.Medium => new Size(20, 20),
|
||||
SymbolSize.Large => new Size(32, 32),
|
||||
_ => Size.Empty,
|
||||
};
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user