public void InitializeGDI() { if (_hFirstMemDc == IntPtr.Zero || _hSecondMemDc == IntPtr.Zero) { Graphics g = this.CreateGraphics(); IntPtr hDc = g.GetHdc(); if (_hFirstMemDc == IntPtr.Zero) { _hFirstMemDc = Win32GDISupport.CreateCompatibleDC(hDc); } if (_hSecondMemDc == IntPtr.Zero) { _hSecondMemDc = Win32GDISupport.CreateCompatibleDC(hDc); } g.ReleaseHdc(); } }
private void CreateOffScreenBitmap(ref IntPtr hMemDc) { int bitmapWidth = 0, bitmapHeight = 0; //Caculate the cached bitmap size LinkedListNode <Photo> node = _photos.First; Photo photo; do { photo = node.Value; bitmapWidth = System.Math.Max(bitmapWidth, photo.Width); bitmapHeight += photo.Height + PhotoInterval; } while ((node = node.Next) != null); if (hMemDc == _hFirstMemDc) { _firstBitmapSize.Width = bitmapWidth; _firstBitmapSize.Height = bitmapHeight; } else { _secondBitmapSize.Width = bitmapWidth; _secondBitmapSize.Height = bitmapHeight; } // Create bimap Graphics g = this.CreateGraphics(); IntPtr hDc = g.GetHdc(); IntPtr hBitmap = Win32GDISupport.CreateCompatibleBitmap(hDc, bitmapWidth, bitmapHeight); IntPtr hOldBimap = Win32GDISupport.SelectObject(hMemDc, hBitmap); if (hOldBimap != IntPtr.Zero) { Win32GDISupport.DeleteObject(hOldBimap); } IntPtr hPhotoDc = Win32GDISupport.CreateCompatibleDC(hDc); g.ReleaseHdc(); //g.FillRectangle(_bkgdBrush, 0, 0, bitmap.Width, bitmap.Height); Graphics gMem = Graphics.FromHdc(hMemDc); node = _photos.First; Rectangle rectPhoto = Rectangle.Empty; PointF textLocation = new PointF(100, 100); do { photo = node.Value; rectPhoto.Size = photo.Size; IntPtr hPhoto; if (photo is ManagedPhoto) { hPhoto = photo.GetBitmap().GetHbitmap(); } else if (photo is NativePhoto) { hPhoto = photo.GetHBitmap(); } else { throw new NotSupportedException("Native bitmap is not supported by the photo class"); } Win32GDISupport.SelectObject(hPhotoDc, hPhoto); Win32GDISupport.SetStretchBltMode(hMemDc, Win32GDISupport.StretchBltMode.HALFTONE); Win32GDISupport.StretchBlt(hMemDc, rectPhoto.X, rectPhoto.Y, rectPhoto.Width, rectPhoto.Height, hPhotoDc, 0, 0, photo.UnZoomWidth, photo.UnZoomHeight, Win32GDISupport.TernaryRasterOperations.SRCCOPY); if (photo is ManagedPhoto) { Win32GDISupport.DeleteObject(hPhoto); } //gMem.DrawImage(photo.Bitmap, rectPhoto); gMem.DrawString(photo.Number.ToString(), SystemFonts.DefaultFont, Brushes.White, textLocation); textLocation.Y += rectPhoto.Height + PhotoInterval; rectPhoto.Y += rectPhoto.Height + PhotoInterval; } while ((node = node.Next) != null); Win32GDISupport.DeleteDC(hPhotoDc); gMem.Dispose(); Trace.WriteLine("++++++Bitmap created, photo number " + _photos.First().Number + " to " + _photos.Last().Number); GC.Collect(); }