2016-11-01 2 views
-2

CodeA :Drawstring이 서로 겹치고 있습니까?

Image imageChipsetName = new System.Drawing.Bitmap(photoWidth, photoHeight); 

StringFormat strFormat = new StringFormat(); 
strFormat.Alignment = StringAlignment.Center; 
strFormat.LineAlignment = StringAlignment.Center; 

Graphics graphics = Graphics.FromImage(imageChipsetName); 
graphics.DrawString(stringA + "\n", 
        new Font("Tahoma", 14, FontStyle.Underline), Brushes.Black, 
        new RectangleF(0, 0, photoWidth, photoHeight), strFormat); 
graphics.DrawString(stringB, 
        new Font("Tahoma", 14), Brushes.Black, 
        new RectangleF(0, 0, photoWidth, photoHeight), strFormat); 

CodeB :

Image imageChipsetName = new System.Drawing.Bitmap(photoWidth, photoHeight); 

StringFormat strFormat = new StringFormat(); 
strFormat.Alignment = StringAlignment.Center; 
strFormat.LineAlignment = StringAlignment.Center; 

Graphics graphics = Graphics.FromImage(imageChipsetName); 
graphics.DrawString(stringA + "\n"+stringB, 
        new Font("Tahoma", 14, FontStyle.Underline), Brushes.Black, 
        new RectangleF(0, 0, photoWidth, photoHeight), strFormat); 

나는 상자 내에서 두 문자열을 그릴 필요가있다. 밑줄 스타일이있는 StringA StringB는 그렇지 않습니다.

코드 B는 내가 원하는 것을 거의 달성하지만 stringAstringB은 같은 스타일을 공유합니다. 그래서 CodeA로 테스트했는데, 그 프로그램은 두 문자열이 서로 겹치는 것입니다. 알 수 있어요

+0

'DrawString()'은 한 번의 호출로 줄 바꿈 만 처리합니다. 그것은 하나의 호출에서 다음 호출로 상태를 가지지 않습니다. 따라서 여러 줄로 다른 스타일로 그릴 필요가 있다면'DrawString()'을 호출 할 때마다 문자열을 올바르게 그려야 할 위치를 지정해야합니다. 관련 세부 정보 및 추가 정보 링크는 중복 표시된 부분을 참조하십시오. –

답변

0

codeA의 문제점은 stringA와 stringB가 모두 동일한 위치에 그려지는 것입니다.

graphics.DrawString 문자열을 이미지로 바꾸어 용지에 인쇄하십시오. "\ n"문자열이 이미지로 바뀌면 아무런 의미가 없습니다. 인쇄되지 않거나 새로운 라인을 생성합니다. 사실 종이에 "선"이 없습니다. 그냥 이미지.

문자열 B에 다른 위치를 지정해야합니다. Graphics.MeasureString (String, Font)을 사용하여 stringA의 크기를 측정 한 다음 결과에 따라 stringB의 위치를 ​​조정하십시오.

Image imageChipsetName = new System.Drawing.Bitmap(photoWidth, photoHeight); 

StringFormat strFormat = new StringFormat(); 
strFormat.Alignment = StringAlignment.Center; 
strFormat.LineAlignment = StringAlignment.Center; 
Font strFontA = new Font("Tahoma", 14, FontStyle.Underline);//Font used by stringA 


Graphics graphics = Graphics.FromImage(imageChipsetName); 
graphics.DrawString(stringA + "\n", 
        strFont_A, Brushes.Black, 
        new RectangleF(0, 0, photoWidth, photoHeight), strFormat); 

SizeF stringSizeA = new SizeF(); 
stringSizeA = Graphics.MeasureString(stringA, strFont_A);//Measuring the size of stringA 

graphics.DrawString(stringB, 
        new Font("Tahoma", 14), Brushes.Black, 
        new RectangleF(0, stringSizeA.Height, photoWidth, photoHeight - stringSizeA.Height), strFormat); 
관련 문제