Community
I'm trying to copy some pixel from one image to a new one. the code executes fine but I can't seem to save it to disk, it gives me an AccessViolationException and autocad carshes.
here is my code :
int Functions::Crop(void* img, int rectangle[]) { //read image Atil::Image* image = (Atil::Image*)img; //create empty image to hold the pixels Atil::ImagePixel initialImage; initialImage.setToZero(); initialImage.type = image->dataModel().pixelType();//Atil::DataModelAttributes::kRgba; initialImage.value.rgba = 0xff000000; Atil::Size size(rectangle[2], rectangle[3]); /*const Atil::RgbModel *pDm = new Atil::RgbModel( Atil::RgbModelAttributes::k4Channels, Atil::DataModelAttributes::kBlueGreenRedAlpha);*/ Atil::Image* cropped = new Atil::Image(size, &image->dataModel(), initialImage); //copy pixels Atil::Offset upperLeft1(rectangle[0],rectangle[1]); Atil::ImageContext *pImgContext1 = image->createContext( Atil::ImageContext::kRead, size, upperLeft1 ); Atil::Offset upperLeft(0,0); Atil::ImageContext *pImgContext2 = cropped->createContext( Atil::ImageContext::kWrite, size, upperLeft ); int row=0,col = 0; for (row=upperLeft1.y;row< size.height+upperLeft1.y;++row) { for (col=upperLeft1.x;col<size.width+upperLeft1.x;++col) { try { Atil::ImagePixel pixel = pImgContext1->getPixel(col,row); pImgContext2->putPixel(col-upperLeft1.x,row-upperLeft1.y,pixel); } catch(Atil::ATILException* e) { delete e; return 1; } } } //save image try { Atil::RowProviderInterface* pPipe = cropped->read(cropped->size(), Atil::Offset(0,0)); Atil::ImageFormatCodec *pCodec = NULL; pCodec = new JfifFormatCodec(); Atil::FileWriteDescriptor *pFileWriteDescriptor = new Atil::FileWriteDescriptor(pCodec); wchar_t pFileName[1024] = L"C:\\img.jpg"; Atil::FileSpecifier fs(Atil::StringBuffer(lstrlen(pFileName) + 1, (const Atil::Byte *) pFileName, Atil::StringBuffer::kASCII), Atil::FileSpecifier::kFilePath); pFileWriteDescriptor->setFileSpecifier(fs); pFileWriteDescriptor->createImageFrame(pPipe->dataModel(), pPipe->size()); pFileWriteDescriptor->writeImageFrame(pPipe); delete pFileWriteDescriptor; delete pPipe; delete pCodec; } catch (Atil::ATILException* e) { delete e; return 0; } //clean up delete pImgContext1; delete pImgContext2; delete cropped; return 1; }
can somebody please points me to what I'm doing wrong.
Hi Youssef,
Sorry for the delay.
Have you tried the code provided in this blog post to write the image to a file ?
Look for the "writeImageFile" method in this blog post.
http://adndevblog.typepad.com/autocad/2013/01/capturing-a-screen-shot-using-objectarx.html
Regards,
Balaji