Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC GDI+
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.
 
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 5-Nov-12 22:41pm
Edited 6-Nov-12 3:31am
v2
Comments
Richard MacCutchan at 6-Nov-12 5:20am
   
Which line of code gives the error?
lin98666521 at 6-Nov-12 8:32am
   
sNext and sNext2,they all Equal to 2.
Jochen Arndt at 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 at 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 at 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 at 6-Nov-12 9:02am
   
thanks
i will try . It is now ten p.m. in my country,Tomorrow I will try to check them on that computer.
lin98666521 at 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"));
fontFamily(_T("微软雅黑"));.
 
do you have other way to make sure fontfamily to be right?
 
lin98666521 at 7-Nov-12 0:31am
   
FontFamilyNotFound = 14
i get the error code is 14(FontFamilyNotFound);
Jochen Arndt at 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:
font(Gdiplus::FontFamily::GeneriySansSerif(),12,Gdiplus::FontStyleRegular,Gdiplus::UnitPixel);
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 at 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 at 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
good
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,
            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:
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);
  Permalink  
Comments
lin98666521 at 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 at 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 at 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
0 Marcin Kozub 330
1 OriginalGriff 256
2 Sergey Alexandrovich Kryukov 215
3 Praneet Nadkar 197
4 Richard MacCutchan 182
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2014
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