Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 移动图形轴或在任意点获取子图形?_C#_Winforms_Drawing - Fatal编程技术网

C# 移动图形轴或在任意点获取子图形?

C# 移动图形轴或在任意点获取子图形?,c#,winforms,drawing,C#,Winforms,Drawing,我经常需要在Graphics对象中绘制项目,我一直以来的做法是使用一个函数DrawItem,该函数接收Graphics对象和一个offsetX和offsetY参数,这些参数确定项目将在哪个点绘制 问题是DrawItem中的代码看起来会更好,如果Graphics中有一种方法,可以给我一个X轴和Y轴零点位于其他点的图形版本,比如myGraphics.DisplacedGraphics(offsetX,offsetY)。这样,我只需将这个图形对象传递给我的DrawItem方法,该方法不需要接收其他两个

我经常需要在
Graphics
对象中绘制项目,我一直以来的做法是使用一个函数
DrawItem
,该函数接收
Graphics
对象和一个
offsetX
offsetY
参数,这些参数确定项目将在哪个点绘制

问题是
DrawItem
中的代码看起来会更好,如果
Graphics
中有一种方法,可以给我一个X轴和Y轴零点位于其他点的图形版本,比如
myGraphics.DisplacedGraphics(offsetX,offsetY)
。这样,我只需将这个
图形
对象传递给我的
DrawItem
方法,该方法不需要接收其他两个参数。有没有这样的功能或者最接近的东西是什么

编辑:与此同时,这是我写的,但似乎是这样一个基本要求,我仍然希望已经存在这样的功能(我仍然需要添加一系列方法,但这些是我现在所需要的)(请注意
DisplacedCanvas
方法):

我很肯定他们会满足你的要求

原点通常是图形曲面的左上角。转换操作包括将转换矩阵乘以转换部分为dx和dy参数的矩阵。该方法通过在变换矩阵前面加上平移矩阵来应用平移

因此,如果希望新原点为100,50,则在绘制图像之前,首先调用
graphics.TranslateTransform(100,50)

public class Canvas
{
    private readonly Graphics _Graphics;
    private readonly int _OriginX = 0;
    private readonly int _OriginY = 0;

    public Canvas(Graphics graphics, int originX, int originY)
    {
        _Graphics = graphics;
        _OriginX = originX;
        _OriginY = originY;
    }

    public Canvas(Graphics graphics) : this(graphics, 0, 0) { }

    public SizeF MeasureString(string text, Font font)
    {
        return _Graphics.MeasureString(text, font);
    }

    public void FillRectangle(Brush brush, int x, int y, int width, int height)
    {
        _Graphics.FillRectangle(brush, _OriginX + x, _OriginY + y, width, height);
    }

    public void DrawString(string s, Font font, Brush brush, float x, float y)
    {
        _Graphics.DrawString(s, font, brush, _OriginX + x, _OriginY + y);
    }

    public Canvas DisplacedCanvas(int x, int y)
    {
        return new Canvas(_Graphics, _OriginX + x, _OriginY + y);
    }
}