| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.Dtf.Resources |
| 4 | { |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// A subclass of Resource which provides specific methods for manipulating the resource data. |
| 10 | /// </summary> |
| 11 | /// <remarks> |
| 12 | /// The resource is of type <see cref="ResourceType.Bitmap"/> (RT_GROUPICON). |
| 13 | /// </remarks> |
| 14 | public sealed class BitmapResource : Resource |
| 15 | { |
| 16 | private const int SizeOfBitmapFileHeader = 14; // this is the sizeof(BITMAPFILEHEADER) |
| 17 | |
| 18 | /// <summary> |
| 19 | /// Creates a new BitmapResource object without any data. The data can be later loaded from a file. |
| 20 | /// </summary> |
| 21 | /// <param name="name">Name of the resource. For a numeric resource identifier, prefix the decimal number with a "#".</param> |
| 22 | /// <param name="locale">Locale of the resource</param> |
| 23 | public BitmapResource(string name, int locale) |
| 24 | : this(name, locale, null) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | /// <summary> |
| 29 | /// Creates a new BitmapResource object with data. The data can be later saved to a file. |
| 30 | /// </summary> |
| 31 | /// <param name="name">Name of the resource. For a numeric resource identifier, prefix the decimal number with a "#".</param> |
| 32 | /// <param name="locale">Locale of the resource</param> |
| 33 | /// <param name="data">Raw resource data</param> |
| 34 | public BitmapResource(string name, int locale, byte[] data) |
| 35 | : base(ResourceType.Bitmap, name, locale, data) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | /// <summary> |
| 40 | /// Reads the bitmap from a .bmp file. |
| 41 | /// </summary> |
| 42 | /// <param name="path">Path to a bitmap file (.bmp).</param> |
| 43 | public void ReadFromFile(string path) |
| 44 | { |
| 45 | using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) |
| 46 | { |
| 47 | // Move past the BITMAPFILEHEADER, and copy the rest of the bitmap as the resource data. Resource |
| 48 | // functions expect only the BITMAPINFO struct which exists just beyond the BITMAPFILEHEADER |
| 49 | // struct in bitmap files. |
| 50 | fs.Seek(BitmapResource.SizeOfBitmapFileHeader, SeekOrigin.Begin); |
| 51 | |
| 52 | base.Data = new byte[fs.Length - BitmapResource.SizeOfBitmapFileHeader]; |
| 53 | fs.Read(base.Data, 0, base.Data.Length); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |