Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ImageFile.cs
1using System.IO;
2using System.Threading.Tasks;
3using System.Xml;
4using SkiaSharp;
5using Waher.Content;
7
9{
13 public class ImageFile : Image
14 {
15 private StringAttribute fileName;
16
22 public ImageFile(Layout2DDocument Document, ILayoutElement Parent)
23 : base(Document, Parent)
24 {
25 }
26
30 public override string LocalName => "ImageFile";
31
36 {
37 get => this.fileName;
38 set => this.fileName = value;
39 }
40
45 public override Task FromXml(XmlElement Input)
46 {
47 this.fileName = new StringAttribute(Input, "fileName", this.Document);
48 return base.FromXml(Input);
49 }
50
55 public override void ExportAttributes(XmlWriter Output)
56 {
57 base.ExportAttributes(Output);
58
59 this.fileName?.Export(Output);
60 }
61
68 public override ILayoutElement Create(Layout2DDocument Document, ILayoutElement Parent)
69 {
70 return new ImageFile(Document, Parent);
71 }
72
77 public override void CopyContents(ILayoutElement Destination)
78 {
79 base.CopyContents(Destination);
80
81 if (Destination is ImageFile Dest)
82 Dest.fileName = this.fileName?.CopyIfNotPreset(Destination.Document);
83 }
84
91 protected override async Task<SKImage> LoadImage(DrawingState State)
92 {
93 string FileName = await this.fileName.Evaluate(State.Session, string.Empty);
94 if (!string.IsNullOrEmpty(FileName) && File.Exists(FileName))
95 {
96 byte[] Bin = await Resources.ReadAllBytesAsync(FileName);
97 SKBitmap Bitmap = SKBitmap.Decode(Bin);
98 return SKImage.FromBitmap(Bitmap);
99 }
100 else
101 return null;
102 }
103
108 public override void ExportStateAttributes(XmlWriter Output)
109 {
110 base.ExportStateAttributes(Output);
111
112 this.fileName?.ExportState(Output);
113 }
114 }
115}
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static async Task< byte[]> ReadAllBytesAsync(string FileName)
Reads a binary file asynchronously.
Definition: Resources.cs:183
Contains a 2D layout document.
void Export(XmlWriter Output)
Exports the attribute.
Definition: Attribute.cs:187
void ExportState(XmlWriter Output)
Exports the state of the attribute.
Definition: Attribute.cs:199
StringAttribute CopyIfNotPreset(Layout2DDocument ForDocument)
Copies the attribute object if undefined, or defined by an expression. Returns a reference to itself,...
Variables Session
Current session.
Definition: DrawingState.cs:91
override void CopyContents(ILayoutElement Destination)
Copies contents (attributes and children) to the destination element.
Definition: ImageFile.cs:77
StringAttribute FileNameAttribute
Filename
Definition: ImageFile.cs:36
override ILayoutElement Create(Layout2DDocument Document, ILayoutElement Parent)
Creates a new instance of the layout element.
Definition: ImageFile.cs:68
override Task FromXml(XmlElement Input)
Populates the element (including children) with information from its XML definition.
Definition: ImageFile.cs:45
ImageFile(Layout2DDocument Document, ILayoutElement Parent)
An image defined in a file.
Definition: ImageFile.cs:22
override void ExportStateAttributes(XmlWriter Output)
Exports the local attributes of the current element.
Definition: ImageFile.cs:108
override string LocalName
Local name of type of element.
Definition: ImageFile.cs:30
override async Task< SKImage > LoadImage(DrawingState State)
Loads the image defined by the element.
Definition: ImageFile.cs:91
override void ExportAttributes(XmlWriter Output)
Exports attributes to XML.
Definition: ImageFile.cs:55
Abstract base class for images.
Definition: Image.cs:12
Layout2DDocument Document
Layout document.
Base interface for all layout elements.
Layout2DDocument Document
Layout document.