public static Bitmap getFileChooserBitmap() { if (hmodShell32 != IntPtr.Zero) { // Code copied from ShellFolder2.cpp Java_sun_awt_shell_Win32ShellFolder2_getFileChooserBitmapBits // Get a handle to an icon. SafeGdiObjectHandle hBitmap = null; try { hBitmap = isVista ? LoadImage(hmodShell32, "IDB_TB_SH_DEF_16", IMAGE_BITMAP, 0, 0, 0) : LoadImage(hmodShell32, (IntPtr)216, IMAGE_BITMAP, 0, 0, 0); if (hBitmap == null && hmodComctl32 != IntPtr.Zero) { hBitmap = LoadImage(hmodComctl32, (IntPtr)124, IMAGE_BITMAP, 0, 0, 0); } if (hBitmap != null) { BITMAPINFO bmi = new BITMAPINFO(); GetObject(hBitmap, Marshal.SizeOf(bmi), ref bmi); int width = bmi.biWidth; int height = bmi.biHeight; bmi.biSize = 40; bmi.biHeight = -bmi.biHeight; bmi.biPlanes = 1; bmi.biBitCount = 32; bmi.biCompression = 0; if (width == 0 || height == 0) { return(null); } using (SafeDeviceContextHandle dc = SafeDeviceContextHandle.Get()) { int[] data = new int[width * height]; GetDIBits(dc, hBitmap, (uint)0, (uint)height, data, ref bmi, 0); Bitmap bitmap = new Bitmap(data.Length / 16, 16, PixelFormat.Format32bppArgb); BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); Marshal.Copy(data, 0, bitmapData.Scan0, data.Length); bitmap.UnlockBits(bitmapData); return(bitmap); } } } finally { if (hBitmap != null) { hBitmap.Close(); } } } return(null); }
public static Bitmap getShell32IconResourceAsBitmap(int iconID) { if (hmodShell32 == IntPtr.Zero) { return(null); } using (SafeGdiObjectHandle hicon = LoadImage(hmodShell32, (IntPtr)iconID, IMAGE_ICON, 16, 16, 0)) { if (hicon != null) { return(getIconBits(hicon.DangerousGetHandle(), 16)); } } return(null); }
/// <summary> /// Select a font used to draw text on this <see cref="Canvas"/> class. /// </summary> /// <param name="fontName">The name of the font used to draw on the canvas.</param> /// <param name="fontSize">The size of the font used to draw on the canvas.</param> /// <param name="fontStyle">The style of the font used to draw on the canvas.</param> /// <param name="unit">The GraphicsUnit of the new font. </param> public void SelectFont(string fontName, float fontSize, System.Drawing.FontStyle fontStyle, System.Drawing.GraphicsUnit unit) { if (this.hdc != null) { if (this.hfont != null) { this.hfont.Dispose(); this.hfont = null; } // get a handle to the Font object. using (System.Drawing.Font font = new System.Drawing.Font(fontName, fontSize, fontStyle, unit)) { this.hfont = new SafeGdiObjectHandle(font.ToHfont(), true); NativeMethods.SelectObject(this.hdc, this.hfont); } // select color NativeMethods.SetBkMode(this.hdc, NativeMethods.TRANSPARENT); NativeMethods.SetTextColor(this.hdc, System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.Black)); } }
private void ReleaseCanvas() { if (this.hdc != null) { NativeMethods.SetMapMode(this.hdc, this.oldMapMode); NativeMethods.SetBkColor(this.hdc, this.oldBkColor); this.hdc.Dispose(); this.hdc = null; } if (this.bitmap != null) { this.bitmap.Dispose(); this.bitmap = null; } if (this.hfont != null) { this.hfont.Dispose(); this.hfont = null; } }
/// <summary> /// Initializes a new instance of the <see cref="Canvas"/> class. /// </summary> /// <param name="width">The canvas width.</param> /// <param name="height">The canvas height.</param> public Canvas(int width, int height) { try { // allocate enough to fit all characters this.width = width <= 0 ? 2000 : width; this.height = height; // get handle to device context. this.hdc = NativeMethods.CreateCompatibleDC(new SafeHdcHandle()); if (this.hdc.IsInvalid) { throw new InvalidOperationException(Properties.Resources.E_InvalidCanvasOperation); } // create destination bitmap this.bitmap = NativeMethods.CreateCompatibleBitmap(this.hdc, this.width, this.height); if (this.bitmap.IsInvalid) { throw new InvalidOperationException(Properties.Resources.E_InvalidCanvasOperation); } NativeMethods.SelectObject(this.hdc, this.bitmap); // select color this.oldMapMode = NativeMethods.SetBkMode(this.hdc, NativeMethods.TRANSPARENT); this.oldBkColor = NativeMethods.SetTextColor(this.hdc, System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.Black)); // erase background this.Clear(); } catch { this.ReleaseCanvas(); throw; } }
private static extern int GetObject(SafeGdiObjectHandle hgdiobj, int cbBuffer, ref BITMAPINFO lpvObject);
private static extern int GetDIBits(SafeDeviceContextHandle hdc, SafeGdiObjectHandle hbmp, uint uStartScan, uint cScanLines, int[] lpvBits, ref BITMAPINFO lpbmi, uint uUsage);