How to thumbnail show preview of autocad dwg file ?

dwg thumnail preview in c sharp Select dwg file in local directory and by FileStream ready the autocad file by binaryreader. Save Bitmap data in memorystream and convert to Bitmap as shown in c# code snippet.
This code doesn't required to install autocad program , this will be ideal to preview autocad dwg file before opening it which will save considerable time.

Autocad Dwg Viewer Online

AutoCAD viewer Online, available in this site eCadViewer.com view any CAD files dwg or dxf, support any AutoCAD version from r14 to the latest version. Open any dwg file with Open 2D, 3D, Model View, layout

Download

Example Source Snippets - thumbnailView.zip

namespace : using System.IO

Dwg thumbnail Viewer


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace thumbnailView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog
            {
                InitialDirectory = @"D:\",             
                CheckFileExists = true,
                CheckPathExists = true,

                DefaultExt = "dwg",
                Filter = "dwg files (*.dwg)|*.dwg",
                FilterIndex = 2,
                RestoreDirectory = true,

                ReadOnlyChecked = true,
                ShowReadOnly = true
            };
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }  
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            addDwgAsBmpImage(textBox1.Text);
        }

        private void addDwgAsBmpImage(string name)
        {
            try
            {
                FileStream fs = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader br = new BinaryReader(fs);
                br.BaseStream.Seek(0x0D, SeekOrigin.Begin);
                Int32 imgPos = br.ReadInt32();
                br.BaseStream.Seek(imgPos, SeekOrigin.Begin);
                byte[] imgBSentinel = { 0x1F, 0x25, 0x6D, 0x07, 0xD4, 0x36, 0x28, 0x28, 0x9D, 0x57, 0xCA, 0x3F, 0x9D, 0x44, 0x10, 0x2B };
                byte[] imgCSentinel = new byte[16];
                imgCSentinel = br.ReadBytes(16);

                if (imgBSentinel.ToString() == imgCSentinel.ToString())
                {
                    UInt32 imgSize = br.ReadUInt32();
                    //Get number of images present
                    byte imgPresent = br.ReadByte();
                    // header
                    Int32 imgHeaderStart = 0;
                    Int32 imgHeaderSize = 0;
                    // bmp data
                    Int32 imgBmpStart = 0;
                    Int32 imgBmpSize = 0;
                    bool bmpDataPresent = false;
                    // wmf data
                    Int32 imgWmfStart = 0;
                    Int32 imgWmfSize = 0;
                    //bool wmfDataPresent = false;

                    //get each image present
                    for (int I = 0; I <= imgPresent; I++)
                    {
                        // Get image type
                        byte imgCode = br.ReadByte();
                        switch (imgCode)
                        {
                            case 1:
                                // Header data
                                imgHeaderStart = br.ReadInt32();
                                imgHeaderSize = br.ReadInt32();
                                break;
                            case 2:
                                // bmp data
                                imgBmpStart = br.ReadInt32();
                                imgBmpSize = br.ReadInt32();
                                bmpDataPresent = true;
                                break;
                            case 3:
                                //wmf data
                                imgWmfStart = br.ReadInt32();
                                imgWmfSize = br.ReadInt32();
                                //wmfDataPresent = true;
                                break;
                        }
                    }
                    if (bmpDataPresent)
                    {
                        br.BaseStream.Seek(imgBmpStart, SeekOrigin.Begin);
                        byte[] tempPixelData = new byte[imgBmpSize + 14];

                        // indicate it is a bit map
                        tempPixelData[0] = 0x42;
                        tempPixelData[1] = 0x4D;

                        //offBits
                        tempPixelData[10] = 0x36;
                        tempPixelData[11] = 0x04;

                        byte[] tempBuffData = new byte[imgBmpSize];
                        tempBuffData = br.ReadBytes(imgBmpSize);
                        tempBuffData.CopyTo(tempPixelData, 14);

                        MemoryStream memStream = new MemoryStream(tempPixelData);
                        Bitmap bmp = new Bitmap(memStream);
                        pictureBox1.Image = bmp;
                    } 
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}
       

Parent Comment jackal May 12 2019
autocad comment
Reply  Cancel


smartsnipps.ecomparefiles.com © 2019, All Rights Reserved | Disclaimer: smartsnipps.eCompareFiles.com is free to use any code snippets without guarantee