Problem with image drawing

Hello!
I try to draw a .BMP image in my code, but something is wrong with it: although the program itself can run, but it doesn't draw the image.
(I wrote comments about what lines what doing - as I think.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC graphicshandler;
    PAINTSTRUCT ps;
    static HBITMAP mypicture;
    BITMAP bitmappic;
    HDC graphicshandler2;
    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
            //Load the image:
            mypicture = (HBITMAP) LoadImageW(NULL, L"blue_ball.bmp",
                        IMAGE_BITMAP, 0, 0, LR_LOADTRANSPARENT);
        break;
        case WM_PAINT:
            graphicshandler = BeginPaint(hwnd, &ps);
            //Create a new graphic handler, based on the previous:
            graphicshandler2 = CreateCompatibleDC(graphicshandler);
            //Load mypicture into this new graphic handler:
            SelectObject(graphicshandler2, mypicture);
            //Read the size of mypicture:
            GetObject(mypicture, sizeof(bitmappic), &bitmappic);
            //Finally draw the image at 50-50 screen coordinates:
            BitBlt(graphicshandler, 50, 50, bitmappic.bmWidth, bitmappic.bmHeight, graphicshandler2, 0, 0, SRCCOPY);

What's wrong in it? And is this the easiest way to draw an image in C++?
In your WM_CREATE you need to check if mypicture is valid.
Hello!
Finally, I have solved this drawnig problem, but the images have a visible bounding box. I would like set the transparency for these bounding boxes.
I found two possible solutions on the internet:

1. I should make a bitmap mask to transparency: http://www.winprog.org/tutorial/transparency.html
But it seems a bit difficult for me. Is it unavoidable? Just because:

2. I've found the AlphaBlend() function together with BLENDFUNCTION.
I tried to build in into my proggram, but Code::Blocks throw the following error message:
F:\progsetup\codeblocks-17.12mingw-nosetup\sajatok\ablakosrajz\Jatekom\main.cpp|45|error: 'AlphaBlend' was not declared in this scope|
I insert this before BitBlt:
BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
AlphaBlend(hdc, 0, 0, myimage.bmWidth, myimage.bmHeight, hdcMem, 0, 0, myimage.bmWidth, myimage.bmHeight, pixelblend);


The second solution seems to be easier, but it is not work because of the error message.
Finally, which is the good one, and how can I achieve it?
It might be a problem with the MinGW compiler. Some functions of the WIN API are not implemented.
Bad news... So, should I download a newest MinGW compiler?
If you want to display a bitmap with transparent parts of the image you use TransparentBlt, not BitBlt. You simply designate a particular color to be the transparency color.
https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-transparentblt

You'd need to link the msimg32.lib library.

should I download a newest MinGW compiler?

I'd recommend having the newest version of your compiler no matter what. Snag Code::Blocks 20.03.
I tried use TransparentBlt() too, but the situation is similar to AlphaBlend(): "not declared in this scope".
I linked msimg32.lib here: Project->Build options->Linker settings. Is this the right place?
Where did you add the libwinmm.a library for linking earlier? Same place for libmsimg32.a

This is another reason why you might want to move from Code::Blocks to Visual Studio 2019. The documentation for Win32 refers to xxx.lib library files, yet for MinGW/GCC you need to link to libxxx.a library files.

MSVC allows a non-standard way to tell the compiler to include a library for linking:
#pragma comment(lib, "winmm.lib")

Include that in the .cpp file that is using Win32 multi-media functions, and MSVC adds it to the project's IDE maintained make file for linking. Only need to add it once. Any other .cpp files using library routines will get linked properly.

MSVC also allows for the non-standard header guard #pragma once . You can still use the #ifndef/#define/#endif instead if you are concerned about cross-platform for your code.

> "not declared in this scope"
This is not a linker error. Did you #include the right header files?
Topic archived. No new replies allowed.