+ yを上げて、Origin C#System.Drawing.Graphicsを移動します
-
06-07-2019 - |
質問
原点をウィンドウの中心にしたい。
______________ | ^ | | | | | o----->| | | |____________|
.NETは、左上隅に配置することを望んでいます。
_____________> | | | | | | | | V____________|
ドットネットと私は仲良くしようとしている。
Graphicsオブジェクトを使用してC#でこれを行う方法を知っている人はいますか?
Graphics.TranslateTransformは、座標を上下逆にしたままにするため、これを行いません。このGraphics.ScaleTransform(1、-1)を組み合わせても、テキストが逆さまに表示されるため、満足のいくものではありません。
解決
1つの解決策は、TranslateTransformプロパティを使用することです。次に、Point / PointF構造体を使用する代わりに、Point / PointFへの暗黙的なキャストを持つ独自のFlippedPoint / FlippedPointF構造体を作成できます(ただし、それらをキャストすることにより、座標が反転します)。
public struct FlippedPoint
{
public int X { get; set; }
public int Y { get; set; }
public FlippedPoint(int x, int y) : this()
{ X = x; Y = y; }
public static implicit operator Point(FlippedPoint point)
{ return new Point(-point.X, -point.Y); }
public static implicit operator FlippedPoint(Point point)
{ return new FlippedPoint(-point.X, -point.Y); }
}
他のヒント
ScaleTransform(1、-1)
の使用を継続し、テキストの描画中に現在の変換を一時的にリセットできます。
// Convert the text alignment point (x, y) to pixel coordinates
PointF[] pt = new PointF[] { new PointF(x, y) };
graphics.TransformPoints(CoordinateSpace.Device, CoordinateSpace.World, pt);
// Revert transformation to identity while drawing text
Matrix oldMatrix = graphics.Transform;
graphics.ResetTransform();
// Draw in pixel coordinates
graphics.DrawString(text, font, brush, pt[0]);
// Restore old transformation
graphics.Transform = oldMatrix;
負の高さのグラフィックスオブジェクトを作成してみてください。私はC#ライブラリを特に知りませんが、このトリックはGDIの最近のバージョンで動作します。
所属していません StackOverflow