Jump to content
  • Advertisement
Sign in to follow this  
Fromethius

HDC's

This topic is 4165 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, I have this code:
   Image* newImage = Image::FromFile(szFileName);
   Bitmap image(50, 50);
   Graphics graphics(&image);
   HDC gfxHDC = graphics.GetHDC();
   graphics.DrawImage(newImage, 0, 0);
   int pixel, row;
   for (pixel = 1, row = 1; row <= 50; pixel++)
   if (GetPixel(gfxHDC, pixel, row) != RGB(255, 255, 255))
Anyways, whenever I use GetPixel, the IF statement ALWAYS runs. Even if it is a completely white image. I suspect that either DrawImage isn't drawing it onto gfxHDC or that GetPixel isn't reading the pixels from gfxHDC or I don't even know. The point is, it isn't working and I need some help. Thanks. [Edited by - Promit on July 25, 2007 4:55:29 AM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Fromethius
Hello everyone,

I have this code:


Image* newImage = Image::FromFile(szFileName);
Bitmap image(50, 50);
Graphics graphics(&image);
HDC gfxHDC = graphics.GetHDC();
graphics.DrawImage(newImage, 0, 0);
int pixel, row;
for (pixel = 1, row = 1; row <= 50; pixel++)
if (GetPixel(gfxHDC, pixel, row) != RGB(255, 255, 255))


Anyways, whenever I use GetPixel, the IF statement ALWAYS runs. Even if it is a completely white image. I suspect that either DrawImage isn't drawing it onto gfxHDC or that GetPixel isn't reading the pixels from gfxHDC or I don't even know. The point is, it isn't working and I need some help.

Thanks.
So what does GetPixel() return? Is gfxHDC valid (Non-NULL)? Your for() loop seems screwed as well, it'll never exit, and you don't use the first row (row 0) or first pixel (pixel 0). What language is this? If it's C++, what API?

Share this post


Link to post
Share on other sites
Looks like C++ with GDI+ to me. Does that mean it's managed C++? [EDIT: Apparently not necessarily. Interesting.]

I guessing as I haven't used GDI+, but it is possible that acquiring the HDC for your Graphics object is locking the underlying surface, preventing the following DrawImage() call.

I'd try doing the DrawImage(), then acquiring the device context.

Could be talking a load of rubbish though.

[EDIT] Apparently, and suprisingly, I'm not talking rubbish. The following quote is from MSDN:

Quote:

Each call to the GetHDC method of a Graphics object should be paired with a call to the ReleaseHDC method of that same Graphics object. Do not call any methods of the Graphics object between the calls to GetHDC and ReleaseHDC. If you attempt to call a method of the Graphics object between GetHDC and ReleaseHDC, the method will fail and will return ObjectBusy.


BTW, you are releasing the HDC later in the code, right? [smile]

Oh, and Steve's right about your for loop. If you want to loop across the whole image, you need a nested for loop, like:


for(int Y=0;Y<;50;++Y)
{
for(int X=0;X<;50;++X)
{
if(GetPixel(gfxHDC,X,Y)!= RGB(255, 255, 255)) // etc
}
}


[EDIT - Interesting stuff, this GDI+]

So I reckon you want:


void f()
{
Image* newImage=Image::FromFile(szFileName);
Bitmap image(50,50);

Graphics graphics(&image);
graphics.DrawImage(newImage,0,0);

HDC gfxHDC=graphics.GetHDC();

for(int Y=0;Y<50;++Y)
{
for(int X=0;X<50;++X)
{
if(GetPixel(gfxHDC,X,Y)!= RGB(255, 255, 255))
{
// do whatever when the pixel is not white
}
}
}

graphics.ReleaseHDC(gfxHDC);
}



MSDN does not seem to be forthcoming on whether or not you need to delete the Image that FromFile() returns manually or not (assuming you are not using managed C++).

[Edited by - EasilyConfused on July 25, 2007 6:06:55 AM]

Share this post


Link to post
Share on other sites
as mentioned, the if statement isn't really doing anything, other than evaluating a conditional. Try finishing it up, and look at CDC::GetPixel for the API documentation.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!