Which Api to use to create bitmap??

I see 3 Api of windows to create bitmap
but i don't know which i must to use to
create Bitmap.
the bitmap i want to create will be parameter
of the GetDIBits Api and this bitmap is
related to a window on my screen that i
want to copy inbuffer.
I use CreateCompatibleBitmap, I use
CreateBitmapIndirect but i don't have the
expected result
nadiamehrezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
Normally, you should use CreateCompatibleBitmap or CreateDIBSection.
0
nietodCommented:
It hard to tell you hat to do without your providing details about what you want to accomplish.   but you say the bitmap is "related" to the screen.   I think you must mean compatible?  In that case, You probably want to use CreateCompatibleBitmap().

If you are having problems with that, it would help if you post your code or describe your error.  Otherwise, we can't really help you.
0
nadiamehrezAuthor Commented:
I SEND YOU A PART OF CODE TO EXPLAIN YOU EXACTLY MY PROBLEM.
I COPY A rectangle FROM SCREEN TO BUFFER BY USING GetDIBits and after this I retrieve this rectangle from buffer to screen by using SetDIBitsToDevice but the color is not
the same than origin, I copy a grey rectangle to buffer but i retrieve a black rectangle from a buffer, why this change of color.
I precise you that i use the same
BITMAPINFO for the two api: GetDIBits and SetDIBitsToDevice, is this correct.

This is my portion of code, the call of
GetDIBits and SetDIBitsToDevice is in
win_dib_repaint function.

/* Repaint a section of the window. */
private void
win_dib_repaint(gx_device_win * dev, HDC hdc, int dx, int dy, int wx, int wy,
            int sx, int sy)
{
BITMAP bmp;
HBITMAP hdmpdc;
char  buffer[200];

struct bmi_s {
      BITMAPINFOHEADER h;
      ushort pal_index[256];
    } bmi;
 int i;
UINT which_colors;

bmi.h.biSize = sizeof(bmi.h);
bmi.h.biWidth = wdev->mdev.width;
bmi.h.biHeight = wy;
bmi.h.biPlanes = 1;
bmi.h.biBitCount=dev->color_info.depth;
bmi.h.biCompression = 0;
bmi.h.biSizeImage = 0;      /* default */
bmi.h.biXPelsPerMeter = 0;/* default */
bmi.h.biYPelsPerMeter = 0;/* default */

if (dev->BitsPerPixel <= 8) {
bmi.h.biClrUsed = wdev->nColors;
bmi.h.biClrImportant = wdev->nColors;
for (i = 0; i < wdev->nColors; i++)
bmi.pal_index[i] = i;
which_colors = DIB_PAL_COLORS;
} else {
 bmi.h.biClrUsed = 0;
 bmi.h.biClrImportant = 0;
 which_colors = DIB_RGB_COLORS;
}

hdmpdc = CreateCompatibleBitmap( hdc,wx,wy);


GetDIBits(hdc, hdmpdc, 0, wy,
wdev->mdev.line_ptrs[wdev->height - (sy + wy)], (BITMAPINFO FAR *) & bmi, which_colors);

 SetDIBitsToDevice(hdc, dx, dy, wx, wy,
                  sx, 0, 0, wy,
                 
wdev->mdev.line_ptrs[wdev->height - (sy + wy)],BITMAPINFO FAR *) & bmi, which_colors);
}
 
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

nietodCommented:
I'm not sure what you are trying to do, but I'm sure you are making some mistakes.

First of all, since the two bitmaps are compatible, why are you using GetDibBits and SetDIBits()?   You should use BitBlt() to copy from the device (screen) to the memory bitmap.  This will be much easier because you don't need to set up a BITMAINFOHEADER. (where hou have problems.)

Now how are you getting the DC in hdc?  
0
nietodCommented:
You don't have to reject an answer, that is not complete.  You can just post a comment asking for clarification.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chensuCommented:
There is nothing done in between CreateCompatibleBitmap and GetDIBits. hdmpdc is an empty bitmap.
0
nietodCommented:
That's okay, the GetDIBits gets bits from some sort of source DC and then those bits are copied to the bitmap created with CreateCompatibleBitmap.  However, I don't think that copy is goignto work well.  I doubt that bitmap header is initialized okay.
0
chensuCommented:
No, the GetDIBits retrieves the bits of hdmpdc and copies them into the buffer.
0
chensuCommented:
You have solved the problem?
0
nadiamehrezAuthor Commented:
Comment to chensu...........
Why do you want to do something between CreateCompatibleBitmap and GetDIBits. hdmpdc is not an empty bitmap because
this function takes 3 arguments which have values, and what do you want me to do.

