GcodeThumbnail.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; 6 using System.Drawing; 7 using System.IO; 8 9 namespace Microsoft.PowerToys.FilePreviewCommon 10 { 11 /// <summary> 12 /// Represents a gcode thumbnail. 13 /// </summary> 14 public class GcodeThumbnail 15 { 16 /// <summary> 17 /// Gets the gcode thumbnail image format. 18 /// </summary> 19 public GcodeThumbnailFormat Format { get; } 20 21 /// <summary> 22 /// Gets the gcode thumbnail image data in base64. 23 /// </summary> 24 public string Data { get; } 25 26 /// <summary> 27 /// Initializes a new instance of the <see cref="GcodeThumbnail"/> class. 28 /// </summary> 29 /// <param name="format">The gcode thumbnail image format.</param> 30 /// <param name="data">The gcode thumbnail image data in base64.</param> 31 public GcodeThumbnail(GcodeThumbnailFormat format, string data) 32 { 33 Format = format; 34 Data = data; 35 } 36 37 /// <summary> 38 /// Gets a <see cref="Bitmap"/> representing this thumbnail. 39 /// </summary> 40 /// <returns>A <see cref="Bitmap"/> representing this thumbnail.</returns> 41 public Bitmap? GetBitmap() 42 { 43 switch (Format) 44 { 45 case GcodeThumbnailFormat.JPG: 46 case GcodeThumbnailFormat.PNG: 47 return BitmapFromBase64String(); 48 49 case GcodeThumbnailFormat.QOI: 50 return BitmapFromQoiBase64String(); 51 52 default: 53 return null; 54 } 55 } 56 57 private Bitmap BitmapFromBase64String() 58 { 59 var bitmapBytes = Convert.FromBase64String(Data); 60 61 return new Bitmap(new MemoryStream(bitmapBytes)); 62 } 63 64 private Bitmap BitmapFromQoiBase64String() 65 { 66 var bitmapBytes = Convert.FromBase64String(Data); 67 68 return QoiImage.FromStream(new MemoryStream(bitmapBytes)); 69 } 70 } 71 }