I have a problem similar to this one: How to copy a CImage object? I need to make a deep copy of CImage object. The specific problem is that I'm getting a CImage* from a secondary thread through PostMessage, and want to copy it to CImage object which is an attribute of different class.
i currently have following code:
LRESULT CMainFrame::TransApplied(UINT wParam,LONG lParam)
{
DEB("message recieved\n"); //this is debug message to output
CImage *obrazek=(CImage* )wParam;
int a;
CString t;
CImage tymczas;
obrazek->Save(_T("mesydz.jpg"));//checking if the image is valid
DEB("o1\n");
obrazek->GetDC();
PointerToViewClass->przetransformowany.Create(obrazek->GetWidth(),obrazek->GetHeight(),obrazek->GetBPP());
DEB("o2\n");
obrazek->BitBlt(PointerToViewClass->przetransformowany.GetDC(),0,0,SRCCOPY);
// obrazek->ReleaseDC();
DEB("o8\n");
PointerToViewClass->przetransformowany.ReleaseDC();
DEB("o6\n");
DEB("o7\n");
obrazek->ReleaseDC();
//delete obrazek;
return 0;
}
i suspect there is something wrong with obrazek->GetDC() and obrazek->ReleaseDC(). i have no errors when i have obrazek->GetDC() (but otherwise there is debug assertion failed on obrazek->BitBlt(...); ), though I am unable to release obrazek's DC and/or delete the obrazek object, which i would like to do, because i am having memory leaks if i don't do it.
so in general there is something i don't know about copying the object.
EDIT:
the other thread handles the CImage in the following way:
CImage wyniczek;
CImage *doprzeslania;
objekt->ApplyTransform(macierz,&wyniczek);//object is an object of a class declared in dll
doprzeslania=new CImage(wyniczek);
PostMessage(data->parent,MY_WM_MESSAGE4,(WPARAM)doprzeslania,(LPARAM)3);
delete objekt;
FreeLibrary(hDLL);
delete data;//deleting an object with settings for the thread
return 0;
and additionally, what is happening in the dll with the image:
void IProcess::ApplyTransform(std::vector<double> matrix,CImage *rezult)
{
//
//some image processing here
//
output.Create(dest.cols,dest.rows,24);
BITMAPINFO bmi; //it is initialized properly, had erased it for clarity
StretchDIBits(output.GetDC(),0,0,dest.cols,dest.rows,0,0,dest.cols,dest.rows,temp.data,&bmi,DIB_RGB_COLORS,SRCCOPY); //copy from opencv Mat object to CImage
rezult->Create(output.GetWidth(),output.GetHeight(),24);
output.BitBlt(rezult->GetDC(),0,0,SRCCOPY); //copy to CImage *rezult
output.ReleaseDC();
rezult->ReleaseDC();
}