// Track whether Dispose has been called. // The class constructor. public ImageData(string imagePath) { if (string.IsNullOrEmpty(imagePath)) { return; } _imageHandle = Win32.SHLoadImageFile(imagePath); if (_imageHandle != IntPtr.Zero) { var bm = new Win32.BITMAP(); Win32.GetObject(_imageHandle, Marshal.SizeOf(bm), ref bm); _ImageSize.Height = bm.bmHeight; _ImageSize.Width = bm.bmWidth; ViewportSize = ImageSize; } }
private void button7_Click(object sender, EventArgs e) { //from msdn: CreateCompatibleBitmap function //The color format of the bitmap created by the CreateCompatibleBitmap function matches the color format of the device identified by the hdc parameter. //This bitmap can be selected into any memory device context that is compatible with the original device. //Because memory device contexts allow both color and monochrome bitmaps, //the format of the bitmap returned by the CreateCompatibleBitmap function differs when the specified device context is a memory device context. //However, a compatible bitmap that was created for a nonmemory device context always possesses the same color format //and uses the same color palette as the specified device context. //Note: When a memory device context is created, it initially has a 1 - by - 1 monochrome bitmap selected into it. //If this memory device context is used in CreateCompatibleBitmap, the bitmap that is created is a monochrome bitmap. //To create a color bitmap, use the HDC that was used to create the memory device context, as shown in the following code: IntPtr winHwnd = panel2.Handle; IntPtr hdc = Win32.MyWin32.GetDC(winHwnd); IntPtr hbmp = Win32.MyWin32.CreateCompatibleBitmap(hdc, 400, 50); //Bitmap bmp = new Bitmap(panel2.Width, panel2.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); //IntPtr hbmp = bmp.GetHbitmap(); Win32.MyWin32.SelectObject(hdc, hbmp); IntPtr hfont = ff.ToHfont(); Win32.MyWin32.SelectObject(hdc, hfont); Win32.MyWin32.SetTextColor(hdc, 0); Win32.NativeTextWin32.TextOut(hdc, 0, 0, "OKOK\0", 4); Win32.BITMAP win32Bitmap = new Win32.BITMAP(); unsafe { Win32.MyWin32.GetObject(hbmp, System.Runtime.InteropServices.Marshal.SizeOf(typeof(Win32.BITMAP)), &win32Bitmap); } Win32.MyWin32.ReleaseDC(winHwnd, hdc); Win32.MyWin32.DeleteObject(hbmp); //-------------------------------------------- }