private IImage GetIconWindows(IntPtr hIcon, UIEngine engine, bool large) { IconInfo iconInfo = new IconInfo(); if (true == GetIconInfo(hIcon, out iconInfo)) { BitmapInfo bitmapInfo = new BitmapInfo(); bitmapInfo.bmiHeader.biSize = Marshal.SizeOf(bitmapInfo); IntPtr dc = CreateCompatibleDC(IntPtr.Zero); int rez = GetDIBits(dc, iconInfo.hbmColor, 0, 0, null, ref bitmapInfo, Win32.DIB_RGB_COLORS); if (rez == 0) { DeleteDC(dc); return null; } byte[] pBuffer = new byte[bitmapInfo.bmiHeader.biWidth * bitmapInfo.bmiHeader.biHeight * 4]; byte[] buffer = new byte[bitmapInfo.bmiHeader.biWidth * bitmapInfo.bmiHeader.biHeight * 4]; rez = GetDIBits(dc, iconInfo.hbmColor, 0, (uint)bitmapInfo.bmiHeader.biHeight, buffer, ref bitmapInfo, Win32.DIB_RGB_COLORS); bool bAllAlphaTransparent = true; for (int y = 0; y < bitmapInfo.bmiHeader.biHeight; y++) { for (int x = 0; x < bitmapInfo.bmiHeader.biWidth; x++) { pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 0] = buffer[(bitmapInfo.bmiHeader.biHeight - y - 1) * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 2]; pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 1] = buffer[(bitmapInfo.bmiHeader.biHeight - y - 1) * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 1]; pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 2] = buffer[(bitmapInfo.bmiHeader.biHeight - y - 1) * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 0]; pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 3] = buffer[(bitmapInfo.bmiHeader.biHeight - y - 1) * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 3]; if ((false != bAllAlphaTransparent) && (pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 3] != 0x0)) { bAllAlphaTransparent = false; } } } rez = GetDIBits(dc, iconInfo.hbmMask, 0, 0, null, ref bitmapInfo, Win32.DIB_RGB_COLORS); if ((rez == 0) || (false == bAllAlphaTransparent)) { } else { byte[] colorBits = new byte[4096 * 4]; byte[] maskBits = new byte[4096 * 4]; BitmapInfo bmi = new BitmapInfo(); bmi.bmiHeader.biSize = Marshal.SizeOf(bmi); bmi.bmiHeader.biWidth = /*32;*/ bitmapInfo.bmiHeader.biWidth; bmi.bmiHeader.biHeight = /*32;*/ bitmapInfo.bmiHeader.biHeight; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; bmi.bmiHeader.biCompression = Win32.BI_RGB; // Extract the color bitmap int nBits = bitmapInfo.bmiHeader.biWidth * bitmapInfo.bmiHeader.biHeight * 32 / 8; GetDIBits(dc, iconInfo.hbmColor, 0, 32, colorBits, ref bmi, Win32.DIB_RGB_COLORS); // Extract the mask bitmap GetDIBits(dc, iconInfo.hbmMask, 0, 32, maskBits, ref bmi, Win32.DIB_RGB_COLORS); // Copy the mask alphas into the color bits for (int i = 0; i < nBits; i++) { colorBits[i + 0] = (byte)(colorBits[i + 0] | ((maskBits[i + 0] != 0) ? 0 : 0xff)); } memcpy(pBuffer, colorBits, bitmapInfo.bmiHeader.biWidth * bitmapInfo.bmiHeader.biHeight * 4); byte[] buffer1 = colorBits; for (int y = 0; y < bitmapInfo.bmiHeader.biHeight; y++) { for (int x = 0; x < bitmapInfo.bmiHeader.biWidth; x++) { pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 0] = buffer1[(bitmapInfo.bmiHeader.biHeight - y - 1) * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 2]; pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 1] = buffer1[(bitmapInfo.bmiHeader.biHeight - y - 1) * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 1]; pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 2] = buffer1[(bitmapInfo.bmiHeader.biHeight - y - 1) * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 0]; pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 3] = buffer1[(bitmapInfo.bmiHeader.biHeight - y - 1) * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 3]; } } } DeleteDC(dc); DeleteObject(iconInfo.hbmColor); DeleteObject(iconInfo.hbmMask); IImage resultImage = engine.CreateImage("icon_" + hIcon.ToInt64() + "_" + large, bitmapInfo.bmiHeader.biWidth, bitmapInfo.bmiHeader.biHeight, pBuffer); return resultImage; } return null; }
private static extern int GetDIBits(IntPtr hdc, IntPtr hbmp, uint uStartScan, uint cScanLines, [Out] byte[] lpvBits, ref BitmapInfo lpbmi, uint uUsage);
/// <summary> /// Loads letter iamge from win32 font. /// </summary> /// <param name="cache">should the iamge be cached in tga, xml files.</param> /// <returns>loader letter information.</returns> protected internal override LetterInfo Load(bool cache) { LetterInfo result = null; #if WINDOWS IntPtr bitmapHandle = IntPtr.Zero; byte[] textureData = null; try { if (null == PlatformWindows.SelectFont(this.font.fontRenderingDisplayContext, this.font.fontHandle)) { throw new Exception(); } TextMetric tm = new TextMetric(); if (false == PlatformWindows.GetTextMetrics(this.font.fontRenderingDisplayContext, out tm)) { throw new Exception(); } if (PlatformWindows.GDI_ERROR == PlatformWindows.SetTextAlign(this.font.fontRenderingDisplayContext, PlatformWindows.TA_LEFT | PlatformWindows.TA_TOP | PlatformWindows.TA_UPDATECP)) { throw new Exception(); } ABC[] abc = new ABC[1]; if (false == PlatformWindows.GetCharABCWidths(this.font.fontRenderingDisplayContext, (uint)this.character, (uint)this.character, abc)) { throw new Exception(); } this.offsetX = abc[0].abcA; this.width = (int)(abc[0].abcB + abc[0].abcC); BitmapInfo header = new BitmapInfo(); header.bmiHeader.biSize = Marshal.SizeOf(header); GlyphMetrics metrics = new GlyphMetrics(); MAT2 identity = new MAT2(); identity.eM12.value = 0; identity.eM21.value = 0; identity.eM11.value = 1; identity.eM22.value = 1; if (PlatformWindows.GDI_ERROR == PlatformWindows.GetGlyphOutline(this.font.fontRenderingDisplayContext, this.character, PlatformWindows.GGO_METRICS, out metrics, 0, IntPtr.Zero, ref identity)) { throw new Exception(); } header.bmiHeader.biWidth = metrics.gmBlackBoxX; header.bmiHeader.biHeight = -1 * metrics.gmBlackBoxY; header.bmiHeader.biPlanes = 1; header.bmiHeader.biBitCount = 32; header.bmiHeader.biCompression = 0;// BI_RGB; byte[] bitmapData = null; IntPtr bitmapDataPointer = IntPtr.Zero; bitmapHandle = PlatformWindows.CreateDIBSection(this.font.fontRenderingDisplayContext, ref header, /*DIB_RGB_COLORS*/0, out bitmapDataPointer, IntPtr.Zero, 0); if (IntPtr.Zero == bitmapHandle) { int err = Marshal.GetLastWin32Error(); throw new Exception(); } if (null == PlatformWindows.SelectObject(this.font.fontRenderingDisplayContext, bitmapHandle)) { throw new Exception(); } if (PlatformWindows.CLR_INVALID == PlatformWindows.SetBkColor(this.font.fontRenderingDisplayContext, new RGB(new byte[] { 0, 0, 0 }).ToInt32())) { throw new Exception(); } if (PlatformWindows.CLR_INVALID == PlatformWindows.SetTextColor(this.font.fontRenderingDisplayContext, new RGB(new byte[] { 0xff, 0xff, 0xff }).ToInt32())) { throw new Exception(); } if (0 == PlatformWindows.SetBkMode(this.font.fontRenderingDisplayContext, PlatformWindows.OPAQUE)) { throw new Exception(); } if (false == PlatformWindows.MoveToEx(this.font.fontRenderingDisplayContext, 0 - abc[0].abcA, -1 * (tm.tmAscent - metrics.gmptGlyphOrigin.y), IntPtr.Zero)) { throw new Exception(); } this.offsetY = tm.tmAscent - metrics.gmptGlyphOrigin.y - tm.tmDescent - 1; String str = "" + this.character; RECT rect = new RECT(); if (false == PlatformWindows.ExtTextOut(this.font.fontRenderingDisplayContext, 0, 0, (uint)0, ref rect, str, 1, null)) { throw new Exception(); } if (0 == PlatformWindows.SetBkMode(this.font.fontRenderingDisplayContext, PlatformWindows.TRANSPARENT)) { throw new Exception(); } int bitmapWidth = header.bmiHeader.biWidth; int bitmapHeight = -header.bmiHeader.biHeight; int textureWidth = RoundToPowerOf2(bitmapWidth); int textureHeight = RoundToPowerOf2(bitmapHeight); bitmapData = new byte[bitmapWidth * bitmapHeight * 4]; Marshal.Copy(bitmapDataPointer, bitmapData, 0, bitmapWidth * bitmapHeight * 4); textureData = new byte[4 * textureWidth * textureHeight]; for (int j = 0; j < textureHeight; j++) { for (int i = 0; i < textureWidth; i++) { textureData[4 * (i + j * textureWidth) + 0] = 0xff; textureData[4 * (i + j * textureWidth) + 1] = 0xff; textureData[4 * (i + j * textureWidth) + 2] = 0xff; textureData[4 * (i + j * textureWidth) + 3] = (byte)((i >= bitmapWidth || j >= bitmapHeight) ? 0 : bitmapData[(i + bitmapWidth * j) * 4 + 2]); } } if (true == cache) { result = new LetterInfo(); result.width = bitmapWidth; result.height = bitmapHeight; result.bytes = textureData; result.textureWidth = textureWidth; result.textureHeight = textureHeight; } else { this.image = this.engine.CreateImage("Letter [" + this.character + "]", textureWidth, textureHeight, textureData); // this.internalImage = true; } this.textureWidth = textureWidth; this.textureHeight = textureHeight; } catch (Exception) { this.engine.DeleteImage(ref this.image); } if (null != bitmapHandle) { PlatformWindows.DeleteObject(bitmapHandle); } this.loaded = true; #endif return result; }
internal static extern IntPtr CreateDIBSection(IntPtr hdc, [In] ref BitmapInfo pbmi, uint iUsage, out IntPtr ppvBits, IntPtr hSection, uint dwOffset);
private IImage GetIconWindows(IntPtr hIcon, UIEngine engine, bool large) { IconInfo iconInfo = new IconInfo(); if (true == GetIconInfo(hIcon, out iconInfo)) { BitmapInfo bitmapInfo = new BitmapInfo(); bitmapInfo.bmiHeader.biSize = Marshal.SizeOf(bitmapInfo); IntPtr dc = CreateCompatibleDC(IntPtr.Zero); int rez = GetDIBits(dc, iconInfo.hbmColor, 0, 0, null, ref bitmapInfo, Win32.DIB_RGB_COLORS); if (rez == 0) { DeleteDC(dc); return(null); } byte[] pBuffer = new byte[bitmapInfo.bmiHeader.biWidth * bitmapInfo.bmiHeader.biHeight * 4]; byte[] buffer = new byte[bitmapInfo.bmiHeader.biWidth * bitmapInfo.bmiHeader.biHeight * 4]; rez = GetDIBits(dc, iconInfo.hbmColor, 0, (uint)bitmapInfo.bmiHeader.biHeight, buffer, ref bitmapInfo, Win32.DIB_RGB_COLORS); bool bAllAlphaTransparent = true; for (int y = 0; y < bitmapInfo.bmiHeader.biHeight; y++) { for (int x = 0; x < bitmapInfo.bmiHeader.biWidth; x++) { pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 0] = buffer[(bitmapInfo.bmiHeader.biHeight - y - 1) * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 2]; pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 1] = buffer[(bitmapInfo.bmiHeader.biHeight - y - 1) * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 1]; pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 2] = buffer[(bitmapInfo.bmiHeader.biHeight - y - 1) * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 0]; pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 3] = buffer[(bitmapInfo.bmiHeader.biHeight - y - 1) * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 3]; if ((false != bAllAlphaTransparent) && (pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 3] != 0x0)) { bAllAlphaTransparent = false; } } } rez = GetDIBits(dc, iconInfo.hbmMask, 0, 0, null, ref bitmapInfo, Win32.DIB_RGB_COLORS); if ((rez == 0) || (false == bAllAlphaTransparent)) { } else { byte[] colorBits = new byte[4096 * 4]; byte[] maskBits = new byte[4096 * 4]; BitmapInfo bmi = new BitmapInfo(); bmi.bmiHeader.biSize = Marshal.SizeOf(bmi); bmi.bmiHeader.biWidth = /*32;*/ bitmapInfo.bmiHeader.biWidth; bmi.bmiHeader.biHeight = /*32;*/ bitmapInfo.bmiHeader.biHeight; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; bmi.bmiHeader.biCompression = Win32.BI_RGB; // Extract the color bitmap int nBits = bitmapInfo.bmiHeader.biWidth * bitmapInfo.bmiHeader.biHeight * 32 / 8; GetDIBits(dc, iconInfo.hbmColor, 0, 32, colorBits, ref bmi, Win32.DIB_RGB_COLORS); // Extract the mask bitmap GetDIBits(dc, iconInfo.hbmMask, 0, 32, maskBits, ref bmi, Win32.DIB_RGB_COLORS); // Copy the mask alphas into the color bits for (int i = 0; i < nBits; i++) { colorBits[i + 0] = (byte)(colorBits[i + 0] | ((maskBits[i + 0] != 0) ? 0 : 0xff)); } memcpy(pBuffer, colorBits, bitmapInfo.bmiHeader.biWidth * bitmapInfo.bmiHeader.biHeight * 4); byte[] buffer1 = colorBits; for (int y = 0; y < bitmapInfo.bmiHeader.biHeight; y++) { for (int x = 0; x < bitmapInfo.bmiHeader.biWidth; x++) { pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 0] = buffer1[(bitmapInfo.bmiHeader.biHeight - y - 1) * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 2]; pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 1] = buffer1[(bitmapInfo.bmiHeader.biHeight - y - 1) * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 1]; pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 2] = buffer1[(bitmapInfo.bmiHeader.biHeight - y - 1) * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 0]; pBuffer[y * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 3] = buffer1[(bitmapInfo.bmiHeader.biHeight - y - 1) * bitmapInfo.bmiHeader.biWidth * 4 + x * 4 + 3]; } } } DeleteDC(dc); DeleteObject(iconInfo.hbmColor); DeleteObject(iconInfo.hbmMask); IImage resultImage = engine.CreateImage("icon_" + hIcon.ToInt64() + "_" + large, bitmapInfo.bmiHeader.biWidth, bitmapInfo.bmiHeader.biHeight, pBuffer); return(resultImage); } return(null); }