|
- 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();
- }
复制代码 |
|