/ src / common / FilePreviewCommon / BgcodeThumbnail.cs
BgcodeThumbnail.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System.Drawing;
 6  using System.IO;
 7  
 8  namespace Microsoft.PowerToys.FilePreviewCommon
 9  {
10      /// <summary>
11      /// Represents a bgcode thumbnail.
12      /// </summary>
13      public class BgcodeThumbnail
14      {
15          /// <summary>
16          /// Gets the bgcode thumbnail image format.
17          /// </summary>
18          public BgcodeThumbnailFormat Format { get; }
19  
20          /// <summary>
21          /// Gets the bgcode thumbnail image data.
22          /// </summary>
23          public byte[] Data { get; }
24  
25          /// <summary>
26          /// Initializes a new instance of the <see cref="BgcodeThumbnail"/> class.
27          /// </summary>
28          /// <param name="format">The bgcode thumbnail image format.</param>
29          /// <param name="data">The bgcode thumbnail image data.</param>
30          public BgcodeThumbnail(BgcodeThumbnailFormat format, byte[] data)
31          {
32              Format = format;
33              Data = data;
34          }
35  
36          /// <summary>
37          /// Gets a <see cref="Bitmap"/> representing this thumbnail.
38          /// </summary>
39          /// <returns>A <see cref="Bitmap"/> representing this thumbnail.</returns>
40          public Bitmap? GetBitmap()
41          {
42              switch (Format)
43              {
44                  case BgcodeThumbnailFormat.JPG:
45                  case BgcodeThumbnailFormat.PNG:
46                      return BitmapFromByteArray();
47  
48                  case BgcodeThumbnailFormat.QOI:
49                      return BitmapFromQoiByteArray();
50  
51                  default:
52                      return null;
53              }
54          }
55  
56          private Bitmap BitmapFromByteArray()
57          {
58              return new Bitmap(new MemoryStream(Data));
59          }
60  
61          private Bitmap BitmapFromQoiByteArray()
62          {
63              return QoiImage.FromStream(new MemoryStream(Data));
64          }
65      }
66  }