Comment to nietod.......
That's okay, the GetDIBits gets bits from some sort of source DC and then those bits are copied to the bitmap created with CreateCompatibleBitmap.  However, I don't think that copy is goignto work well.  I doubt that bitmap header is initialized okay.
yes, you are right but how can
I get bitmap header from handle to bitmap because CreateCompatibleBitmap return handle to bitmap and i need
to use BITMAPINFOHEADER in
GetDIBits and i don't know how to retrieve this value from handle to bitmap.  
0
chensuCommented:
OK, it is not empty. It is a wx x wy bitmap. But you never draw anything into it, nor do you copy (BitBlt) anything into it. What do you want GetDIBits to get?
0
nietodCommented:
chensu, right.  I was thinking GetDitBits() got the bits from the DC.

>> hdmpdc is not an empty bitmap
The newly created bitmap will "contain" random pixel data.  i.e. it doesn't store any particular image.  That is what chensu means by "empty".  

>> yes, you are right
Actually, I was wrong.

What is it that you are trying to accomplish in the code.  what is, what is the end goal, not how are your trying to get there?
0
nadiamehrezAuthor Commented:
You say me that the newly created bitmap will "contain" random pixel data.  i.e. it doesn't store any particular image, yes you are right, so i decide to invoque an Api
selectobject()after a bitmap is created

HBITMAP hdmpdc, Newhdmpdc;

hdmpdc = CreateCompatibleBitmap( hdc,wx,wy);
 
Newhdmpdc = selectobject(hdc, hdmpdc);

GetDIBits(hdc, Newhdmpdc, 0, wy,
wdev->mdev.line_ptrs[wdev->height - (sy + wy)], (BITMAPINFO FAR *) & bmi, which_colors);
The error that I have is that selectobject() return Null,It means that it fails, WHY i don't understand???????????????????????

My second question, can I invoque
CreateDIBSection() and after this
CreateCompatibleBitmap()to resolve my problem
 
0
nietodCommented:
>> selectobject()after a bitmap is created
That might be a good idea.  That might be a terrible idea.   What DC are you selecting it into?  What do you do after that to initialize the bitmap?

>> error that I have is that
>> selectobject() return Null
Like I said, that might be a terrible idea.  I think it was.  What dC are you selecting it into?  You can only select a bitmap into a memory DC, not a device DC.  So what DC are you selecting it into?  How do you get that DC?


But once again, it comes down to the question that I've asked several times before.  What do you want to do?  We need to know what you are trying to do!  The code has obvious errors but we can't begin to fix it because we don't know what you want to do.
0
nadiamehrezAuthor Commented:
I work in a very big project.
I work on DLL which have a lot of function. I have an OCX on my screen which has been loaded before to call
the fuction
win_dib_repaint(gx_device_win * dev, HDC hdc, int dx, int dy, int wx, int wy, int sx, int sy)

This function repaint section of window
and this section is my OCX which have
white background.
When I call the API SetDIBitsToDevice()
in the fuction win_dib_repaint()
(This is the original code), this API
load multimedia document in my OCX
(I realise this with success) but what i want to do is to change the background of ocx after loading
multimedia document and i want to have
the same background on OCX and on screen(the background on screen is cloud),
this is why i think to API GetDIBits()
GetDIBits(hdc, Newhdmpdc, 0, wy,
wdev->mdev.line_ptrs[wdev->height - (sy + wy)], (BITMAPINFO FAR *) & bmi, which_colors); Because this API retrieves the bits of the specified bitmap and copies them into a buffer using the specified format.
But this API GetDIBits() needs a handle to BITMAP, this is why i think to create
a BITMAP by using API CreateCompatibleBitmap() but the result
that ocx become black so i can't see my multimedia document and i don't have cloudy background that i want.
I hope that you understand what I want to do and you can help me because I can't find a solution and I work on it
two weeks ago.
 
0
nietodCommented:
I don't really understand.

Let me take a guess.

