0

How can I create a device context compatible bitmap and then associating the obtained handle to a BITMAP struct? If I write:

...
HBITMAP hbitmap = CreateCompatibleBitmap(hdc, width, height); // these three arguments are initialized somewhere else
hbitmap = CreateBitmapIndirect(bitmap); // argument already initialized and properly filled
...

A HBITMAP handle compatible with hdc is created, and then a new HBITMAP (filled with bitmap data) is initialized, though without keeping its compatibility. Is there a function which allows not to create a HBITMAP from a BITMAP, but rather fills an initialized HBITMAP with an already existing BITMAP source?

Stencil
  • 1,833
  • 1
  • 17
  • 19
  • 1
    It's not clear what you're asking here. You can't change an existing HBITMAP, all you can do is to create a new one with the desired characteristics and copy the bits to it. You should also consider using device-independent bitmaps instead. – Neil Oct 19 '11 at 20:38
  • I don't want to change an existing HBITMAP, but I'm looking for a function such as 'HBITMAP CreateCompatibleBitmapFromBitmap(HDC hDC, BITMAP *bitmap)' – Stencil Oct 19 '11 at 20:42

2 Answers2

1

CopyImage function

Creates a new image (icon, cursor, or bitmap) and copies the attributes of the specified image to the new one. If necessary, the function stretches the bits to fit the desired size of the new image.

HANDLE WINAPI CopyImage(
  HANDLE hImage,
  UINT uType,
  int cxDesired,
  int cyDesired,
  UINT fuFlags
);

hImage A handle to the image to be copied.

uType The type of image to be copied. This parameter can be one of the following values.

  • IMAGE_BITMAP 0 Copies a bitmap.
  • IMAGE_ICON 1 Copies an icon.
  • IMAGE_CURSOR 2 Copies a cursor.

cxDesired The desired width, in pixels, of the image. If this is zero, then the returned image will have the same width as the original hImage.

cyDesired The desired height, in pixels, of the image. If this is zero, then the returned image will have the same height as the original hImage.

fuFlags

Neil
  • 54,642
  • 8
  • 60
  • 72
  • Thank you, but it still doesn't copy an image into a device compatible HANDLE (or a compatible HBITMAP as well) – Stencil Oct 19 '11 at 21:20
  • @Stencil I obviously still failed to understand the question. If you already have two compatible HANDLEs then, as Roman R. said, simply use BitBlt to copy from one to the other. (If they are not compatible then you can still use StretchBlt.) – Neil Oct 20 '11 at 20:36
0

CreateBitmapIndirect takes BITMAP on its input. And you can get it via GetObject from HBITMAP:

BITMAP Bitmap;
INT nResult = GetObject((HGDIOBJ) hBitmap, sizeof Bitmap, &Bitmap);

CreateBitmapIndirect will be able create a bitmap from this struct. Or you can use CreateCompatibleBitmap to create compatible bitmap providing width/height from obtained Bitmap.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • Thank you for your answer, but I have to fill a HBITMAP with a BITMAP provided that HBITMAP has been already initialized. CreateCompatibleBitmap does not create a HBITMAP from an existing BITMAP. – Stencil Oct 19 '11 at 20:38
  • So you create this new `HBITMAP` and then `BitBlt` the data from original bitmap and you end up with a full copy. Is this going to be fine? You cannot create a new bitmap object which references exactly the same underlying data, but making a copy of picture with `BitBlt` is just easy. – Roman R. Oct 19 '11 at 20:41