Author Topic: Need Help in saving thumbnail Image  (Read 1819 times)

0 Members and 1 Guest are viewing this topic.

wilbur

  • Guest
Need Help in saving thumbnail Image
« on: October 08, 2016, 05:46:40 AM »
Hi,
    Now I am facing a problem,I use database.SaveAs function to save a dwg file,but the dwg file do not have an thumbnail image,so I trid to search kean's blog ,and I write code like below,but I can't get what I want.
Code: [Select]
[CommandMethod("SendSaveAsCmd", CommandFlags.Session)]
        public static void DoSaveAs()
        {
            string filepath = @"C:\Users\FL_DRAWING\Desktop\Images\1.dwg";
            string targetpath = @"C:\Users\FL_DRAWING\Desktop\Images\newdwg.dwg";

            Document doc = Application.DocumentManager.Open(filepath, false);
            doc.LockDocument();
            Database db = doc.Database;
            object ocmd = Application.GetSystemVariable("CMDECHO");
            string newpath = targetpath.Replace("\\", "/");

            string cmd =
                "(setvar \"CMDECHO\" 0)" +
                "(command \"_.SAVEAS\" \"\" \"" + newpath + "\")" +
                "(setvar \"CMDECHO\" " + ocmd.ToString() + ")" +
                "(princ) ";
            doc.SendStringToExecute(cmd,false,false,false);

            doc.CloseAndDiscard();
        }

And I tried Another way like below

Code: [Select]
[CommandMethod("SaveThumbnail", CommandFlags.Session)]
        public static void DoSaveThumbnail()
        {
            string filepath = @"C:\Users\FL_DRAWING\Desktop\Images\1.dwg";
            string targetpath = @"C:\Users\FL_DRAWING\Desktop\Images\newdwg.dwg";

            Document doc = Application.DocumentManager.Open(filepath);
            doc.LockDocument();
            Database db = doc.Database;
            db.RetainOriginalThumbnailBitmap = true;
            Bitmap thumb = db.ThumbnailBitmap;
            db.ThumbnailBitmap = thumb;
            db.SaveAs(targetpath, DwgVersion.Current);
            doc.CloseAndDiscard();
        }
But the thumb is null in line Bitmap thumb = db.ThumbnailBitmap
In one of kean's blog ,I saw Document.CapturePreviewImage
So I rewrite my code like below
Code: [Select]
[CommandMethod("SaveThumbnail", CommandFlags.Session)]
        public static void DoSaveThumbnail()
        {
            string filepath = @"C:\Users\FL_DRAWING\Desktop\Images\1.dwg";
            string targetpath = @"C:\Users\FL_DRAWING\Desktop\Images\newdwg.dwg";

            Document doc = Application.DocumentManager.Open(filepath);
            doc.LockDocument();
            Database db = doc.Database;
            Bitmap thumb = doc.CapturePreviewImage(320, 240);
            db.ThumbnailBitmap = thumb;
            db.SaveAs(targetpath, DwgVersion.Current);
            doc.CloseAndDiscard();
        }

And I get a thumbnail image with a black background, but the thumbnail could not fullfill the thumbnail in Windows document viewer.And I also could not see clear in Windows Document viewer.
 
Any help will be great, thank you.
« Last Edit: October 08, 2016, 05:50:30 AM by wilbur »