C#根据 graphic的transform 和现有的RectangleF,反算原始的矩形区域
时间: 2024-03-21 09:40:41 浏览: 25
要根据 `Graphics` 的 `Transform` 和现有的 `RectangleF` 反算原始的矩形区域,可以使用 `Matrix` 类的 `Invert` 方法。
具体做法如下:
1. 创建一个 `Matrix` 对象,将其设置为 `Graphics` 对象的 `Transform` 属性。
2. 调用 `Matrix` 对象的 `Invert` 方法,得到一个反转后的矩阵。
3. 使用反转后的矩阵对现有的 `RectangleF` 进行变换,得到原始的矩形区域。
以下是代码示例:
```csharp
// 假设有一个 Graphics 对象 g 和一个 RectangleF 对象 rect
Matrix matrix = g.Transform;
matrix.Invert();
PointF[] points = { rect.Location, new PointF(rect.Right, rect.Bottom) };
matrix.TransformPoints(points);
RectangleF originalRect = new RectangleF(points[0], new SizeF(points[1].X - points[0].X, points[1].Y - points[0].Y));
```
上述代码中,我们首先获取了 `Graphics` 对象的 `Transform` 属性,并将其存储在一个 `Matrix` 对象中。然后调用 `Invert` 方法得到反转后的矩阵。接着,我们将现有的 `RectangleF` 对象的左上角和右下角坐标存储在一个 `PointF` 数组中,使用反转后的矩阵对这个数组进行变换,得到变换后的坐标。最后,我们使用这些坐标计算出原始的矩形区域。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)