2017-04-16 38 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

矩形的大小如何?如果連續有四個「你好」太大,而五個太小則太大。你是否調整矩形的大小?或者你調整文本之間的間距? – 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