Thread: colorref and setpixel

  1. #16
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Code:
    system("pause");
    I'd suggest not using the system function as much as possible (just words of advice).

    Only thing I can think of that might be happening is for some reason array1 isn't being given all the memory you're asking for? You're asking for a 3.8 MB array, so possibly it's not getting a valid pointer (for some reason).

  2. #17
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    First, array1 should be an array of COLORREF, not INT. Second, why are you getting the cursor position and then getting the pixel at that position? Third, chPixColor is never used.

    Other than that, as things stand I see no reason for the program to quit immediately.

    >>possibly it's not getting a valid pointer
    Possibly, but I just don't see that happening. Arrays are different from dynamic arrays, and aren't really pointers even though they are often implicitly converted to pointers. Besides which, they're allocated on the stack, which is allocated on program startup - and if stack allocation fails, I don't think the program will run at all. Although perhaps the array declaration is optimized out when there's no reference to it and the stack size is decreased? Anyhow, that's just speculation and I have no idea if the stack size actually CAN be decreased by the compiler. Or perhaps the compiler is too stupid to realize that the array will overflow the stack, but smart enough to get rid of it when nothing's referencing it, in which case that might be the problem.

    And if that's the problem, try making the array smaller... much smaller.
    Last edited by Hunter2; 09-11-2004 at 05:30 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #18
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    Code:
    	COLORREF array1[1000][1000];
    ...
    for (INT col = 400; col < 500; col += 10)
    	{
    		for (INT row = 400; row < 500; row += 10)
    		{
    			//GetCursorPos(&pt);
    			//clr=GetPixel(hdcScrn,pt.x,pt.y);
    			COLORREF pixColor = GetPixel(hdcScrn,col,row);
    
    			array1[row][col] = pixColor;
    			Sleep(10);
    		}
    	}
    i'm not sure why i left that bit of code in there i must have just not have realized it was there.

    well anyway i left the pause in there b/c i know that is not the problem (but i'll find another way to do the job)

    i also made the array 50 X 50 instead. the outcome was it would run and after a few seconds it would close and the windows dialog box would appear saying the program has encountered an error would i like to send an error report to microsoft.

    oh yea. problem is still there

    Edit: could the problem be that the array starts putting in values at array1[400][400] ?
    Last edited by bballzone; 09-11-2004 at 05:42 PM.

  4. #19
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    i did some debugging and made a separate program

    Code:
    int main()
    {
    	HDC      hdcScrn;
    	COLORREF array1[100][100];
    
    	Sleep(2000);
    	hdcScrn=CreateDC("DISPLAY",0,0,0);
    
    	for (INT col = 1; col < 50; col += 1)
    	{
    		for (INT row = 1; row < 50; row += 1)
    		{
    			COLORREF pixColor = GetPixel(hdcScrn,col,row);
    
    			array1[row][col] = pixColor;
    			cout << row << " ";
    			cout << col << " ";
    			cout << array1[row][col] << endl;
    			Sleep(10);
    		}
    	}
    	DeleteDC(hdcScrn);
    
    	system("pause");
    return 0;
    }
    it works. if you are going to run it on your computer i'd change the loop to 20 instead of 50 b/c it takes a while.

    when i change the loop values to my original program and declare the array to be bigger it exits just like before.
    Last edited by bballzone; 09-11-2004 at 05:53 PM.

  5. #20
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>could the problem be that the array starts putting in values at array1[400][400] ?
    If you changed the array size to 50X50 and then put values at array1[400][400], yes that's a problem. Because the max position is array1[49][49] then.

    >>when i change the loop values to my original program and declare the array to be bigger it exits just like before.
    Then it would seem that there may be a shred of accuracy in my speculation about the stack size and compiler intelligence. Try allocating your array on the heap (i.e. dynamically allocate it, or use a std::vector<std::vector<COLORREF>>) instead. That way you'll be fairly sure that your stack isn't being overflowed.

    I hope I'm right about it, if I am than this is my first correct guess in a looooong time!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed