aboutsummaryrefslogtreecommitdiff
path: root/src/dtf/WixToolset.Dtf.Resources/BitmapResource.cs
blob: 42c886cb3f727a2de73634f3a78aa8c4a636fedf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 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.

namespace WixToolset.Dtf.Resources
{
    using System;
    using System.IO;

    /// <summary>
    /// A subclass of Resource which provides specific methods for manipulating the resource data.
    /// </summary>
    /// <remarks>
    /// The resource is of type <see cref="ResourceType.Bitmap"/> (RT_GROUPICON).
    /// </remarks>
    public sealed class BitmapResource : Resource
    {
        private const int SizeOfBitmapFileHeader = 14; // this is the sizeof(BITMAPFILEHEADER)

        /// <summary>
        /// Creates a new BitmapResource object without any data. The data can be later loaded from a file.
        /// </summary>
        /// <param name="name">Name of the resource. For a numeric resource identifier, prefix the decimal number with a "#".</param>
        /// <param name="locale">Locale of the resource</param>
        public BitmapResource(string name, int locale)
            : this(name, locale, null)
        {
        }

        /// <summary>
        /// Creates a new BitmapResource object with data. The data can be later saved to a file.
        /// </summary>
        /// <param name="name">Name of the resource. For a numeric resource identifier, prefix the decimal number with a "#".</param>
        /// <param name="locale">Locale of the resource</param>
        /// <param name="data">Raw resource data</param>
        public BitmapResource(string name, int locale, byte[] data)
            : base(ResourceType.Bitmap, name, locale, data)
        {
        }

        /// <summary>
        /// Reads the bitmap from a .bmp file.
        /// </summary>
        /// <param name="path">Path to a bitmap file (.bmp).</param>
        public void ReadFromFile(string path)
        {
            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                // Move past the BITMAPFILEHEADER, and copy the rest of the bitmap as the resource data. Resource
                // functions expect only the BITMAPINFO struct which exists just beyond the BITMAPFILEHEADER
                // struct in bitmap files.
                fs.Seek(BitmapResource.SizeOfBitmapFileHeader, SeekOrigin.Begin);

                base.Data = new byte[fs.Length - BitmapResource.SizeOfBitmapFileHeader];
                fs.Read(base.Data, 0, base.Data.Length);
            }
        }
    }
}