How to Convert DWG to JPG in C#

In this elaborative example, we’ll see how to convert DWG to JPG in C#. The Autocad export to JPG using C# can be easily done by using Aspose.CAD for .NET API. In this tutorial, we will learn how to export Autocad DWG to JPG using C#. By using Aspose.CAD in C#, save Autocad as Jpeg in both multiple colors as well as single colored form.

Steps to Export DWG to JPG in C#

  1. Get Aspose.CAD for .NET package from NuGet.org
  2. Include Aspose.CAD and Aspose.CAD.FileFormat namespaces in your code
  3. Load Aspose.CAD license using SetLicense method
  4. Load source DWG for export to JPEG using Image class
  5. Set rasterization options for desired JPEG file
  6. Save the DWG to JPEG exported image on disc

The DWG file format for CAD (Computer Aided Design) drawings contain 2D and 3D design data in binary format data. It comprise of vector images and metadata for holding contents of CAD files. This example is about DWG to JPEG using C# but it can be used for DXF or DGN file formats as well.

Code for Autocad export to JPG using C#

using System;
using Aspose.CAD;
using Aspose.CAD.FileFormats.Jpeg;
namespace DWGToJPEG
{
class ExportDWGToJpg
{
public static void Main(string[] args)
{
// Initialize license object
License license = new License();
// Set license by providing path to license file
license.SetLicense("Aspose.Cad.lic");
//Load the DWG to export to JPEG
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(""))
{
// Create an instance of CadRasterizationOptions
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions =
new Aspose.CAD.ImageOptions.CadRasterizationOptions();
// Set page width & height
rasterizationOptions.PageWidth = 1200;
rasterizationOptions.PageHeight = 1200;
//Set background color and object colors
rasterizationOptions.BackgroundColor = Aspose.CAD.Color.White;
rasterizationOptions.DrawType = Aspose.CAD.FileFormats.Cad.CadDrawTypeMode.UseObjectColor;
// Create an instance of JpegOption for the converted Jpeg image
ImageOptionsBase options = new Aspose.CAD.ImageOptions.JpegOptions();
// Set rasterization options for exporting to JPEG
options.VectorRasterizationOptions = rasterizationOptions;
// Save DWG to JPEG image
image.Save("Exported_image_out.jpeg", options);
}
}
}
}

In our previous topic, we demonstrated, How to Extract Text from DXF file in C#. In afore mentioned topic, we have used C# code sample to export DWG to JPEG. In this whole process to save Autocad file to JPG, you do not need to install Autocad on your system and the API works seamlessly in any form of C# application including Web, Desktop, and ASP.NET etc.

 English