The AcDbRasterImageDef::setImage can be used, if you do not want to include the image file path while inserting a raster image. This can be considered another approach to accomplish what is explained in this blog post. The AcDbRasterImageDef::setImage method requires a pointer to an ATIL image. In this sample code, the image buffer is read from the image and is used to create an ATIL image. The ATIL image pointer is used with the AcDbRasterImageDef::setImage to create a raster image definition. To build this sample code, you will need the include and library files from the ObjectARX SDK folder under "\utils\Atil".
static void AdskInsertImage(void)
{
// Image path to use for the RasterImageDef
AcString imagePath(ACRX_T("C:\\Temp\\Test.png"));
// Load the image to get access to its image buffer
AcTcImage tc;
tc.Load(imagePath);
HBITMAP bmp=0;
tc.GetHBITMAP(RGB(0xff,0xff,0xff),bmp);
if (bmp == NULL)
return;
BITMAP _bmp={0};
GetObject(bmp,sizeof BITMAP,&_bmp);
HDC hdcScr=GetDC(NULL);
HDC hdcMem=CreateCompatibleDC(hdcScr);
SelectObject(hdcMem,bmp);
// Create an Atil::Image.
// The AcDbRasterImageDef::setImage requires it
Atil::ImagePixel initialImage;
initialImage.setToZero();
initialImage.type = Atil::DataModelAttributes::kRgba;
initialImage.value.rgba = 0xff000000;
Atil::Size size(_bmp.bmWidth, _bmp.bmHeight);
const Atil::RgbModel *pDm = new Atil::RgbModel(
Atil::RgbModelAttributes::k4Channels,
Atil::DataModelAttributes::kBlueGreenRedAlpha);
Atil::Image *pAtilImage = new Atil::Image(
size, pDm, initialImage);
// Write the image data on to the Atil image
// using an Image Context
Atil::Offset upperLeft(0,0);
Atil::ImageContext *pImgContext
= pAtilImage->createContext(
Atil::ImageContext::kWrite,
size,
upperLeft
);
if (pImgContext != NULL)
{
for (int xf=0;xf<_bmp.bmWidth;xf++)
{
for (int yf=0;yf<_bmp.bmHeight;yf++)
{
BYTE alpha=0x0;
COLORREF pix=GetPixel(hdcMem,xf,yf);
BYTE rr = (pix&0xff);
BYTE gg = (pix>>8)&0xff;
BYTE bb = (pix>>16)&0xff;
// Alpha channel to account for transparency
if ((rr!=0xff) || (gg!=0xff) || (bb!=0xff))
alpha=0xff;
Atil::RgbColor p;
p.set(rr, gg, bb, alpha);
pImgContext->put32(xf, yf, p);
}
}
}
pImgContext->flush();
delete pImgContext;
bool isImageValid = pAtilImage->isValid();
ASSERT(isImageValid);
// Create a RasterImageDef and set the image
// from the Atil image
AcDbRasterImageDef *pImageDef = new AcDbRasterImageDef();
Acad::ErrorStatus es = pImageDef->setImage(
pAtilImage, NULL);
// Insert the RasterImageDef and create a RasterImage
// using it
es = InsertImageInDwg(pImageDef);
if(es != Acad::eOk)
{
delete pImageDef;
return;
}
// Cleanup
DeleteDC(hdcMem);
ReleaseDC(NULL,hdcScr);
DeleteObject( bmp);
}
static Acad::ErrorStatus InsertImageInDwg(
AcDbRasterImageDef *pImageDef)
{
Acad::ErrorStatus es;
if(! pImageDef->isLoaded())
{
es = pImageDef->load();
if(es != Acad::eOk)
return es;
}
AcApDocument *pActiveDoc = acDocManager->mdiActiveDocument();
AcDbDatabase *pDB = pActiveDoc->database();
// Get the image dictionary
// Create it if not available already
AcDbObjectId dictID
= AcDbRasterImageDef::imageDictionary(pDB);
if(dictID == AcDbObjectId::kNull)
{
es = AcDbRasterImageDef::createImageDictionary(
pDB, dictID);
if(es != Acad::eOk)
return es;
}
AcDbDictionary* pDict;
es = acdbOpenObject(pDict, dictID, AcDb::kForWrite);
if(es != Acad::eOk)
return es;
ACHAR *DICT_NAME = ACRX_T("ISM_RASTER_IMAGE_DICT_VIEW");
BOOL bExist = pDict->has(DICT_NAME);
AcDbObjectId objID = AcDbObjectId::kNull;
if (!bExist)
{
es = pDict->setAt(DICT_NAME, pImageDef, objID);
if(es != Acad::eOk)
return es;
}
else
{
pDict->getAt(DICT_NAME,
(AcDbObject*&)pImageDef,
AcDb::kForWrite);
objID = pImageDef->objectId();
}
// close Dictionary and Definition.
pDict->close();
pImageDef->close();
// Create a raster image using the RasterImage Def
AcDbRasterImage* pImage = new AcDbRasterImage;
es = pImage->setImageDefId(objID);
if (es != Acad::eOk)
{
delete pImage;
return es;
}
// Add the raster image to the model space
AcDbBlockTable* pBlockTable;
AcDbBlockTableRecord* pBTRecord;
es = acdbCurDwg()->getBlockTable(pBlockTable,
AcDb::kForRead);
assert(es == Acad::eOk);
es = pBlockTable->getAt(ACDB_MODEL_SPACE,
pBTRecord,
AcDb::kForWrite);
assert(es == Acad::eOk);
es = pBTRecord->appendAcDbEntity(pImage);
assert(es == Acad::eOk);
pBTRecord->close();
pBlockTable->close();
AcDbObjectId entID = pImage->objectId();
// Set the transparency options
pImage->setDisplayOpt( AcDbRasterImage::kTransparent,
Adesk::kTrue);
pImage->setImageTransparency(true);
pImage->setDisplayOpt(AcDbRasterImage::kShow, Adesk::kTrue);
AcDbObjectPointer<AcDbRasterImageDefReactor>
rasterImageDefReactor;
rasterImageDefReactor.create();
es = rasterImageDefReactor->setOwnerId(pImage->objectId());
if (es == Acad::eOk)
{
AcDbObjectId defReactorId;
es = curDoc()->database()->addAcDbObject(
defReactorId,
rasterImageDefReactor.object());
if (es == Acad::eOk)
{
pImage->setReactorId(defReactorId);
AcDbObjectPointer<AcDbRasterImageDef> rasterImagedef
(pImage->imageDefId(), AcDb::kForWrite);
if (rasterImagedef.openStatus() == Acad::eOk)
{
rasterImagedef->addPersistentReactor
(defReactorId);
}
}
}
pImageDef->close();
pImage->close();
}