Using GDI+ in C - gdiPlusStartup function returning 2 - c

I am attempting to use GDI+ in my C application to take a screenshot and save it as JPEG. I am using GDI+ to convert the BMP to JPEG but apparently when calling the GdiplusStartup function, the return code is 2(invalid parameter) instead of 0:
int main()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
//if(GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL) != 0)
// printf("GDI NOT WORKING\n");
printf("%d",GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL));
HDC hdc = GetDC(NULL); // get the desktop device context
HDC hDest = CreateCompatibleDC(hdc); // create a device context to use yourself
// get the height and width of the screen
int height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
int width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
// create a bitmap
HBITMAP hbDesktop = CreateCompatibleBitmap( hdc, width, height);
// use the previously created device context with the bitmap
SelectObject(hDest, hbDesktop);
// copy from the desktop device context to the bitmap device context
// call this once per 'frame'
BitBlt(hDest, 0,0, width, height, hdc, 0, 0, SRCCOPY);
// after the recording is done, release the desktop context you got..
ReleaseDC(NULL, hdc);
// ..and delete the context you created
DeleteDC(hDest);
SaveJpeg(hbDesktop,"a.jpeg",100);
GdiplusShutdown(gdiplusToken);
return 0;
}
I am trying to figure out why the GdiplusStartup function is not working.
Any thoughts?

Initialize gdiplusStartupInput variable with the following values: GdiplusVersion = 1, DebugEventCallback = NULL, SuppressBackgroundThread = FALSE, SuppressExternalCodecs = FALSE
According to MSDN article GdiplusStartup function http://msdn.microsoft.com/en-us/library/windows/desktop/ms534077%28v=vs.85%29.aspx
GdiplusStartupInput structure has default constructor which initializes the structure with these values. Since you call the function from C, constructor is not working and structure remains uninitialized. Provide your own initialization code to solve the problem.

// As Global
ULONG_PTR gdiplusToken;
// In top of main
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&programInfo.gdiplusToken, &gdiplusStartupInput, NULL);
works for me.

Related

Screenshot code gives unexpected error on call to GetDIBits

