Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2520_PersistenceReintegration/HeuristicLab.Persistence/3.3/Transformers/BitmapTransformer.cs @ 16482

Last change on this file since 16482 was 16482, checked in by gkronber, 5 years ago

#2520: added two custom transformers (for Fonts and Bitmaps) and registered types that are available in .NET Framework but not in .NET Standard 2.0 and added.

File size: 930 bytes
Line 
1using System;
2using System.Drawing;
3using System.Drawing.Imaging;
4using System.IO;
5using System.Linq;
6using Google.Protobuf;
7using HEAL.Fossil;
8
9namespace HeuristicLab.Persistence {
10  [Transformer("D0ADB806-2DFD-459D-B5DA-14B5F1152534", 404)]
11  [StorableType("B13D6153-E71D-4B76-9893-81D3570403E8")]
12  internal sealed class BitmapTransformer : BoxTransformer<Bitmap> {
13    protected override void Populate(Box box, Bitmap value, Mapper mapper) {
14      lock (value)
15        using (var ms = new MemoryStream()) {
16          value.Save(ms, ImageFormat.Png);
17          box.Bytes = ByteString.CopyFrom(ms.ToArray());
18        }
19    }
20
21    protected override Bitmap Extract(Box box, Type type, Mapper mapper) {
22      using (var ms = new MemoryStream()) {
23        ms.Write(box.Bytes.ToArray(), 0, box.Bytes.Length);
24        ms.Seek(0, SeekOrigin.Begin);
25        return new Bitmap(ms);
26      }
27    }
28  }
29
30}
Note: See TracBrowser for help on using the repository browser.