【发布时间】:2018-04-16 23:48:45
【问题描述】:
我正在尝试将图像添加到 CAD 绘图中,但只是得到一个显示路径的矩形,而没有显示图像。
我可以不将图像添加到图纸空间或仅将特定类型的图像添加到图纸空间(在我的情况下添加 .jpg 类型)。这是我的代码。
private void AddLogoImage(Transaction trans, BlockTableRecord titleRecord)
{
// Define the name and image to use
string imageName = "Comp Logo";
string fileName = @"C:\Autodesk\Image\comp_logo.jpg";
RasterImageDef imageDef;
ObjectId imageDefId;
// Get the image dictionary
ObjectId imageDictId = RasterImageDef.GetImageDictionary(titleRecord.Database);
// Check to see if the dictionary does not exist, it not then create it
if (imageDictId.IsNull)
{
imageDictId = RasterImageDef.CreateImageDictionary(titleRecord.Database);
}
// Open the image dictionary
DBDictionary imageDict = trans.GetObject(imageDictId, OpenMode.ForRead) as DBDictionary;
// Check to see if the image definition already exists
if (imageDict.Contains(imageName))
{
imageDefId = imageDict.GetAt(imageName);
imageDef = trans.GetObject(imageDefId, OpenMode.ForWrite) as RasterImageDef;
}
else
{
// Create a raster image definition
RasterImageDef newImageDef = new RasterImageDef();
// Set the source for the image file
newImageDef.SourceFileName = fileName;
// Load the image into memory
newImageDef.Load();
// Add the image definition to the dictionary
imageDict.UpgradeOpen();
imageDefId = imageDict.SetAt(imageName, newImageDef);
trans.AddNewlyCreatedDBObject(newImageDef, true);
imageDef = newImageDef;
}
// Create the new image and assign it the image definition
using (RasterImage acRaster = new RasterImage())
{
acRaster.ImageDefId = imageDefId;
Vector3d width;
Vector3d height;
// Check to see if the measurement is set to English (Imperial) or Metric units
if (titleRecord.Database.Measurement == MeasurementValue.English)
{
width = new Vector3d((imageDef.ResolutionMMPerPixel.X * acRaster.ImageWidth) / 25.4, 0, 0);
height = new Vector3d(0, (imageDef.ResolutionMMPerPixel.Y * acRaster.ImageHeight) / 25.4, 0);
}
else
{
width = new Vector3d(imageDef.ResolutionMMPerPixel.X * acRaster.ImageWidth, 0, 0);
height = new Vector3d(0, imageDef.ResolutionMMPerPixel.Y * acRaster.ImageHeight, 0);
}
// Define the position for the image
Point3d insPt = new Point3d(720, 129, 0.0);
// Define and assign a coordinate system for the image's orientation
CoordinateSystem3d coordinateSystem = new CoordinateSystem3d(insPt, width * 2, height * 2);
acRaster.Orientation = coordinateSystem;
// Set the rotation angle for the image
acRaster.Rotation = 0;
// Add the new object to the block table record and the transaction
titleRecord.AppendEntity(acRaster);
trans.AddNewlyCreatedDBObject(acRaster, true);
// Connect the raster definition and image together so the definition
// does not appear as "unreferenced" in the External References palette.
RasterImage.EnableReactors(true);
acRaster.AssociateRasterDef(imageDef);
imageDef.Dispose();
}
}
我应该不使用 Rastor 图像吗?如果不是,是否有替代方法?
在来自 dwg 文件的 PDF 中正确显示,尽管更喜欢没有矩形边框。
【问题讨论】: