How to convert cad to jpg or tiff?

Hi, Support:
Please provide me a full demo how to convert each layer of cad as jpg file based on Net.

Thanks for your help.

@ducaisoft

To convert CAD files to JPG or TIFF format using .NET, you can utilize the Aspose.CAD library. Below is a complete example demonstrating how to convert each layer of a CAD file into separate JPG or TIFF files.

Example Code

using Aspose.CAD;
using Aspose.CAD.ImageOptions;
using Aspose.CAD.FileFormats.Tiff.Enums;

class Program
{
    static void Main(string[] args)
    {
        // Specify the path to the CAD file
        string cadFilePath = "path/to/your/file.dxf";
        
        // Load the CAD file
        using (Image image = Image.Load(cadFilePath))
        {
            // Create an instance of CadRasterizationOptions
            CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions
            {
                PageWidth = 1200,
                PageHeight = 1200,
                Layouts = new string[] { "Model", "Layout1" } // Specify the layouts you want to convert
            };

            // Convert to TIFF
            TiffOptions tiffOptions = new TiffOptions(TiffExpectedFormat.Default)
            {
                VectorRasterizationOptions = rasterizationOptions
            };
            image.Save("output_image.tiff", tiffOptions);

            // Convert to JPG (if needed)
            JpegOptions jpegOptions = new JpegOptions
            {
                Quality = 100,
                VectorRasterizationOptions = rasterizationOptions
            };
            image.Save("output_image.jpg", jpegOptions);
        }
    }
}

Explanation

  1. Loading the CAD File: The CAD file is loaded using the Image.Load method.
  2. Setting Rasterization Options: You can specify the page dimensions and the layouts you want to convert using CadRasterizationOptions.
  3. Saving as TIFF: The TiffOptions class is used to save the image in TIFF format, utilizing the rasterization options set earlier.
  4. Saving as JPG: Similarly, you can save the image in JPG format using the JpegOptions class.

Make sure to replace "path/to/your/file.dxf" with the actual path to your CAD file. This code will generate TIFF and JPG files for the specified layouts in the CAD document.

For more details, you can refer to the Aspose.CAD documentation and examples available on their GitHub repository.

@ducaisoft,
please try whether this is useful:

using (CadImage cadImage = (CadImage)Image.Load(fileName))
{
     CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
     rasterizationOptions.PageWidth = 3000;
     rasterizationOptions.PageHeight = 3000;
     rasterizationOptions.DrawType = CadDrawTypeMode.UseObjectColor;

     foreach (string layerName in cadImage.Layers.GetLayersNames())
     {
         rasterizationOptions.Layers = new string[] { layerName };

         TiffOptions tiffOptions = new TiffOptions();
         tiffOptions.VectorRasterizationOptions = rasterizationOptions;

         cadImage.Save(outPath, tiffOptions);
     }
}