Click here to Skip to main content
13,554,429 members
Rate this:
Please Sign up or sign in to vote.
See more:
Gdiplus::Graphics::DrawString throw the error code "InvalidParameter(2)" on some Computer(window xp) why?


My program run on most of the computer(window xp).
but someone tell me that my progrom run on his computer can't see the text,i try to get the error code, i get the code 2(InvalidParameter),but my Parameter shall be right,Because it work well on most of the computer(window xp).

when i run my demo on his computer, i can see one line.

the key code.

LRESULT CMainDlg::OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	CPaintDC _dc(m_hWnd);
	RECT rc;
	CMemoryDC _memdc(_dc,rc);

	RECT rcText={20,20,200,40};
	std::wstring sTest=_T("this draw by DrawText!");

	RectF gRect(20,40,200,60);
	sTest=_T("this draw by GDI+ and 255!");
	Gdiplus::Graphics grap(_memdc);
	Gdiplus::SolidBrush SolidBrush2(Color(255,51,51,51));
	Gdiplus::FontFamily fontFamily(_T("宋体"));
	Gdiplus::Font font(&fontFamily,12,Gdiplus::FontStyleRegular,Gdiplus::UnitPixel);
	grap.SetSmoothingMode( SmoothingModeHighSpeed );
	StringFormat _strformat;
	Status sNext = grap.DrawString(sTest.c_str(),sTest.length(),&font,gRect,&_strformat,&SolidBrush2);
        //i get the error code here,sNext=2;

	RectF gRect2(20,60,200,80);
	sTest=_T("this draw by GDI+ and 254!");
	Status sNext2 = grap.DrawString(sTest.c_str(),sTest.length(),&font,gRect2,&_strformat,&SolidBrush2);

       //i get the error code here too,sNex2t=2;
	return TRUE;
Posted 5-Nov-12 21:41pm
Updated 6-Nov-12 2:31am
Richard-MacCutchan 6-Nov-12 5:20am
Which line of code gives the error?
lin98666521 6-Nov-12 8:32am
sNext and sNext2,they all Equal to 2.
User-2223753 6-Nov-12 6:33am
It's hard to guess what is wrong.

To check for errors, add GetLastStatus() calls after constructing objects. My first choice would be calling fontFamily.GetLastStatus() because the family used in your code may be not present on all systems.
lin98666521 6-Nov-12 8:39am
i think that the fontFamily maybe wrong,but i think This is a small possibility.The Function DrawText work well,it also use the fontfamily "宋体" in my code.
User-2223753 6-Nov-12 8:49am
DrawText() does not use the FontFamily object. It uses the device context's selected font.

The invalid parameter error indicates that one of the DrawString() parameters is invalid. So just check them all by using GetLastStatus():
Gdiplus::FontFamily fontFamily(_T("宋体"));
if (fontFamily.GetLastStatus())
// error
and also for Font and StringFormat.
lin98666521 6-Nov-12 9:02am
i will try . It is now ten p.m. in my country,Tomorrow I will try to check them on that computer.
lin98666521 7-Nov-12 0:17am
hi,i do that as what you tell me.
you are right, fontFamily.GetLastStatus() is wrong,i make a program to test,
they all get error,such as fontFamily(_T("宋体")); fontFamily(_T("Arial"));

do you have other way to make sure fontfamily to be right?

lin98666521 7-Nov-12 0:31am
FontFamilyNotFound = 14
i get the error code is 14(FontFamilyNotFound);
User-2223753 7-Nov-12 3:08am
The error message is clear. You noted that the error occurs on one system only. So there must be something wrong with the fonts on that system.

An alternative may be the usage of a generic typeface:
A good option is a fallback to a generic face when constructing the font family fails:
font(fontFamily.GetLastStatus() ? Gdiplus::FontFamily::GeneriySansSerif() : &fontFamily, 12);

One more note:
The construction parameter is a WCHAR string. You should use:
FontFamily fonfFamily(L"Arial");
With Unicode builds using the _T() macro does not care but should be avoided where a wide string must be passed.
lin98666521 7-Nov-12 7:53am
thanks,I knew very little about the gdiplus and I did not take effective measures to the Specific system. i think is my mistake.
Tomorrow I will try to use "Gdiplus::FontFamily::GeneriySansSerif()"
i hope it can work well.
lin98666521 7-Nov-12 21:50pm
hi,i try to use Gdiplus::FontFamily::GenericSansSerif(),when the text is english it work well and if the text is Chinese,it look like "□□□□□□□□". i think that the font do not Support Chinese. do you have another way to Other methods to solve the problem.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Using comments the problem has been clarified and I will give a summary here as solution.

The Invalid Parameter error code 2 indicates that one or more of the DrawString() parameters are invalid. So they must be checked. Many GDI+ plus objects are constructed with parameters. Because constructors does not return a value and no exceptions are thrown, GDI+ plus provides GetLastStatus() member functions that can be used after construction to check for success. So the code can be updated with error checking:
Gdiplus::SolidBrush SolidBrush2(Color(255,51,51,51));
Status st = SolidBrush2.GetLastStatus();
if (Gdiplus::Ok == st)
    Gdiplus::FontFamily fontFamily(L"宋体");
    st = fontFamily.GetLastStatus();
    if (Gdiplus::Ok == st)
        Gdiplus::Font font(&fontFamily,12,
        st = font.GetLastStatus();
        if (Gdiplus::Ok == st)
            grap.SetSmoothingMode( SmoothingModeHighSpeed );
            StringFormat _strformat;
            st = grap.DrawString(sTest.c_str(),sTest.length(),&font,gRect,
if (Gdiplus::Ok != st)
    // Insert error handling/message here

A potential error candidate is the FontFamily construction. The specified family may be not present on all systems. To avoid errors on such systems, a fallback using a generic type face may be used:
Gdiplus::FontFamily fontFamily(L"宋体");
// If construction of font family fails fall back to a generic face.
// Provided are GenericMonoSpace, GenericSansSerif, and GenericSerif.
Gdiplus::Font font(Gdiplus::Ok == fontFamily.GetLastStatus() ?
    &fontFamily : Gdiplus::FontFamily::GenericSansSerif(),
lin98666521 7-Nov-12 22:22pm
hi,i try to use Gdiplus::FontFamily::GenericSansSerif(),when the text is english it work well and if the text is Chinese,it look like "□□□□□□□□". i think that the font do not Support Chinese. do you have another way to Other methods to solve the problem.
User-2223753 8-Nov-12 3:18am
I assume that GenericSansSerif uses Arial which does not support Chinese. Maybe GenericMonoSpace will do it (most Chinese fonts are mono spaced). As a German I'm not familar with Chinese fonts.

See for a list of CJK fonts shipped with Windows. Using one of these which is shipped with XP should do the work as fallback (pick one that is shipped with all Windows versions, not only with Chinese Windows versions; your app may be used with a non-Chinese Windows version).
lin98666521 8-Nov-12 3:27am
thanks,I understand.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180515.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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