2017-04-16 2 views
0

모양은 사각형 (예를 들어 "hello"라고 함)이있는 모양을 가정하면 간단합니다. 따라서 사각형의 테두리를 가로 지르는 텍스트를 여러 번 쓸 것입니다 예를 들면 다음과 같습니다.도형에 텍스트를 그리는 방법은 무엇입니까? C#

hello hello hello hello 

hello    hello 

hello    hello 

hello hello hello hello 

그래픽 변수를 사용해야 할 필요가 있다고 가정합니다. 어떻게 해야할지 잘 모릅니다.

비트 맵 객체에 문자열을 그리기위한 코드 : 사전에

Bitmap tempp = new Bitmap(1, 1); 
Graphics g = Graphics.FromImage(tempp); 
SizeF w = g.MeasureString("22", new Font("Tahoma", 200));//in order to get the size of the string as a pixel measurement 
Bitmap bmp = new Bitmap((int)w.Width+1, (int)w.Height+1);//the bitmap that will contain the text as a picture 
RectangleF rectf = new RectangleF(0, 0, (int)w.Width+1, (int)w.Height+1); 
g = Graphics.FromImage(bmp); 
g.SmoothingMode = SmoothingMode.AntiAlias; 
g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
g.PixelOffsetMode = PixelOffsetMode.HighQuality; 
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; 
StringFormat format = new StringFormat() 
{ 
    Alignment = StringAlignment.Center, 
    LineAlignment = StringAlignment.Center 
}; 
g.DrawString("22", new Font("Tahoma", 200), Brushes.Black, rectf, format); 
g.Flush(); 

감사합니다. 두 번째 의견에 대한

:

hello hello hello 
hel   llo 
hel   llo 
hello hello hello 
+1

당신이 문자열 - 이미지의 크기를 갖게되면, 최종 사각형의 전체 크기를 계산에, 그것을 바탕으로 모든 사본을 여러 문자열 이미지 사본을 넣어 그 자신의 좌표. 어쩌면 [이 링크] (http://stackoverflow.com/questions/1478022/c-sharp-get-a-controls-position-on-a-form)가 도움이 될 수 있습니다. –

+0

사각형의 크기는 어떻게됩니까? 4 개의 "hello"를 연속적으로 갖기에는 너무 크지 만 5 개의 경우에는 너무 작 으면 어떨까요? 그런 다음 사각형의 크기를 조정합니까? 또는 텍스트 사이의 간격을 조정합니까? – Anthony

+0

@Anthony 이렇게 : 그렇게 코드를 보여줄 수 없기 때문에 스레드를 편집 할 것입니다. –

답변

1

나는 이것이 당신이 필요 바랍니다. 여기서 설명 할 내용이 많지 않습니다. 논리는 매우 간단합니다.

public string MyString = "Hello" 
    protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 
     var g = e.Graphics; 
     var strFont = new Font("Tahoma", 10); 
     var strSizeF = g.MeasureString(MyString, strFont); 

     var canvas = new Rectangle 
     { 
      Height = 200, 
      Width = 200, 
      Location = new Point(10, 10), 
     }; 

     g.DrawRectangle(new Pen(new SolidBrush(Color.Blue)), canvas); 

     var nx = (int)(canvas.Width/strSizeF.Width); 
     var ny = (int)(canvas.Height/strSizeF.Height); 
     var spacingX = (canvas.Width - nx * strSizeF.Width)/(nx-1); 
     var spacingY = (canvas.Height - ny * strSizeF.Height)/(ny-1); 

     //draw top row and bottom row 
     int i; 
     for (i = 0; i < nx; i++) 
     { 
      g.DrawString(
       MyString, 
       strFont, 
       Brushes.Black, 
       new PointF(canvas.X + i*(strSizeF.Width + spacingX), canvas.Y) 
       ); 
      g.DrawString(
       MyString, 
       strFont, 
       Brushes.Black, 
       new PointF(canvas.X + i * (strSizeF.Width + spacingX), canvas.Y + canvas.Height - strSizeF.Height) 
      ); 
     } 

     //divide the string into half 
     var isLengthOdd = MyString.Length % 2 != 0; 
     var substr1 = MyString.Substring(0, MyString.Length/2 + (isLengthOdd ? 1 : 0)); 
     var substr2 = MyString.Substring(MyString.Length/2, MyString.Length - MyString.Length/2); 
     var substr2SizeF = g.MeasureString(substr2, strFont); 
     //draw side rows 
     for (i = 1; i < ny - 1; i++) 
     { 
      g.DrawString(
       substr1, 
       strFont, 
       Brushes.Black, 
       new PointF(canvas.X, canvas.Y + i * (strSizeF.Height + spacingY)) 
      ); 
      g.DrawString(
       substr2, 
       strFont, 
       Brushes.Black, 
       new PointF(canvas.X + canvas.Width - substr2SizeF.Width, canvas.Y + i * (strSizeF.Height + spacingY)) 
      ); 
     } 

    } 

결과 :

Result

+0

안녕하십니까, 늦게까지 늦어서 죄송합니다. 아직 답을 찾아 주셨습니다. 좋은 방향을 알려 주셨고, 이해에 많은 도움을 주셨습니다. 답변 해 주셔서 감사합니다 :). –

+1

나는 당신의 대답을 탐구했는데, 내가하고 싶은 것에 정확히 맞지는 않았지만 그래픽으로 작업하는 방법을 보여주었습니다. 귀하의 답변 덕택에 나는 원하는 것을 얻었고 귀하의 답변을 수락했습니다. –

+0

@matanjustme 고맙습니다. 당신이 기꺼이한다면, 당신은 당신 자신의 질문에 대답하고 받아 들일 수 있습니다. 이렇게하면 다른 사람들이 귀하의 질문과 답변을 유용하게 이용할 수 있습니다. :) – Anthony

관련 문제