Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Gdiplus::Graphics::DrawString throw the error code "InvalidParameter(2)" on some Computer(window xp) why?

hi,everyone

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.

C++
LRESULT CMainDlg::OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	CPaintDC _dc(m_hWnd);
	RECT rc;
	GetClientRect(&rc);
	CMemoryDC _memdc(_dc,rc);
	_memdc.FillSolidRect(&rc,RGB(255,255,255));

	RECT rcText={20,20,200,40};
	std::wstring sTest=_T("this draw by DrawText!");
	_memdc.DrawText(sTest.c_str(),sTest.length(),&rcText,DT_LEFT|DT_SINGLELINE|DT_VCENTER);

	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;
	_strformat.SetTrimming(StringTrimmingEllipsisWord); 
	_strformat.SetLineAlignment(StringAlignmentCenter);
	Status sNext = grap.DrawString(sTest.c_str(),sTest.length(),&font,gRect,&_strformat,&SolidBrush2);
        //i get the error code here,sNext=2;
	if(sNext!=S_OK)
		Save(sNext);

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

       //i get the error code here too,sNex2t=2;
	if(sNext2!=S_OK)
		Save(sNext2);
	return TRUE;
}
Posted
Updated 6-Nov-12 2:31am
v2
Comments
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.
Jochen Arndt 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.
Jochen Arndt 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.

1 solution

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,
            Gdiplus::FontStyleRegular,Gdiplus::UnitPixel);
        st = font.GetLastStatus();
        if (Gdiplus::Ok == st)
        {
            grap.SetSmoothingMode( SmoothingModeHighSpeed );
            StringFormat _strformat;
            _strformat.SetTrimming(StringTrimmingEllipsisWord); 
            _strformat.SetLineAlignment(StringAlignmentCenter);
            st = grap.DrawString(sTest.c_str(),sTest.length(),&font,gRect,
                &_strformat,&SolidBrush2);
        }
    }
}
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:
C++
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(),
    12,Gdiplus::FontStyleRegular,Gdiplus::UnitPixel);
 
Share this answer
 
Comments
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.
Jochen Arndt 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 http://en.wikipedia.org/wiki/List_of_CJK_fonts 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900