如何使用 GroupDocs.Conversion for .NET 将 DGN 转换为 PNG:完整指南

将 DGN 文件转换为 PNG 图像是工程师、建筑师以及任何需要以轻量级、适合网页的图片形式共享 CAD 图纸的人的常见任务。在本教程中,您将学习如何使用 GroupDocs.Conversion for .NET 快速可靠地 convert dgn to png。我们将逐步演示安装、加载 DGN 文件、配置 PNG 选项以及保存结果,同时解释每一步对性能和准确性的意义。

快速答案

  • 哪个库负责转换? GroupDocs.Conversion for .NET.
  • 我能一次性转换多页 DGN 吗? Yes – the API processes each page automatically.
  • 基本使用是否需要许可证? A trial works for development; a full license is required for production.
  • 支持哪些 .NET 版本? .NET Framework 4.5+, .NET Core 3.1+, .NET 5/6/7.
  • 转换是否内存高效? Yes, it streams pages and never loads the whole file into RAM.

什么是 GroupDocs.Conversion for .NET?

GroupDocs.Conversion for .NET 是一个强大的 SDK,能够在超过 100 种文件格式之间进行程序化转换,包括 CAD 图纸、PDF、图像和办公文档。它可处理高达 500 MB 的文件而无需将整个文档加载到内存中,非常适合服务器端批处理任务。

为什么在 CAD 图纸中使用 GroupDocs.Conversion?

GroupDocs.Conversion 提供速度、准确性和可扩展性的组合,使其非常适合处理 CAD 图纸。它能够快速转换复杂的 DGN 文件,同时保留矢量数据,支持批处理,并跨平台运行,确保为工程和建筑工作流提供可靠的结果。

  • 速度: Converts a 300‑page DGN to PNG in under 12 seconds on a typical cloud VM.
  • 准确性: Preserves vector geometry, layers, and raster images with 99.9 % fidelity.
  • 可扩展性: Supports asynchronous and parallel processing, allowing you to handle thousands of files per day.
  • 跨平台: Works on Windows, Linux, and macOS with .NET Core.

前置条件

  • 必需的库: GroupDocs.Conversion for .NET (install via NuGet).
  • 开发环境: Visual Studio 2022 or any IDE that supports .NET 6+.
  • 基本 C# 知识: Familiarity with namespaces, classes, and async patterns.

如何安装 GroupDocs.Conversion?

Installing the SDK is straightforward with NuGet. The package includes all necessary binaries and dependencies, allowing you to start converting files immediately after adding it to your project. You can choose between the Package Manager Console or the .NET CLI, both of which fetch the latest stable version and integrate it into your build pipeline.

包管理器控制台

Install-Package GroupDocs.Conversion -Version 25.3.0

.NET CLI

dotnet add package GroupDocs.Conversion --version 25.3.0

After the package is added, obtain a trial or full license from the GroupDocs website (purchase page) or request a temporary evaluation license (temporary license) and add it to your application configuration.

如何将 dgn 转换为 png?

Load your DGN file with a Converter instance, configure PNG options, and invoke the Convert method. The API streams each page to a MemoryStream, which you then write to disk or return to a client. This approach ensures low memory consumption even for large drawings.

如何在 .NET 项目中设置 GroupDocs.Conversion?

The setup involves adding your license key and creating a Converter instance that points to the source file. This prepares the SDK to perform conversions and ensures that all operations respect your licensing terms.
The Converter class is the core component that manages file loading and transformation within GroupDocs.Conversion.

using GroupDocs.Conversion;

// Initialize a converter object with the path to your DGN file
string dgnFilePath = "YOUR_DOCUMENT_DIRECTORY/sample.dgn";
Converter converter = new Converter(dgnFilePath);

如何加载 DGN 文件进行转换?

Loading a DGN file is done by constructing a Converter with the file path. This step validates the file and readies it for subsequent conversion operations.
The Converter class handles opening the source document and exposing its pages for processing.

string dgnFilePath = "YOUR_DOCUMENT_DIRECTORY/sample.dgn";

// Load the DGN file using GroupDocs.Conversion's Converter class
Converter converter = new Converter(dgnFilePath);

如何配置 PNG 转换选项?

PNG conversion settings are defined through the ImageConvertOptions object, which lets you specify output format, resolution, and visual properties. Adjusting these options controls the quality and size of the resulting images.
The ImageConvertOptions class encapsulates all configurable parameters for image output, such as DPI, background color, and page dimensions.

using GroupDocs.Conversion.Options.Convert;

// Initialize image conversion options with desired output format
ImageConvertOptions options = new ImageConvertOptions
{
    Format = GroupDocs.Conversion.FileTypes.ImageFileType.Png
};

如何执行转换并保存 PNG 文件?

The conversion is triggered by calling the Convert method on the Converter, passing the options and a delegate that creates a stream for each page. This method processes the document page by page and writes the PNG data to the provided streams.
The Convert method performs the actual transformation from the source format to the target format using the specified options.

using System.IO;

string outputFolder = "YOUR_OUTPUT_DIRECTORY";
string outputFileTemplate = Path.Combine(outputFolder, "converted-page-{0}.png");

// Define a method to create file streams for each page being converted
Func<SavePageContext, Stream> getPageStream = savePageContext =>
    new FileStream(string.Format(outputFileTemplate, savePageContext.Page), FileMode.Create);

// Perform the conversion from DGN to PNG using the Converter object and options defined earlier
converter.Convert(getPageStream, options);

实际使用案例

  1. 建筑事务所 share design snapshots with clients who don’t have CAD software.
  2. 施工经理 embed PNG previews into project management tools for quick visual checks.
  3. 营销团队 extract high‑resolution images for brochures and online portfolios without exposing the original CAD source.

性能提示

  • Dispose of the Converter object as soon as you finish to release unmanaged resources.
  • Prefer asynchronous conversion (ConvertAsync) when processing many files in a web API to keep the request thread free.
  • Monitor CPU and memory usage; for files larger than 200 MB, consider processing pages in batches.

常见问题

Q: What other formats can GroupDocs.Conversion handle besides DGN and PNG?
A: It supports over 100 formats, including PDF, DOCX, XLSX, PPTX, SVG, JPEG, BMP, and many CAD formats such as DWG and DXF.

Q: How do I handle password‑protected DGN files?
A: Pass the password to the Converter constructor via the LoadOptions parameter; the SDK will decrypt the file before conversion.

Q: Can I run the conversion in a Linux container?
A: Yes, GroupDocs.Conversion for .NET is fully compatible with .NET Core on Linux, and you can use Docker to containerize the service.

Q: Is there a limit to the number of pages I can convert in one request?
A: There’s no hard limit, but for very large drawings (500 + pages) it’s advisable to process pages in chunks to avoid long‑running requests.

Q: Where can I get a temporary license for evaluation?
A: Visit the GroupDocs website and request a trial license; it’s valid for 30 days and enables all conversion features.


最后更新: 2026-06-25
测试环境: GroupDocs.Conversion 25.3.0 for .NET
作者: GroupDocs

相关教程