2017-04-16 9 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つの「こんにちは」が連続しているのに大きすぎるが、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

関連する問題