这是获取GetBlockImage接口的替代方案,可以测试一下能不能满足需求
[CommandMethod("GetBlkImg")]
public void GetBlkImg4()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
var dwgPath = (string)Application.GetSystemVariable("DWGPREFIX");
var promptRes = ed.GetInteger("\nOutput image height:");
var nImgHeight = promptRes.Value;
promptRes = ed.GetInteger("\nOutput image width:");
var nImgWidth = promptRes.Value;
using (var tr = doc.TransactionManager.StartTransaction())
{
var blkTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
foreach (var blkId in blkTbl)
{
var blkTblRec = tr.GetObject(blkId, OpenMode.ForRead) as BlockTableRecord;
if (blkTblRec == null || blkTblRec.IsLayout || blkTblRec.IsAnonymous) continue;
var blkName = blkTblRec.Name;
var backgroundColor = new ZwSoft.ZwCAD.Colors.Color();
backgroundColor.SetRgb(0, 0, 0);
Bitmap bitmap = GetBlockImage(blkId, nImgWidth, nImgHeight, backgroundColor);
if (bitmap != null)
{
var savePath = dwgPath + blkName + ".png";
if (File.Exists(savePath))
{
File.Delete(savePath);
}
bitmap.Save(savePath);
bitmap.Dispose();
ed.WriteMessage("Save block \"" + blkName + "\" to path : " + savePath);
}
else
{
ed.WriteMessage("block icon didn't generate.\n");
}
}
}
}
private bool SetView(View m_View, ObjectId id, double nImgWidth, double nImgHeight)
{
var doc = Application.DocumentManager.MdiActiveDocument;
using (var tr = doc.TransactionManager.StartTransaction())
{
try
{
var blkTblRec = tr.GetObject(id, OpenMode.ForRead) as BlockTableRecord;
if (blkTblRec.IsLayout || blkTblRec.IsAnonymous)
{
doc.Editor.WriteMessage("id not a block!");
tr.Commit();
return false;
}
Extents3d ext = new Extents3d();
var ents = blkTblRec.GetEnumerator();
while (ents.MoveNext())
{
var ent = tr.GetObject(ents.Current, OpenMode.ForRead) as Entity;
ext.AddExtents(ent.GeometricExtents);
}
ents.Dispose();
var minPt = ext.MinPoint;
var maxPt = ext.MaxPoint;
var center = minPt + 0.5 * (maxPt - minPt);
m_View.EraseAll();
m_View.SetView(center + Vector3d.ZAxis, center, Vector3d.YAxis, nImgWidth, nImgHeight);
m_View.ZoomExtents(minPt, maxPt);
m_View.Zoom(0.9);
tr.Commit();
}
catch (ZwSoft.ZwCAD.Runtime.Exception ex)
{
doc.Editor.WriteMessage(ex.Message);
return false;
}
}
return true;
}
private void Display(View m_View, Model m_Model, ObjectId id)
{
var doc = Application.DocumentManager.MdiActiveDocument;
using (var tr = doc.TransactionManager.StartTransaction())
{
var blkTblRec = tr.GetObject(id, OpenMode.ForRead) as BlockTableRecord;
m_View.Add(blkTblRec, m_Model);
m_View.Invalidate();
m_View.Update();
tr.Commit();
}
}
public Bitmap GetBlockImage(ObjectId id, double nImgWidth, double nImgHeight, ZwSoft.ZwCAD.Colors.Color backgroundColor)
{
Bitmap bmp = null;
var doc = Application.DocumentManager.MdiActiveDocument;
var m_Manager = doc.GraphicsManager;
var gk = new GraphicsKernel();
var m_Device = m_Manager.CreateZWCADOffScreenDevice(gk);
var m_Model = m_Manager.CreateZWCADModel(gk);
var m_View = new View();
if (m_View == null)
{
doc.Editor.WriteMessage("\nView create error!\n");
goto _FIN;
}
m_Device.BackgroundColor = backgroundColor.ColorValue;
m_Device.OnSize(new Size(Convert.ToInt32(nImgWidth), Convert.ToInt32(nImgHeight)));
m_Device.Add(m_View);
if (!SetView(m_View, id, nImgWidth, nImgHeight))
{
goto _FIN;
}
Display(m_View, m_Model, id);
bmp = m_Device.GetSnapshot(m_View.Viewport);
if (bmp == null)
{
doc.Editor.WriteMessage("\nGetSnapshot error!\n");
}
_FIN:
if (m_Device != null) m_Device.Erase(m_View);
if (m_View != null)
{
m_View.EraseAll();
m_View.Dispose();
m_View = null;
}
if (m_Manager != null)
{
if (m_Model != null)
{
m_Model.Dispose();
m_Model = null;
}
if (m_Device != null)
{
m_Device.Dispose();
m_Device = null;
}
}
return bmp;
}
没有成功,还是遇到了错误。
遇到的错误是什么,可以具体描述一下
您给的例子似乎只能用于ZCAD(虽然我确实没有测试成功),我希望能够ACAD也可以用。 我找到了一篇博客,可以成功生成清晰图片(但是带属性的块显示有问题)。 (不知为何网址贴不上来。)
/swtool/p/3764774.html
cnblogs
我这里测试zwcad2024和2025都是可以正常输出图片的,不知道你用的是什么版本。如果ACAD要用,你也需要将代码中的ZWCAD变量改为Autocad的变量才能运行。
十分感谢,抽空我测试一下。