예제 #1
0
        ///<summary>Returns a Png Bitmap from the currently loaded ShellStyle.dll</summary>
        public Bitmap GetResourcePNG(string resourceName)
        {
            // the resource size includes some header information
            const int FILE_HEADER_BYTES = 40;
            Bitmap    tmpNoAlpha        = Bitmap.FromResource(hModule, "#" + resourceName);

            IntPtr hResource    = this["#" + resourceName, (IntPtr)RC_ENUM.RT_BITMAP];
            int    resourceSize = SizeofResource(hModule, hResource);
            Bitmap bitmap       = new Bitmap(tmpNoAlpha.Width, tmpNoAlpha.Height, PixelFormat.Format32bppArgb);

            IntPtr hLoadedResource = LoadResource(hModule, hResource);

            byte[]   bitmapBytes = new byte[resourceSize];
            GCHandle gcHandle    = GCHandle.Alloc(bitmapBytes, GCHandleType.Pinned);

            IntPtr firstCopyElement = Marshal.UnsafeAddrOfPinnedArrayElement(bitmapBytes, 0);

            CopyMemory(firstCopyElement, hLoadedResource, resourceSize);

            FreeResource(hLoadedResource);

            Rectangle  copyArea  = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            BitmapData alphaBits = bitmap.LockBits(
                copyArea,
                ImageLockMode.WriteOnly,
                PixelFormat.Format32bppArgb
                );

            firstCopyElement =
                Marshal.UnsafeAddrOfPinnedArrayElement(
                    bitmapBytes,
                    FILE_HEADER_BYTES
                    );
            CopyMemory(
                alphaBits.Scan0,
                firstCopyElement,
                resourceSize - FILE_HEADER_BYTES
                );

            gcHandle.Free();
            bitmap.UnlockBits(alphaBits);
            Gdi32.GdiFlush();
            // flip bits (not sure why this is needed at the moment..)
            bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
            return(bitmap);
        }