2009-06-23 2 views
5

원점이 내 창 가운데에 있도록하고 싶습니다.만들기 + y UP, 원본 이동 C# System.Drawing.Graphics

 

______________ 
| ^ | 
|  |  | 
|  o----->| 
|   | 
|____________| 

.NET이 맨 위 왼쪽 구석에 있기를 원합니다.

 

_____________> 
|   | 
|   | 
|   | 
|   | 
V____________| 
내가 함께 얻으려고 노력하고

점 그물 ..

사람이 바로 그래픽 객체를 사용하여 C#에서이 작업을 수행하는 방법을 알고 있나요?

Graphics.TranslateTransform은 좌표가 거꾸로 뒤집어 져 있기 때문에 수행하지 않습니다. 이 Graphics.ScaleTransform (1, -1)을 결합하면 텍스트가 거꾸로 나타나기 때문에 만족스럽지 않습니다.

답변

1

한 가지 해결책은 TranslateTransform 속성을 사용하는 것입니다. 그런 다음, 대신 포인트/PointF 구조체를 사용하는 당신은/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); } 
} 
0

음수 높이로 그래픽 객체를 생성 해보십시오. 특히 C# 라이브러리에 대해 잘 모릅니다. 그러나이 트릭은 최신 버전의 GDI에서 작동합니다.

1

당신은 계속 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;