Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
date: 2025-12-01
description: 学习如何在 .NET 中使用 Aspose.Drawing 执行坐标系转换并绘制矩形图形。一步一步的页面坐标转换指南
date: 2026-02-04
description: 了解如何在 .NET 中使用 Aspose.Drawing 转换坐标并绘制矩形图形。关于页面坐标转换的分步指南
linktitle: Coordinate System Transformation in Aspose.Drawing
second_title: Aspose.Drawing .NET API – Alternative to System.Drawing.Common
title: 坐标系转换 – Aspose.Drawing for .NET 中的页面转换
title: 如何转换坐标 – Aspose.Drawing for .NET 中的页面转换
url: /zh/net/coordinate-transformations/page-transformation/
weight: 13
---
Expand All @@ -12,132 +12,132 @@ weight: 13
{{< blocks/products/pf/main-container >}}
{{< blocks/products/pf/tutorial-page-section >}}

# 坐标系转换 – Aspose.Drawing for .NET 中的页面转换
# 如何转换坐标 – Aspose.Drawing for .NET 中的页面转换

## 介绍

欢迎!在本教程中,您将了解 **如何使用 Aspose.Drawing for .NET 转换页面坐标**,并学习 **坐标系转换** 的基础知识。无论您是在构建图形密集型应用,还是需要对绘图单位进行精确控制,本指南都会一步步带您完成——从设置画布到绘制矩形图形元素。阅读完本教程后,您将能够自信地在自己的项目中应用这些技术。
Welcome! In this tutorial you’ll discover **how to transform coordinates** using Aspose.Drawing for .NET and learn the basics of **coordinate system transformation**. Whether you’re building a graphics‑intensive application or need precise control over drawing units, this guide walks you through every step—from setting up the canvas to drawing a rectangle graphics element. By the end, you’ll be able to apply these techniques in your own projects with confidence.

## 快速答案
- **什么是坐标系转换?** 将页面级单位(如英寸)映射到设备级像素。
- **为什么使用 Aspose.Drawing?** 它提供了完全托管的 System.Drawing.Common 替代方案,支持跨平台。
- **实现示例需要多长时间?** 基本的页面转换大约需要 5‑10 分钟。
- **需要许可证吗?** 开发阶段可使用免费试用版;生产环境需要商业许可证。
- **支持哪些 .NET 版本?** .NET Framework 4.6+.NET Core 3.1+.NET 5/6/7
## 快速回答
- **坐标系转换是什么?** Mapping page‑level units (like inches) to device‑level pixels.
- **为什么使用 Aspose.Drawing?** It offers a fully managed, cross‑platform alternative to System.Drawing.Common.
- **实现示例需要多长时间?** About 5‑10 minutes for a basic page transformation.
- **我需要许可证吗?** A free trial works for development; a commercial license is required for production.
- **支持哪些 .NET 版本?** .NET Framework 4.6+, .NET Core 3.1+, .NET 5/6/7.

## 什么是坐标系转换?
## 在 Aspose.Drawing 中转换坐标

**坐标系转换** 定义了在渲染图形时,逻辑页面单位(如英寸、厘米或点)如何转换为设备像素。通过配置 `Graphics.PageUnit` 属性,您告诉绘图引擎如何解释您提供的坐标,从而实现对尺寸和布局的细粒度控制。
A **coordinate system transformation** defines how logical page units (such as inches, centimeters, or points) are converted into device pixels when rendering graphics. By configuring the `Graphics.PageUnit` property you tell the drawing engine how to interpret the coordinates you supply, giving you fine‑grained control over size and layout.

## 为什么在 Aspose.Drawing 中使用坐标系转换?

- **设备无关设计:** 编写一次代码,Aspose.Drawing 会为任何屏幕或打印机处理像素缩放。
- **精确绘图:** 适用于技术图表、CAD 风格草图或任何对精确测量有要求的场景。
- **跨平台可靠性:** WindowsLinux、macOS 上表现一致,避免了 System.Drawing 的 GDI+ 限制。
- **设备无关的设计:** Write code once and let Aspose.Drawing handle the pixel scaling for any screen or printer.
- **精确绘图:** Ideal for technical diagrams, CAD‑style sketches, or any scenario where exact measurements matter.
- **跨平台可靠性:** Works consistently on Windows, Linux, and macOS without the GDI+ limitations of System.Drawing.

