Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have pixel data and other information(image height & width, bitcount) etc, using it I would like to create a MFC CImage object. I found a number of solutions which create bitmaps (using functions like CreateDIBSection(), CreateDIBitmap() etc)then attaching its handle(HBITMAP). I can't follow these methods because but I don't have device handle, these functions uses handle to device context as parameter.
One of the solution them suggests to use "CreateDIBSection" function with HDC parameter as NULL. Is it an affordable solution?
APlease suggest a method to create MFC "CImage" object from image pixel data (DIB bit values) and BITMAPHEADER information without having device context(HDC) information.
Posted
Updated 12-Mar-14 1:18am
v2

1 solution

Have you tried the Create[^] function ? Can you maybe post a bit of your code, so that we can understand what you are at ?
 
Share this answer
 
Comments
the vacuum 12-Mar-14 1:44am    
BOOL CDCMImage::LoadFromFile(LPCTSTR pszFileName)
{
m_oeImgType = IMG_NONE;
if(TRUE != m_CImage.IsNull())
m_CImage.Destroy();
CDicomImage oCDicomImage;
CString cstrfilename= pszFileName;
cstrfilename.Replace(_T('\\'), _T('//'));
char achfilename [200]= "";
USES_CONVERSION;
strcpy(achfilename,T2A(cstrfilename));
if(TRUE == oCDicomImage.OpenDCMFile((const char*)achfilename))
{
m_oeImgType = IMG_DCM;
void *pvDibData = NULL;
CSize ImgSize(0, 0);
ImgSize = oCDicomImage.DCMImageDIBData(pvDibData);

if((ImgSize != CSize(-1, -1)) && (pvDibData != NULL))
{
BITMAPINFO oBMI;
oBMI.bmiHeader.biSize = sizeof(oBMI);
oBMI.bmiHeader.biWidth = ImgSize.cx;
oBMI.bmiHeader.biHeight = -ImgSize.cy;
oBMI.bmiHeader.biPlanes = 1;
oBMI.bmiHeader.biBitCount = 24;
oBMI.bmiHeader.biCompression = BI_RGB;
oBMI.bmiHeader.biSizeImage = 0;
// hbmp Should destroy after use

m_hbmpDCM = CreateDIBSection(NULL, &oBMI, DIB_RGB_COLORS, (void**)&pvDibData, NULL, NULL);
if(m_hbmpDCM)
{
m_CImage.Attach(m_hbmpDCM);
}
if(pvDibData)
{
delete pvDibData;
pvDibData = NULL;
return true;
}
}
}
else
return false;
}

Given code is part of my program for reading a DICOM file from directory and to attach the dicom image data as a CImage type member of my class "CDCMImage". The pixel data of the reading DICOM image will get at "pvDibData" by call to function "oCDicomImage.DCMImageDIBData(pvDibData)" and . Upto that part part is correct I have already verified it. My problem is in remaining part which create CImage using the pixel data. Issue is how to create a CImage from the pixel data and other image details(image size, bit count etc.)which I already have?.
As you can view in code, I used "CreateDIBSection" windows function with HDC parameter (first parameter )as creating CImage. I'm not sure whether it is correct. Can I use it like that?
Rage 12-Mar-14 9:33am    
I think this is OK.

Note:

oBMI.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); // oBMI is a BITMAPINFO
the vacuum 14-Mar-14 2:59am    
Unfortunately above code not working, it only creating a black Image in CImage object. Can I use memory DC in CreateDIBSection function?.
Or
if use CImage::Create method how can set the image byte array values?
the vacuum 14-Mar-14 6:33am    
I got two solutions from the link
http://stackoverflow.com/questions/6691292/create-cimage-from-byte-array
I have checked both of them and are working...
Rage 14-Mar-14 6:38am    
OK, yeah, they showed you what to do after using ::Create. -> Good !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900