Old School GDI RRS feed

  • Question

  • User-732283392 posted
    Alright, I need to create a bitmap to be drawn on by a COM class i don't have control over. All i want to do is create a GDI (not GDI+) bitmap (DIB?) and pass the pointer of it to the COM class so it can draw on it (as it doesn't work with GDI+ bitmap for some reason). Can anyone give me some code to create the bitmap and call the FillRect() method on it? Here is some code i have that isn't working properly: // note CreateCompatibleDC, GetDC, CreateCompatibleBitmap, SelectObject, etc. are all DLL imports from the gdi32.dll (old GDI) // hWnd is the handle of the form hDC = GetDC(hWnd); IntPtr hMemDC = CreateCompatibleDC(hDC); IntPtr hBitmap = CreateCompatibleBitmap(hDC, size.Width, size.Height); IntPtr hOldBitmap = SelectObject(hMemDC, hBitmap); // i defined the struct locally, it only has top left right and bottom properties. the FillRect function needs a RECT RECT r = new RECT(); r.Top = 0; r.Left = 0; r.Bottom = size.Height; r.Right = size.Width; // i defined the RGB struct locally, it has red, gree, and blue properties. the FillRect needs this as well RGB col = new RGB(); col.red = 70; col.green = 130; col.blue = 180; // create the solid brush IntPtr hBrush = CreateSolidBrush(col); // select the object (i still don't fully understand this methods purpose, i'm borrowing here from some other code if ound) hMemDC is an in memory device context i created IntPtr hOldBrush = SelectObject(hMemDC, hBrush); // call the fillrect to set the color FillRect(hMemDC, ref r, hBrush);//paint a white background. // create the bitmap using the handle of the device context Bitmap b = Bitmap.FromHbitmap(hBitmap); // save it out b.Save("C:\\temp\\pleasework2.jpg", ImageFormat.Jpeg); It does save, and doesn't throw an error, but it's ALWAYS just a solid black! Any help is greatly appreciated on this.
    Thursday, January 6, 2005 2:36 PM

All replies

  • User-732283392 posted
    Nevermind, figured it out. SelectObject takes an object from a different context, and moves it in to the new context, which is necessary when creating brushes, pens, etc. and using them in a bitmap. This must be done before any paint, draw, or fill method called on a bitmap.
    Saturday, January 8, 2005 5:05 PM