## 前置条件

在开始之前,请确保您已具备:
Before we start, ensure you have:

- **Aspose.Drawing 库:** 从官方站点 [here](https://releases.aspose.com/drawing/net/) 下载最新版本。
- **开发环境:** Visual StudioRider 或任意 .NET 兼容的 IDE
- **文档目录:** 将代码中的 `"Your Document Directory"` 替换为您希望保存输出图像的文件夹路径。
- **Aspose.Drawing 库:** Download the latest version from the official site [here](https://releases.aspose.com/drawing/net/).
- **开发环境:** Visual Studio, Rider, or any .NET‑compatible IDE.
- **文档目录:** Replace `"Your Document Directory"` in the code with the folder where you want the output image saved.

准备就绪后,让我们进入逐步指南。
Now that everything is ready, let’s dive into the step‑by‑step guide.

## 导入命名空间

首先,将所需的命名空间引入项目:
First, bring the required namespace into your project:

```csharp
using System.Drawing;
```

## 步骤 1:创建 Bitmap
## 第一步:创建位图

我们先创建一个空白的 bitmap,作为绘图表面。像素格式 `Format32bppPArgb` 提供高质量的预乘 Alpha 支持。
We start by creating a blank bitmap that will serve as the drawing surface. The pixel format `Format32bppPArgb` gives us high‑quality, premultiplied alpha support.

```csharp
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
```

## 步骤 2:创建 Graphics 对象
## 第二步:创建 Graphics 对象

`Graphics` 对象为 bitmap 提供绘图 API。它是代码与像素缓冲区之间的桥梁。
A `Graphics` object provides the drawing API for the bitmap. It’s the bridge between your code and the pixel buffer.

```csharp
Graphics graphics = Graphics.FromImage(bitmap);
```

## 步骤 3:清除画布
## 第三步:清除画布

为画布设置中性背景,使绘制的形状更突出。这里我们使用浅灰色填充。
Give the canvas a neutral background so the drawn shapes stand out. Here we fill it with a light gray.

```csharp
graphics.Clear(Color.FromKnownColor(KnownColor.Gray));
```

## 步骤 4:设置转换(如何转换页面)
## 第四步:设置转换(如何转换页面)

要将页面坐标映射到设备像素,设置 `PageUnit` 属性。在本例中我们选择英寸,您也可以使用 `GraphicsUnit.Millimeter``Point` 等。
To map page coordinates to device pixels, set the `PageUnit` property. In this example we choose inches, but you could also use `GraphicsUnit.Millimeter`, `Point`, etc.

```csharp
graphics.PageUnit = GraphicsUnit.Inch;
```

## 步骤 5:绘制矩形 – draw rectangle graphics
## 第五步:绘制矩形 – 绘制矩形图形

现在使用细蓝色笔绘制矩形。由于我们切换到了英寸,矩形的尺寸和位置以英寸为单位,代码在面向打印的布局中更易读。
Now we draw a rectangle using a thin blue pen. Because we switched to inches, the rectangle’s size and position are expressed in inches, making the code more readable for print‑oriented layouts.

```csharp
Pen pen = new Pen(Color.FromKnownColor(KnownColor.Blue), 0.1f);
graphics.DrawRectangle(pen, 1, 1, 1, 1);
```

## 步骤 6:保存图像
## 第六步:保存图像

最后,将 bitmap 写入 PNG 文件,保存到前面指定的文件夹中。
Finally, write the bitmap to a PNG file in the folder you specified earlier.

```csharp
bitmap.Save("Your Document Directory" + @"CoordinateSystemsTransformations\PageTransformation_out.png");
```

恭喜!您刚刚完成了 **坐标系转换**,将页面单位设置为英寸,并使用 Aspose.Drawing 在 bitmap 上 **绘制矩形图形**。
Congratulations! You’ve just performed a **coordinate system transformation**, set the page unit to inches, and **draw rectangle graphics** on a bitmap using Aspose.Drawing.

## 常见问题及解决方案

| 问题 | 产生原因 | 解决办法 |
|------|----------|----------|
| **未创建输出文件** | 路径错误或文件夹不存在 | 确认目标目录存在,或在保存前使用 `Directory.CreateDirectory` 创建。 |
| **矩形出现畸形** | `PageUnit` 设置错误或 DPI 不匹配 | 检查 `graphics.PageUnit` 是否与您使用的单位一致,并确保 bitmap 的 DPI 正确(默认 96 DPI)。 |
| **许可证异常** | 生产环境未使用有效许可证 | 在创建 graphics 对象之前应用临时或永久的 Aspose.Drawing 许可证。 |
| 问题 | 原因 | 解决方案 |
|-------|----------------|-----|
| **未创建输出文件** | Incorrect path or missing folder | Ensure the target directory exists or use `Directory.CreateDirectory` before saving. |
| **矩形出现变形** | Wrong `PageUnit` or mismatched DPI | Verify that `graphics.PageUnit` matches the units you intend to use and that the bitmap DPI is set appropriately (default is 96 DPI). |
| **许可证异常** | Running without a valid license in production | Apply your temporary or permanent Aspose.Drawing license before creating graphics objects. |

## 常见问答

**Q: 可以免费使用 Aspose.Drawing 吗?**
A: 可以,免费试用版请点击 [here](https://releases.aspose.com/)
**问:我可以免费使用 Aspose.Drawing 吗?**
答:Yes, a free trial is available [here](https://releases.aspose.com/).

**Q: 哪里可以找到 Aspose.Drawing 的详细文档?**
A: 完整的 API 参考位于 [here](https://reference.aspose.com/drawing/net/)
**问:在哪里可以找到 Aspose.Drawing 的详细文档?**
答:The full API reference is located [here](https://reference.aspose.com/drawing/net/).

**Q: 如何获取 Aspose.Drawing 的技术支持?**
A: 访问 [Aspose.Drawing Forum](https://forum.aspose.com/c/drawing/44) 获取社区帮助和官方支持。
**问:如何获取 Aspose.Drawing 的支持?**
答:Visit the [Aspose.Drawing Forum](https://forum.aspose.com/c/drawing/44) for community help and official assistance.

**Q: 是否提供 Aspose.Drawing 的临时许可证?**
A: 当然,您可以在 [here](https://purchase.aspose.com/temporary-license/) 获取。
**问:Aspose.Drawing 是否提供临时许可证?**
答:Absolutely—obtain one [here](https://purchase.aspose.com/temporary-license/).

**Q: 哪里可以购买完整的 Aspose.Drawing 许可证?**
A: 请前往 [here](https://purchase.aspose.com/buy) 进行购买。
**问:在哪里可以购买完整的 Aspose.Drawing 许可证?**
答:You can buy it [here](https://purchase.aspose.com/buy).

## 结论

本指南涵盖了在 Aspose.Drawing 中进行 **坐标系转换** 的全部要点:设置画布、配置页面单位、精确绘制矩形图形以及保存结果。使用这些技术,您可以为报表、CAD 风格绘图或任何对测量精度有要求的应用构建可伸缩、设备无关的图形。
In this guide we covered everything you need to know about **how to transform coordinates** in Aspose.Drawing: setting up the canvas, configuring page units, drawing precise rectangle graphics, and saving the result. Use these techniques to build scalable, device‑independent graphics for reports, CAD‑style drawings, or any application where measurement accuracy matters.

---

**最后更新:** 2025-12-01
**测试环境:** Aspose.Drawing 24.12 for .NET
**作者:** Aspose
**Last Updated:** 2026-02-04
**Tested With:** Aspose.Drawing 24.12 for .NET
**Author:** Aspose

{{< /blocks/products/pf/tutorial-page-section >}}

Expand Down
Loading