Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2520: added license headers

File size: 1.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Drawing;
24using System.Drawing.Imaging;
25using System.IO;
26using System.Linq;
27using Google.Protobuf;
28using HEAL.Fossil;
29
30namespace HeuristicLab.Persistence {
31  [Transformer("D0ADB806-2DFD-459D-B5DA-14B5F1152534", 404)]
32  [StorableType("B13D6153-E71D-4B76-9893-81D3570403E8")]
33  internal sealed class BitmapTransformer : BoxTransformer<Bitmap> {
34    protected override void Populate(Box box, Bitmap value, Mapper mapper) {
35      lock (value)
36        using (var ms = new MemoryStream()) {
37          value.Save(ms, ImageFormat.Png);
38          box.Bytes = ByteString.CopyFrom(ms.ToArray());
39        }
40    }
41
42    protected override Bitmap Extract(Box box, Type type, Mapper mapper) {
43      using (var ms = new MemoryStream()) {
44        ms.Write(box.Bytes.ToArray(), 0, box.Bytes.Length);
45        ms.Seek(0, SeekOrigin.Begin);
46        return new Bitmap(ms);
47      }
48    }
49  }
50}
Note: See TracBrowser for help on using the repository browser.