2016-08-18 7 views
1

定義済みの領域にテキストを書き込むコードがあります。Drawstringを使用してテキストを垂直方向に反転

enter image description here

enter image description here

に私が試してみました:

graphics.DrawString(text, goodFont, Brushes.Black, textarea, stringFormat); 

私はそれから行くように、水平方向にテキストを反転したい場合があります。文字列幅を測定し、その逆を取る:

float w = graphics.MeasureString(text, goodFont).Width; 
graphics.DrawString(text, goodFont, Brushes.Black, -w, 0, stringFormat); 

しかし、私の問題は、テキストがボックス(テキストエリア)に描画したい境界の外側に広がっていることです。

私はボックスの境界を維持しながら、水平にテキストを反転したいと思います。誰でも私の仕事をどのように達成するために正しい方向に向けることができますか?

ありがとうございます!

編集:私はビットマップを作成し、変換を行うことを回避しようとしています。

答えて

2

グラフィックス変換を使用できます。私が見るほうがこのようにMatrix Constructor (Rectangle, Point[])を使用することです:

Point[] transformPoints = 
{ 
    // upper-left: 
    new Point(textarea.Right - 1, textarea.Top), 
    // upper-right: 
    new Point(textarea.Left + 1, textarea.Top), 
    // lower-left: 
    new Point(textarea.Right - 1, textarea.Bottom), 
}; 
var oldMatrix = graphics.Transform; 
var matrix = new Matrix(textarea, transformPoints); 
try 
{ 
    graphics.Transform = matrix; 
    graphics.DrawString(text, goodFont, Brushes.Black, textarea, stringFormat); 
} 
finally 
{ 
    graphics.Transform = oldMatrix; 
    matrix.Dispose(); 
} 

P.S. @serhiybは私の数秒前に同様の答えを出しましたが、これは分かりやすいと思います。ソース矩形を指定するだけで、その左上、右上、左下の点を変換する方法で変換を定義します。

+0

は、このソリューションでは、私は必要なものを私に与えました。本当にありがとう。 – markdozer

2

あなたのようなこと

何かのためTransformation Matrixを使用することができます:あなたは私は盲目的にそれをコーディングしていますが、私はあなたのアイデアを持って期待してマトリックス/エリアパラメータをプレイする必要があるかもしれません

float w = graphics.MeasureString(text, goodFont).Width; 
graphics.MultiplyTransform(new Matrix(-1, 0, 0, 1, w, 0)); 
/* 
Matrix: 
-1 0 
    0 1 
newX -> -x 
newY -> y 
and dx offset = w (since we need to move image to right because of new negative x) 
*/ 
graphics.DrawString(text, goodFont, Brushes.Black, textarea, stringFormat); 
graphics.ResetTransform(); 

3

Matrix Constructorを使用してグラフィックスを変換し、後でDrawStringメソッドを使用してグラフィックスを描画できます。

はこれを試してみてください:

private void Form1_Paint(object sender, PaintEventArgs e) 
{ 
    Graphics g = e.Graphics; 
    string text = "This is a Test"; 
    g.DrawString(text, Font, Brushes.Black, 0, 0); 

    g.MultiplyTransform(new Matrix(-1, 0, 0, 1, 68, 50)); 

    g.DrawString(text, Font, Brushes.Black, 0, 0); 
    g.ResetTransform(); 
} 

出力:

enter image description here

関連する問題