Author Topic: How to get dwg preview in winform picturebox...c#  (Read 5414 times)

0 Members and 1 Guest are viewing this topic.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
How to get dwg preview in winform picturebox...c#
« on: January 21, 2017, 05:10:05 PM »
Anyone have an example they are willing to share that will show a thimbnail or preview of a drawing in a picturebox?

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: How to get dwg preview in winform picturebox...c#
« Reply #1 on: January 21, 2017, 06:58:50 PM »
I created a form, added a picturebox having the size 176,120 and a modifier of Internal. Then this code for a command:

Code - C#: [Select]
  1.         [CommandMethod("PictureTest")]
  2.         public static void testme()
  3.         {
  4.             var doc = Application.DocumentManager.MdiActiveDocument;
  5.             var img = doc.CapturePreviewImage(176, 120);
  6.             var form = new PictureBoxTest();
  7.             form.pictureBox1.Image = img;
  8.             Application.ShowModalDialog(form);
  9.         }
  10.  

ChavesGalinari

  • Guest
Re: How to get dwg preview in winform picturebox...c#
« Reply #2 on: January 23, 2017, 08:01:11 PM »
Autodesk.AutoCAD.Windows.Data.CMLContentSearchPreviews.GetBlockTRThumbnail method generates a preview image for any BlockTableRecord.

If you wan't to select an external document:

Code: [Select]
        [CommandMethod("GetOuterDWGModelBitmap")]
        public static void GetOuterDWGModelBitmap()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.DefaultExt = "*.dwg";
            var dr = ofd.ShowDialog();

            if (dr != DialogResult.OK) return;

            string file = dr.ToString();

            ObjectIdCollection ids = new ObjectIdCollection();
            using (Database OuterDB = new Database())
            {
                OuterDB.ReadDwgFile(ofd.FileName, System.IO.FileShare.Read, false, "");
                using (Transaction tr = OuterDB.TransactionManager.StartTransaction())
                {
                    BlockTable bt;
                    bt = (BlockTable)tr.GetObject(OuterDB.BlockTableId
                                                   , OpenMode.ForRead);

                    BlockTableRecord blk = (BlockTableRecord)tr.GetObject(bt["*Model_Space"], OpenMode.ForRead);

                    var imgsrc = Autodesk.AutoCAD.Windows.Data.CMLContentSearchPreviews.GetBlockTRThumbnail(blk);
                    var bmp = ImageSourceToGDI(imgsrc as System.Windows.Media.Imaging.BitmapSource);

                    var image = new ImageForm(bmp as System.Drawing.Bitmap);
                    image.ShowDialog();

                    tr.Commit();
                }
            }
        }
        private static System.Drawing.Image ImageSourceToGDI(System.Windows.Media.Imaging.BitmapSource src)
        {
            var ms = new MemoryStream();
            var encoder =
              new System.Windows.Media.Imaging.BmpBitmapEncoder();
            encoder.Frames.Add(
              System.Windows.Media.Imaging.BitmapFrame.Create(src)
            );
            encoder.Save(ms);
            ms.Flush();
            return System.Drawing.Image.FromStream(ms);
        }
        private class ImageForm : Form
        {
            public ImageForm(Bitmap ToShow)
            {
                this.Height = 800;
                this.Width = 800;
                var pictureBox1 = new PictureBox() { Location = new System.Drawing.Point(10, 10), Height = 700, Width = 780 };
                var buttonOK = new Button() { Text = "Ok", DialogResult = DialogResult.OK, Width = 30, Location = new System.Drawing.Point(730, 730) };
                this.Controls.Add(pictureBox1);
                this.Controls.Add(buttonOK);

                pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
                pictureBox1.BackgroundImage = ToShow;
            }
        }

Jeff H

  • Needs a day job
  • Posts: 6150
Re: How to get dwg preview in winform picturebox...c#
« Reply #3 on: January 23, 2017, 08:03:13 PM »
Thanks Chaves,
And welcome to the Swamp!

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: How to get dwg preview in winform picturebox...c#
« Reply #4 on: January 24, 2017, 05:12:22 PM »
perfect...thank you

Autodesk.AutoCAD.Windows.Data.CMLContentSearchPreviews.GetBlockTRThumbnail method generates a preview image for any BlockTableRecord.

If you wan't to select an external document:

Code: [Select]
        [CommandMethod("GetOuterDWGModelBitmap")]
        public static void GetOuterDWGModelBitmap()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.DefaultExt = "*.dwg";
            var dr = ofd.ShowDialog();

            if (dr != DialogResult.OK) return;

            string file = dr.ToString();

            ObjectIdCollection ids = new ObjectIdCollection();
            using (Database OuterDB = new Database())
            {
                OuterDB.ReadDwgFile(ofd.FileName, System.IO.FileShare.Read, false, "");
                using (Transaction tr = OuterDB.TransactionManager.StartTransaction())
                {
                    BlockTable bt;
                    bt = (BlockTable)tr.GetObject(OuterDB.BlockTableId
                                                   , OpenMode.ForRead);

                    BlockTableRecord blk = (BlockTableRecord)tr.GetObject(bt["*Model_Space"], OpenMode.ForRead);

                    var imgsrc = Autodesk.AutoCAD.Windows.Data.CMLContentSearchPreviews.GetBlockTRThumbnail(blk);
                    var bmp = ImageSourceToGDI(imgsrc as System.Windows.Media.Imaging.BitmapSource);

                    var image = new ImageForm(bmp as System.Drawing.Bitmap);
                    image.ShowDialog();

                    tr.Commit();
                }
            }
        }
        private static System.Drawing.Image ImageSourceToGDI(System.Windows.Media.Imaging.BitmapSource src)
        {
            var ms = new MemoryStream();
            var encoder =
              new System.Windows.Media.Imaging.BmpBitmapEncoder();
            encoder.Frames.Add(
              System.Windows.Media.Imaging.BitmapFrame.Create(src)
            );
            encoder.Save(ms);
            ms.Flush();
            return System.Drawing.Image.FromStream(ms);
        }
        private class ImageForm : Form
        {
            public ImageForm(Bitmap ToShow)
            {
                this.Height = 800;
                this.Width = 800;
                var pictureBox1 = new PictureBox() { Location = new System.Drawing.Point(10, 10), Height = 700, Width = 780 };
                var buttonOK = new Button() { Text = "Ok", DialogResult = DialogResult.OK, Width = 30, Location = new System.Drawing.Point(730, 730) };
                this.Controls.Add(pictureBox1);
                this.Controls.Add(buttonOK);

                pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
                pictureBox1.BackgroundImage = ToShow;
            }
        }