Thread: GetPixel and CreateDC help. (winapi)

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    12

    GetPixel and CreateDC help. (winapi)

    I've been creating a little program to read in a file (Using CreateDC to create the device handle) and then send it to GetPixel to be read into a RGB value.

    There are two problems so far. I have created an if statement to check if CreateDC succeeded (Checks if it is NULL), but it's always failing. The code is:
    Code:
    if((hdc=CreateDC(Image.png,NULL,NULL,NULL))==NULL) {
           cout<<"ERROR: Failed to load image";
    }
    The image IS in the same directory as the program, so that isn't the problem.

    I also don't understand exactly how GetPixel works. I know it returns f a COLORREF value, which I have passed to GetRValue, GetGValue, and GetBValue and put them in to separate variables. The problem is, I don't know how to combine those values to make a normal color code to perhaps compare against an enum.

    MSDN has been little help, as they are not very descriptive and there are no examples I can find. I've been Googling non-stop for 2 days with no avail. If anyone could shine some light on the situation, I'd be eternally greatful.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    None of the documentation I can find says that you can give a filename of a bitmap image-file to CreateDC - I can't say if it's wrong or not, but as far as I can see, CreateDC is supposed to be given the name of a DEVICE, not a file.

    --
    Mats

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Should "Image.png" not be in quotes?

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    Aww. It was supposed to be in quotes. I have corrected it, but it still doesn't seem to work.

    Regardless, if creating a device handler isn't meant for an image, then I don't really know how to reference a picture as a device for GetPixel to see.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you're OK with using MFC calls, then you can use the CImage class, which supports GetPixel and others, and it is quite easy to use.
    Code:
    ...
    CImage myImage;
    
    ...
    myImage.Load("myfile.jpg");
    ...
    COLORREF px = myImage.GetPixel(x, y);
    
    ...
    But if you don't have a MFC library to use, then I don't know.

    The CImage contains a DC, so you probably need to create a DC (I think CreateCompatibleDC is what you actually want), and then set the bitmap within the DC to use it for GetPixel - of course, you'd also have to decode the PNG format.

    I'm fairly sure that wxWidgets or some such library has functions to do this sort of work.

    --
    Mats

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    You code has no sense at all.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Alex31 View Post
    You code has no sense at all.
    If you are referring to my "code", it's not a full set of code, just some snippets that does the small pieces the original poster was asking for - or is there something else of my code that you mean don't make sense?

    --
    Mats

Popular pages Recent additions subscribe to a feed