I'm using some C code to take a screenshot of a window, translated directly from a PowerBuilder sample that works perfectly. Here's the part up to where there's a problem:
extern "C" __declspec(dllexport) BOOL __stdcall WindowScreenShot(const wchar_t* fileName, unsigned long x, unsigned long y,
unsigned long width, unsigned long height)
{
HWND ll_hWnd;
HDC ll_hdc, ll_hdcMem;
HBITMAP ll_hBitmap;
HANDLE hDib = NULL, hFile = NULL;
char* lpBitmap = NULL;
BOOL lb_result, lb_ok = FALSE;
BITMAPINFO lstr_Info;
BITMAPFILEHEADER lstr_Header;
int li_pixels;
DWORD dwBmpSize, dwBytesWritten;
// get handle to windows background
ll_hWnd = GetDesktopWindow();
// Get the device context of window and allocate memory
ll_hdc = GetDC(ll_hWnd);
ll_hdcMem = CreateCompatibleDC(ll_hdc);
ll_hBitmap = CreateCompatibleBitmap(ll_hdc, width, height);
if (ll_hBitmap != 0)
{
// Select an object into the specified device context
SelectObject(ll_hdcMem, ll_hBitmap);
// Copy the bitmap from the source to the destination
lb_result = BitBlt(ll_hdcMem, 0, 0, width, height, ll_hdc, x, y, SRCCOPY);
lstr_Info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
// Get the bitmapinfo (THIS LINE IS FAILING CURRENTLY)
if (GetDIBits(ll_hdcMem, ll_hBitmap, 0, height, NULL, &lstr_Info, DIB_RGB_COLORS) > 0)
{ ...
That last call to GetDIBits always fails with the only documented error that it can give, ERROR_INVALID_PARAMETER. Since this code is basically absolutely identical to the PowerBuilder code, in terms of the structures used and Windows APIs called, I just have no idea how to resolve it. I've also read the API docs carefully and it all looks like it should work.
Any bright ideas? Thanks.
Paul Ogilvie's answer resolved it - I was using uninitialized structures because they were automatic variables. As soon as I fixed that the code worked.

how can I redraw only the rectangle in my app

I'm using Visual Studios Win32 to develop a control app for an ARINC 629 bus. It works fine for an event-driven action such as a mouse click, but I need it to update in real-time when I'm receiving data over the ARINC 629 bus.
I've learned that Win32 is not the best environment for graphics that need to be continuously updated, but my app is working fairly well except for some flicker.
It seems most of the suggestions on how to reduce flicker don't address when a function is called continuously.
Double-buffering does not seem to help much.
In this case, if I could just redraw the rectangle and not the entire client area, I could reduce or eliminate the flicker.
Here is a code snippet:
void twirling_baton_left_channel(HDC hdc, HWND hWnd, unsigned int left_chan_data_receive)
{
LPRECT lpRect;
RECT rect_chan_one;
lpRect = &rect_chan_one;
lpRect->left = 0;
lpRect->top = 0;
lpRect->right = 0x01ba;
lpRect->bottom = 0x01c6;
static unsigned int wait_to_redraw_left_chan = 0;
status_baton_one.left = 0xb2;
status_baton_one.top = 0x28;
status_baton_one.right = 0x11c;
status_baton_one.bottom = 0x4b;
/* draw the running indicator */
if(wait_to_redraw_left_chan > 5)
{
RedrawWindow(hWnd, lpRect, NULL, RDW_INVALIDATE); //RedrawWindow((HWND)hdc, NULL, NULL, RDW_ERASE);
wait_to_redraw_left_chan = 0;
}
wait_to_redraw_left_chan++;
//SelectObject(ps.hdc, GetStockObject(DC_BRUSH));
if(left_chan_data_txrx)
{
SetDCBrushColor(hdc, RGB(0, 255, 0)); // green
Rectangle(hdc, status_baton_one.left, status_baton_one.top, status_baton_one.left+30, status_baton_one.top+20);
}
else
{
SetDCBrushColor(hdc, RGB(128,128,128)); //grey
Rectangle(hdc, status_baton_one.left, status_baton_one.top, status_baton_one.left+30, status_baton_one.top+20);
}
}
I'm trying to use the RedrawWindow() command to only redraw the box defined by the 'Rectangle' function, but it causes the entire client area to be redrawn, which causes a flicker since it is being called continuously.

Void to HBITMAP conversion Issue

I keep getting an error at line 123:
123 C:\Dev-Cpp\Window_main_2.c invalid conversion from `void*' to `HBITMAP__*'
I don't know what to make of this and its driving me crazy.
void DrawBitmap(HDC hdcDest, char *filename, int x, int y)
{
HBITMAP image;
BITMAP bm;
HDC hdcMem;
// This is the line that brings about the issue (just ask me if more code is required because
// there is a lot more. Essentially this whole function points to a file and I call this
// function in another function that will compile a windows screen filled with the following
// image path. But I cant get this HBITMAP to agree with the image datatype. Please let me
// know if more info is required and thank you.) The line is below.
image = LoadImage(0, "C:\\Users\\Lillian\\Pictures\\c.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
GetObject(image, sizeof(BITMAP), &bm);
hdcMem = CreateCompatibleDC(global_hdc);
SelectObject(hdcMem, image);
BitBlt(
global_hdc,
x,
y,
bm.bmWidth,
bm.bmHeight,
hdcMem,
0,
0,
SRCCOPY);
DeleteDC(hdcMem);
DeleteObject((HBITMAP)image);
}
LoadImage() returns a HANDLE. Your need a cast when assigning the result to your variable:
image = (HBITMAP) LoadImage(0, "C:\\Users\\Lillian\\Pictures\\c.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
Also, before you DeleteDC(), you need to select the original HBITMAP back into hdcMem - you need to save it when you call SelectObject() earlier.

Get pixel data as array from hdc

I followed this code here to take a screenshot. It puts stuff onto an HDC. I was wondering how to get stuff off from this HDC as an array of pixel data. I want to copy it to clipboard and also draw it to a HTML5 canvas.
Do I have to run GetPixel for every point on the HDC, is this the only way to get an array of the bitmap?
HBITMAP MakePrintScreen()
{
HWND hWindow = GetDesktopWindow();
HDC hdcScreen = GetDC(hWindow);
RECT rect;
HBITMAP hbmC;
GetClientRect(hWindow,&rect);
if((hbmC = CreateCompatibleBitmap(hdcScreen,rect.right,rect.bottom)) != NULL)
{
HDC hdcC;
if((hdcC = CreateCompatibleDC(hdcScreen)) != NULL)
{
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcC,hbmC);
BitBlt(hdcC,0,0,rect.right,rect.bottom,hdcScreen,0,0,SRCCOPY);
SelectObject(hdcC,hbmOld);
DeleteDC(hdcC);
}
}
ReleaseDC(hWindow,hdcScreen);
return hbmC;
}
After you have deselected the bitmap from the device context you call GetDIBits to retrieve the bits from the bitmap.

WinAPI get mouse cursor icon

I want to get the cursor icon in Windows.
I think language I use isn't very important here, so I will just write pseudo code with WinAPI functions I'm trying to use:
c = CURSORINFO.new(20, 1, 1, POINT.new(1,1));
GetCursorInfo(c); #provides correctly filled structure with hCursor
DrawIcon(GetWindowDC(GetForegroundWindow()), 1, 1, c.hCursor);
So this part works fine, it draws current cursor on active window.
But that's not what I want. I want to get an array of pixels, so I should draw it in memory.
I'm trying to do it like this:
hdc = CreateCompatibleDC(GetDC(0)); #returns non-zero int
canvas = CreateCompatibleBitmap(hdc, 256, 256); #returns non-zero int too
c = CURSORINFO.new(20, 1, 1, POINT.new(1,1));
GetCursorInfo(c);
DrawIcon(hdc, 1, 1, c.hCursor); #returns 1
GetPixel(hdc, 1, 1); #returns -1
Why doesn't GetPixel() return COLORREF? What am I missing?
I'm not very experienced with WinAPI, so I'm probably doing some stupid mistake.
You have to select the bitmap you create into the device context. If not, the GetPixel function will return CLR_INVALID (0xFFFFFFFF):
A bitmap must be selected within the device context, otherwise, CLR_INVALID is returned on all pixels.
Also, the pseudo-code you've shown is leaking objects badly. Whenever you call GetDC, you must call ReleaseDC when you're finished using it. And whenever you create a GDI object, you must destroy it when you're finished using it.
Finally, you appear to be assuming that the coordinates for the point of origin—that is, the upper left point—are (1, 1). They are actually (0, 0).
Here's the code I would write (error checking omitted for brevity):
// Get your device contexts.
HDC hdcScreen = GetDC(NULL);
HDC hdcMem = CreateCompatibleDC(hdcScreen);
// Create the bitmap to use as a canvas.
HBITMAP hbmCanvas = CreateCompatibleBitmap(hdcScreen, 256, 256);
// Select the bitmap into the device context.
HGDIOBJ hbmOld = SelectObject(hdcMem, hbmCanvas);
// Get information about the global cursor.
CURSORINFO ci;
ci.cbSize = sizeof(ci);
GetCursorInfo(&ci);
// Draw the cursor into the canvas.
DrawIcon(hdcMem, 0, 0, ci.hCursor);
// Get the color of the pixel you're interested in.
COLORREF clr = GetPixel(hdcMem, 0, 0);
// Clean up after yourself.
SelectObject(hdcMem, hbmOld);
DeleteObject(hbmCanvas);
DeleteDC(hdcMem);
ReleaseDC(hdcScreen);
But one final caveat—the DrawIcon function will probably not work as you expect. It is limited to drawing an icon or cursor at the default size. On most systems, that will be 32x32. From the documentation:
DrawIcon draws the icon or cursor using the width and height specified by the system metric values for icons; for more information, see GetSystemMetrics.
Instead, you probably want to use the DrawIconEx function. The following code will draw the cursor at the actual size of the resource:
DrawIconEx(hdcMem, 0, 0, ci.hCursor, 0, 0, 0, NULL, DI_NORMAL);

Resources