Exemplo n.º 1
0
        /// <summary>
        /// Create a bitmap structure with bits from this raw icon image.
        /// </summary>
        /// <returns>An array of bytes that represent data to create a bitmap.</returns>
        /// <remarks></remarks>
        private byte[] _makeBitmap()
        {
            byte[] _makeBitmapRet = default;
            if (!IsPngFormat)
            {
                _makeBitmapRet = _image;
                return(_makeBitmapRet);
            }

            IntPtr bmp  = default;
            var    hbmp = Resources.MakeDIBSection((Bitmap)ToImage(), ref bmp);
            var    mm   = new SafePtr();
            var    bm   = new BITMAPINFOHEADER();
            int    maskSize;
            int    w = _entry.cWidth;
            int    h = _entry.cHeight;

            if (w == 0)
            {
                w = 256;
            }
            if (h == 0)
            {
                h = 256;
            }
            bm.biSize      = 40;
            bm.biWidth     = w;
            bm.biHeight    = h * 2;
            bm.biPlanes    = 1;
            bm.biBitCount  = 32;
            bm.biSizeImage = w * h * 4;
            maskSize       = (int)(Math.Max(w, 32) * h / 8d);
            mm.Alloc(bm.biSizeImage + 40 + maskSize);
            var ptr1 = mm.DangerousGetHandle() + 40;
            var ptr2 = mm.DangerousGetHandle() + 40 + bm.biSizeImage;

            Marshal.StructureToPtr(bm, mm.DangerousGetHandle(), false);
            Native.MemCpy(bmp, ptr1, bm.biSizeImage);
            bm = mm.ToStruct <BITMAPINFOHEADER>();
            _setMask(ptr1, ptr2, w, h);
            _entry.dwImageSize = (int)mm.Length;
            _makeBitmapRet     = (byte[])mm;
            mm.Free();
            NativeShell.DeleteObject(hbmp);
            return(_makeBitmapRet);
        }