'Bitmap' was not declared in this scope

Hello,
anyone know what is the header for Bitmap?
When i try compile

Bitmap myBitmap;

i get the error: 'Bitmap' was not declared in this scope.

Im using Code::Blocks and WinXP sp3.
Last edited on
There is no "Bitmap" class in C++.
Try again.
Thanks for replying..
Maybe im using the code of another language then. This is what i found:
1
2
Bitmap myBitmap;
myBitmap = myBitmap::FromHBITMAP(hBitmap,NULL)

How do i write it in C++?
Last edited on
I doubt it's another language (assuming you meant Bitmap::FromHBITMAP), the Bitmap class is just from some image library.
You'll need to choose an image manipulation library or write your own. How you do it (whatever "it" is) depends on the library.
Last edited on
Thank you very much. I have no experience and your helping is a big step forward, thanks.
Now i will look for an "image library" then.
What im trying to do is get the bitmap from HBITMAP so i can get the rgb values of some pixels and also save the whole image in a .bmp file.
For the rgb values, i would use getPixel(x,y), but for saving to file i still don't have a clue on how to do it without using MFC/ATL.
Can you suggest me any viable way of achieving these 2?
I assume HBITMAP is a handle to a WinAPI bitmap.
There's a simple way to obtain all pixels without an external library, namely GetDIBits:
http://msdn.microsoft.com/en-us/library/dd144879%28VS.85%29.aspx

I believe FreeImagePlus (which is a C++ wrapper for FreeImage) can construct an image object from a bitmap handle as well. FreeImage(Plus) gives you functions that allow you to easily access the pixels, manipulate the image in other ways and save it in many different image formats.

If you want to to it without FreeImage, I'd suggest googling "winapi save bitmap to file".
Yes im using Windows API.
I looked before at GetDIBits and i think im going to give it a try later on.
I will look at FreeImagePlus too.
Also i googled a bit and found http://cimg.sourceforge.net/ and http://openil.sourceforge.net/.
edit: and this also http://opensource.adobe.com/wiki/display/gil/Generic+Image+Library

I hope i can do those 2 simple things without using an external library.
Last edited on
Of course you can. Like I said, use GetDIBits to get the pixel data and the article from the Google search to save it to a bitmap file.
Ok, im trying with GetDIBits but i cant make it work.
It always returns senseless values.
I think im missing the purpose of HDC here. Can someone explain this please? I mean, what's it for???
Note:
1) The image is a screenshot which i take just before running the code by pressing 'PrtScreen' button on the keyboard.
2) GetDIBits scans line 500 which, since im on a resolution of 1280x1024, is approximately in the middle of the screen.
3) Color depth is 32bits.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{

    HBITMAP hBitmap;

    if (OpenClipboard(NULL))
    {
        hBitmap = (HBITMAP)GetClipboardData(CF_DIB);
        if (hBitmap == NULL)
        {
            cout << "Failed to get the clipboard data.";
            return 0;
        }
    }
    else
    {
        cout << "Failed to open the clipboard!";
        return 0;
    }

    HDC hdc=CreateCompatibleDC(NULL);

    BITMAPINFO bmi;
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth = 1280;
    bmi.bmiHeader.biHeight = 1;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = BI_RGB;
    bmi.bmiHeader.biSizeImage = 0;
    bmi.bmiHeader.biXPelsPerMeter = 0;
    bmi.bmiHeader.biYPelsPerMeter = 0;
    bmi.bmiHeader.biClrUsed = 0;
    bmi.bmiHeader.biClrImportant = 0;
    BYTE scanned[4*1280];

    int returnValue = GetDIBits(hdc,hBitmap,500,1,scanned,&bmi,DIB_RGB_COLORS);

    int r = scanned[2401];
    int g = scanned[2402];
    int b = scanned[2403];

    cout << "returnValue = " << returnValue << "\n";
    cout << "r = " << r << "\n";
    cout << "g = " << g << "\n";
    cout << "b = " << b << "\n";

    CloseClipboard();
    return 0;
}

