Autodesk.AutoCAD.Windows.Data.CMLContentSearchPreviews.GetBlockTRThumbnail method generates a preview image for any BlockTableRecord.
If you wan't to select an external document:
[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;
}
}