How to get DWG or DXF drawing width and height?

        private void dataGridView1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

                foreach (string file in files)
                {
                    string outputPngPath = Path.Combine(Path.GetDirectoryName(file), "output.png");

                    using (var viewer = new Viewer(file))
                    {
                        PngViewOptions viewOptions = new PngViewOptions();

                        viewOptions.CadOptions.BackgroundColor = Color.FromArgb(224, 224, 224);
                        viewer.View(viewOptions);

                        // Dosya adını ve uzantısını al
                        string fileName = Path.GetFileName(file);
                        string extension = Path.GetExtension(file);

                        // Sadece dwg ve dxf dosyalarını işle
                        if (extension == ".dwg" || extension == ".dxf")
                        {
                            // Dosya adındaki adet ve mm değerlerini ayrıştır
                            string[] parts = fileName.Split('-', '.', '_', '*');
                            string mmValue = "";
                            string adetValue = "";

                            foreach (string part in parts)
                            {
                                if (part.ToUpper().Contains("MM"))
                                {
                                    mmValue = part.Replace("MM", "").Trim();
                                }
                                else if (part.ToUpper().Contains("ADET"))
                                {
                                    adetValue = part.Replace("ADET", "").Trim();
                                }
                            }

                            using (PictureBox pictureBox = new PictureBox())
                            {
                                pictureBox.BackgroundImage = Image.FromFile(outputPngPath);
                                Image resizedImage = ResizeImage(pictureBox.BackgroundImage, 150, 50);

                                // DataGridView'e yeni satır ekle
                                dataGridView1.Rows.Add(fileName, extension, mmValue, "", "", adetValue, "", resizedImage);
                            }
                        }
                        viewOptions.WordProcessingOptions.RenderTrackedChanges = true;
                    }


                }
            }


        }

        private void dataGridView1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
        }

Hello, I’m developing a program using C# Windows Forms. What I want to do is to drag and drop only .dwg and .dxf files into a DataGridView and display the corresponding file’s images in each row. I have accomplished everything so far, but I have a few issues. I’m unable to change the drawing color of the image added to the PictureBox, and I would like to retrieve the actual dimensions (maximum width and height) of the files I upload. Thank you in advance for your support.

Ekran görüntüsü 2023-05-12 121440.png (66.4 KB)

@nestougur

You’re already changing the background color with viewOptions.CadOptions.BackgroundColor. Please clarify if this setting does not work as expected or if you’re trying to do something else.

You can use GetViewInfo method to get the image size

using (var viewer = new Viewer("house-plan.dwg"))
{
    var viewInfoOptions = ViewInfoOptions.FromPngViewOptions(viewOptions); 
    var viewInfo = viewer.GetViewInfo(viewInfoOptions);

    foreach (var page in viewInfo.Pages)
    {
        Console.WriteLine($"Page {page.Number}, width: {page.Width}, height: {page.Height}");
    }
}
1 Like

ss.png (11.4 KB)
Uploading: ss.png…

Sir, in the sample image, we changed the drawing color to purple in Inventor and added the actual dimensions of the drawing. What I want is to simply change the drawing color from gray to purple and write the dimensions in the DataGridView.

@nestougur

Thank you for sharing the details. We’ll take a look and update you.

1 Like

I am eagerly awaiting the solution to the problem. I sincerely thank you for your efforts in advance and wish you a productive work. :clap:

@nestougur

Sure, as soon as we have any new information we’ll let you know.

1 Like

Hello again, have you been able to find a solution to the problem?

@nestougur

Your scenario is still under investigation. Your investigation ticket ID is VIEWERNET-4364. We’ll notify you in case of any update.

1 Like

Hi. I’m processing your issue now and some missunderstood your problem. If problem still actual, can you share your WinForms application with explanations what need to do.

You were answered earlier: How to get DWG or DXF drawing width and height? - #2 by vladimir.litvinchik

You can use page.Width and page page.Height for:

dimensions in the DataGridView.