edit: if a moderator thinks it is better to split the thread im fine with it
Last edited on
The first issue is that you're not opening the clipboard previously and that you don't check whether GetClipboardData was successful.
Ok, i just edited the code, added those 2 if{} between line 10 and 25.
Even though, it still returns those senseless values.
Last edited on
You can't close the clipboard before you're done with the bitmap, so you should move CloseClipboard() to the end of the program.
That sounds weird to me: isnt it that the bitmap is copied in hBitmap when GetClipboardData is called?
Anyway, i did move CloseClipboard to the end. And still returns senseless values.
Here is a good quote that i found on the topic:
Bitmap is a class defined in [url=http://msdn.microsoft.com/en-us/library/ms533798(VS.85).aspx]GDI+ Library[/url].
To use it, you have to do the following:
- include <ghiplus.h> header;
- link your project to gdiplus.lib;
- initialize GDI+ library by a call to GdiplusStartup;
- use Gdiplus namespace anytime you are using GDI+ stuff;
- read GDI+ documentation and examples (see the links below).

More info you can find in:
- [url=http://msdn.microsoft.com/en-us/library/ms533815(v=VS.85).aspx]MSDN: GDI+ / Using Images, Bitmaps, and Metafiles[/url]
- [url=http://msdn.microsoft.com/en-us/library/ms534077(v=VS.85).aspx]MSDN: GdiplusStartup Function[/url]
- [url=http://msdn.microsoft.com/en-us/library/ms534420(v=VS.85).aspx]MSDN: Bitmap Class[/url]
- [url=http://www.codeguru.com/cpp/g-m/gdi/gdi/]Codeguru articles: GDI+[/url]

Im going to mark this thread as solved and split it.
[a]http://www.cplusplus.com/forum/beginner/26816/[/a]
Thanks Athar
Last edited on
You don't get a copy of the bitmap when calling GetClipboardData. A handle is something like a pointer. That's why you need to make sure no one else is messing with the clipboard by keeping it open.

As you can read here:
http://msdn.microsoft.com/en-us/library/ff729168%28v=VS.85%29.aspx
CF_DIB doesn't get you a bitmap handle, but a bitmap info structure and the actual bitmap bits.

So all you need is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;

int main()
{
    BITMAPINFO* bitmapInfo;

    if (OpenClipboard(NULL))
    {
        bitmapInfo = (BITMAPINFO*)GetClipboardData(CF_DIB);
        if (bitmapInfo == NULL)
        {
            cout << "Failed to get the clipboard data.";
            return 1;
        }
    }
    else
    {
        cout << "Failed to open the clipboard!";
        return 1;
    }

    BYTE* bitmapBits=(BYTE*)bitmapInfo+sizeof(bitmapInfo->bmiHeader);

    [...]

    CloseClipboard();
}


You can verify that it works by saving the clipboard contents as a .bmp file:
1
2
3
4
5
6
7
8
9
10
    BITMAPFILEHEADER bfhead;
    ZeroMemory(&bfhead,sizeof(bfhead));
    bfhead.bfType=0x4D42;
    bfhead.bfOffBits=sizeof(bitmapInfo->bmiHeader)+sizeof(bfhead);
    bfhead.bfSize=bfhead.bfOffBits+bitmapInfo->bmiHeader.biSizeImage;

    ofstream bfile("test.bmp",ios::binary|ios::trunc);
    bfile.write((char*)&bfhead,sizeof(bfhead));
    bfile.write((char*)&bitmapInfo->bmiHeader,sizeof(bitmapInfo->bmiHeader));
    bfile.write((char*)bitmapInfo+sizeof(bitmapInfo->bmiHeader),bitmapInfo->bmiHeader.biSizeImage);
Last edited on
Thanks a lot Athar. Im able to save the .bmp file now but i still have questions on the rgb values that i get and the way i get them.
Please, let's continue on this thread if you don't mind: http://www.cplusplus.com/forum/beginner/26816/.

On a side not, how do you link addresses on this forum?
Thanks
Last edited on
Topic archived. No new replies allowed.