Are you trying to copy part of the screen to a bitmap?  
0
nadiamehrezAuthor Commented:
yes I try to copy part of the screen to a bitmap
0
nadiamehrezAuthor Commented:
I have difficulty to use the two API
CreateDIBSection or CreateBitmapIndirect
because they need as arguments
pointer to the location of the bit values for the bitmap
But I can't allocate memory for bitmap in memory because I don't know the size of bitmap that I create
But I have an adress which points to
the location in memory with the same size that my BITMAP, my question
HOW CAN I DETERMINE THE SIZE OF LOCATION IN MEMORY IF I KNOW JUST ADDRESS OF THIS LOCATION.

0
nietodCommented:
The following code copies the ENTIRE screen to a bitmap.   It should be easy enough to adjust so it copies only a specified rectangular part of the screen

HDC ScreenDC = CreateDC("DISPLAY",NULL,NULL,NULL);
HDC MemDC = CreateCompatibleDC(ScreenDC);
HBITMAP BitMap = CreateCompatibleBitmap(ScreenDC,GetSystemMetrics(SM_CXSCREEN),GetsystemMetrics(SM_CYSCREEN))
HBITMAP OldBmp = SelectObject(MemDC,BitMap);

BitBlt(MemDC,0,0,,GetSystemMetrics(SM_CXSCREEN),GetsystemMetrics(SM_CYSCREEN),ScreenDC,0,0,SRCCOPY);

SelectObject(MemDC,OldBmp);
DeleteDC(ScreenDC);
DeleteDC(MemDC);

The code creates a DC for the screen and a compatible one for a memory bitmap.  Then it creates a memory bitmap the same size as the screen.  It selects the memory bitmap into the memory DC.  Then it copies the screen's bitmap to the memory bitmap with BitBlt().   Then it cleans up by restoring the memory DC's original bitmap (important step!).  and deletes the two DCs.  When it is done Bitmap is a new memory based bitmap that stores a copy of the screen image.

read it over and ask if you have any questions.
0
nadiamehrezAuthor Commented:
I try to adapt what you show me to my situation but my problem is that my program can't read the value returned by
CreateCompatibleDC so when i execute my program, it crashes and message error
sent to me is THE MEMORY COULD NOT BE READ.
this problem happens only when my program calls sprintf(), it is necessary for me to call sprintf() because i want to know what  
CreateCompatibleDC() return

I precise you that when my program calls CreateCompatibleDC() but not sprintf(), it doesn't crashes.
I add another precision to you:
I already have hdc in my program,this is
why i don't call CreateDC().

This is a portion of code which crashes
the execution of my program:

HDC hmemdc=CreateCompatibleDC(hdc);

sprintf(buffer,"hmemdc = %s", CreateCompatibleDC(hdc));

MessageBox(0,buffer,"win_dib_repaint",0);
0
nietodCommented:
>> can't read the value
>> returned by
>> CreateCompatibleDC
Can't read it?  Its handle.  You can't read from a handle.  

>> only when my program calls sprintf()
Hopw are you printing the handle with sprintf()?   You can convert the handle to an int and print it as a numeric value.  You cannot treat the handle like a pointer.  

>> I already have hdc in my program,this is
>> why i don't call CreateDC().
You have ONE DC.  For the most part, you can't do this with one.  you need two.  And they have to be the right types of DC.  On numerious occassions I have asked you how you got that one DC.  You've never said.

>> sprintf(buffer,"hmemdc = %s", CreateCompatibleDC(hdc))
Of course.  %s is used to print a string.  CreateCompatibleDC doesn't return a string.

Why don't you try using a debugger instead of printf().?
0
nadiamehrezAuthor Commented:
>>Why don't you try using a debugger instead of printf().?

I can't use Debugger in my project, this is an another problem, i have to use sprintf and messagebox along my project

>>On numerious occassions I have asked you how you got that one DC.  You've never said.

In another part of the project, it is
calculate, so my procedure receive it as
argument, this is why i don't worry bout
How they proceed to get it.

>>You have ONE DC.  For the most part, you can't do this with one.  you need two.

I don't understand what this mean.

>>HBITMAP OldBmp = SelectObject(MemDC,BitMap);
 
Why do you select the memory bitmap into the memory DC.

>>SelectObject(MemDC,OldBmp);
Why restoring the memory DC's original bitmap .


>>DeleteDC(ScreenDC);
What's the goal of this delete.

>>DeleteDC(MemDC);
When you do this, you don't have anymore
a copy of the screen image in memory.  


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.