Click here to Skip to main content
14,265,979 members
Rate this:
Please Sign up or sign in to vote.
See more:
void LoadImgFromGDIP(){
    GdiplusStartup GDIPStart = (GdiplusStartup)GetProcAddress(LoadLibrary(TEXT("gdiplus.dll")), "GdiplusStartup");
    GdipCreateFromHDC GDIPCreate = (GdipCreateFromHDC)GetProcAddress(LoadLibrary(TEXT("gdiplus.dll")), "GdipCreateFromHDC");
    GdipLoadImageFromFile GDIPLoadImgFile = (GdipLoadImageFromFile)GetProcAddress(LoadLibrary(TEXT("gdiplus.dll")), "GdipLoadImageFromFile");
    GdipDrawImageRect GDIPDrawImgRect = (GdipDrawImageRect)GetProcAddress(LoadLibrary(TEXT("gdiplus.dll")), "GdipDrawImageRect");
    GdipGetImageHeight GDIPGetImgHeight = (GdipGetImageHeight)GetProcAddress(LoadLibrary(TEXT("gdiplus.dll")), "GdipGetImageHeight");
    GdipGetImageWidth GDIPGetImgWidth = (GdipGetImageWidth)GetProcAddress(LoadLibrary(TEXT("gdiplus.dll")), "GdipGetImageWidth");
    GdipDrawImage GDIPDrawImg = (GdipDrawImage)GetProcAddress(LoadLibrary(TEXT("gdiplus.dll")), "GdipDrawImage");

    PAINTSTRUCT ps;
    HDC hdc;
    HDC mDC;
    int lngHeight, lngWidth;
    int *img;
    int *graphics;

    GdiplusStartupInput Input;
    Input.DebugEventCallback = NULL;
    Input.GdiplusVersion = 1;
    Input.SuppressBackgroundThread = 0;
    Input.SuppressExternalCodecs = 0;
    ULONG_PTR ulToken;
    int iRet=0;
    iRet = GDIPStart(&ulToken, &Input, NULL);
    if (iRet != 0){
        MB(TEXT("ERROR,StartUp"),1);//MessageBox
    }
    hdc = BeginPaint(hPIC_LOGO, &ps);//hPIC_LOGO Handle to Picture Control
    mDC = CreateCompatibleDC(hdc);
    iRet = GDIPCreate(mDC, &graphics);
    WCHAR szFilePath[256];
    MultiByteToWideChar(CP_ACP, 0, TEXT("1.bmp"), -1, szFilePath, 256);
    iRet = GDIPLoadImgFile(szFilePath, &img);
    iRet = GDIPGetImgHeight(img, &lngHeight);
    iRet = GDIPGetImgWidth(img, &lngWidth);
    iRet = GDIPDrawImg(graphics, img, 0, 0);
    if (iRet != 0){
        MB(TEXT("ERROR,GDIPDrawImg"), 1);//MessageBox
    }
    DeleteDC(mDC);
    EndPaint(hPIC_LOGO, &ps);
}

I'm using plain C to call these functions of GDI+ ,and response WM_PAINT with this function ,the last call of GdipDrawImage returns 0, it means success ,but on my picture control I can not find my picture on it.
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

Before starting to deal with any runtime issues, throw out this code and write it in human way. Look at the two first lines inside the implementation of LoadImgFromGDIP: you call LoadLibrary again and again with the same DLL. This is not yet a reason for some runtime failures, but this is more enough for stopping to look at your code anymore. It's hard to imagine that a person doing weirdest things can accomplish something more complex; I would be glad if I'm wrong about you, but you certainly need to make a first step and fix it.

Please, no offense; and sorry for not giving you a complete solution; please understand that if could hardly really help you. You really need to learn how to write reasonable code yourself.

—SA
   
Comments
Member 11643863 29-Apr-15 0:43am
   
Well ,I got your advice ,I have fixed the issue which is calling LoadLibrary again and again with the same DLL ,but I still don't know what is wrong with it ,I know my codes are hard to read ,I am testing these codes ,so I will reorganise codes later ,can you please give some more advice about this question?
Rate this:
Please Sign up or sign in to vote.

Solution 2

Don't use LoadLibrary, link all standard libraries into your application, that will ensure that you get the correct versions. And you should not be using plain C to use GDI+ as it is an object based system: see http://msdn.microsoft.com/en-us/library/ms533798(VS.85).aspx[^].

You are also painting your window in the wrong place, you should only do it in response to a WM_PAINT message. There are many tutorials on the internet that explain how to write Windows programs properly. See http://www.winprog.org/tutorial/[^] for a good starter.
   
Comments
Member 11643863 29-Apr-15 19:57pm
   
Yes ,I responded to the WM_PAINT message with these codes ,and I really want to GDI+ with plain C ,so do you have any advice for "how to use GDI+ in plain C" here?
Richard MacCutchan 30-Apr-15 3:58am
   
You cannot use it in plain C, it is a class based library. See https://msdn.microsoft.com/en-us/library/ms536382(v=vs.85).aspx.
Member 11643863 30-Apr-15 18:00pm
   
I have read some VB source codes ,they are using flat api of gdi+ ,and actually they work well ,why can't I use that flat api in plain C?
https://msdn.microsoft.com/en-us/library/windows/desktop/ms534038(v=vs.85).aspx
Richard MacCutchan 1-May-15 2:59am
   
Try it and see what happens. But, as the notes on that page clearly state, if it doesn't work then you will get no help.
Member 11643863 1-May-15 6:21am
   
:)Thank you for your help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100