Exemple #1
0
        private async Task <CursorData> GetCursorDataAsync(ResourceLanguage language)
        {
            var buffer = await GetBytesAsync(language).ConfigureAwait(false);

            byte[] dib;
            bool   isPNG;
            ushort hotspotX;
            ushort hotspotY;
            ushort width;
            ushort height;
            byte   colorCount;

            using (var mem = new MemoryStream(buffer))
            {
                hotspotX = await mem.ReadUInt16Async().ConfigureAwait(false);

                hotspotY = await mem.ReadUInt16Async().ConfigureAwait(false);

                using (var dibMem = new MemoryStream(buffer.Length))
                {
                    mem.CopyTo(dibMem, 4096);
                    dibMem.Seek(0, SeekOrigin.Begin);

                    dib   = dibMem.ToArray();
                    isPNG = GraphicUtils.IsPNG(dib);

                    if (!isPNG)
                    {
                        var header = await dibMem.ReadStructAsync <BITMAPINFOHEADER>().ConfigureAwait(false);

                        width      = header.biWidth.ToUInt16();
                        height     = (header.biHeight / 2).ToUInt16();
                        colorCount = Convert.ToByte(header.biBitCount);
                    }
                    else
                    {
                        using (var png = System.Drawing.Image.FromStream(dibMem))
                        {
                            width      = png.Width.ToUInt16();
                            height     = png.Height.ToUInt16();
                            colorCount = 32;
                        }
                    }
                }
            }

            var result = new CursorData()
            {
                HotspotX   = hotspotX,
                HotspotY   = hotspotY,
                Width      = width,
                Height     = height,
                ColorCount = colorCount,
                DIB        = dib,
                IsPNG      = isPNG
            };

            return(result);
        }
Exemple #2
0
        private async Task <IconData> GetIconDataAsync(ResourceLanguage language)
        {
            var buffer = await GetBytesAsync(language).ConfigureAwait(false);

            bool   isPNG;
            ushort width;
            ushort height;
            byte   colorCount;

            byte[] dib;

            using (var mem = new MemoryStream(buffer))
            {
                isPNG = GraphicUtils.IsPNG(buffer);

                if (!isPNG)
                {
                    var header = await mem.ReadStructAsync <BITMAPINFOHEADER>().ConfigureAwait(false);

                    width      = Convert.ToUInt16(header.biWidth);
                    height     = Convert.ToUInt16(header.biHeight / 2);
                    colorCount = Convert.ToByte(header.biBitCount);
                    dib        = mem.ToArray();
                }
                else
                {
                    using (var png = System.Drawing.Image.FromStream(mem))
                    {
                        width      = Convert.ToUInt16(png.Width);
                        height     = Convert.ToUInt16(png.Height);
                        colorCount = 32;
                        dib        = mem.ToArray();
                    }
                }
            }

            var result = new IconData()
            {
                Width      = width,
                Height     = height,
                ColorCount = colorCount,
                DIB        = dib,
                IsPNG      = isPNG
            };

            return(result);
        }