Click here to Skip to main content
15,912,069 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: two dialogs placing problem? Pin
led mike30-Oct-07 10:12
led mike30-Oct-07 10:12 
GeneralRe: two dialogs placing problem? Pin
Mark Salsbery30-Oct-07 10:17
Mark Salsbery30-Oct-07 10:17 
GeneralRe: two dialogs placing problem? Pin
led mike30-Oct-07 11:10
led mike30-Oct-07 11:10 
QuestionNumber of Columns used in excel Pin
prithaa30-Oct-07 6:28
prithaa30-Oct-07 6:28 
QuestionI Need some help creating HBITMAP Pin
Miguel Resto30-Oct-07 5:12
Miguel Resto30-Oct-07 5:12 
AnswerRe: I Need some help creating HBITMAP [modified] Pin
Mark Salsbery30-Oct-07 6:49
Mark Salsbery30-Oct-07 6:49 
GeneralRe: I Need some help creating HBITMAP Pin
Miguel Resto30-Oct-07 17:18
Miguel Resto30-Oct-07 17:18 
GeneralRe: I Need some help creating HBITMAP Pin
Nelek30-Oct-07 22:13
protectorNelek30-Oct-07 22:13 
Hi, first of all, sorry for the extension. Im pasting a piece of a tutorial I have. It is explained how to load a bitmap from a file. I think you can inverse the process to save it there.

::::::::::::::::::::::::::::::::::::::::::::::::

Loading DIB through Serialization

Since we are going to store data read from the file in globally allocated memory, first we must add an
HGLOBAL type varible (along with a member function) to class CGDIDoc. Because DIB data will be changed
to DDB data before drawing, a CBitmap type variable and its associated funciton are also declared in class
CGDIDoc:
class CGDIDoc : public CDocument
{
protected:
CBitmap m_bmpDraw;
……
HGLOBAL m_hDIB;
……
public:
CBitmap *GetBitmap(){return &m_bmpDraw;}
……
HGLOBAL GetHDib(){return m_hDIB;}
……
}


Variable m_hDIB is initialized in the constructor:
CGDIDoc::CGDIDoc()
{
m_hDIB=NULL;
}


We will allocate global memory each time a new bitmap file is opened. So when the application exits,
we need to check variable m_hDIB to see if the memory has been allocated. If so, we need to release it:
CGDIDoc::~CGDIDoc()
{
if(m_hDIB != NULL)
{
::GlobalFree(m_hDIB);
m_hDIB=NULL;
}
}


We need to modify function CGDIDoc::Serialize(…) to load data from DIB file. First, when a file is
being opened, variable m_hDIB may be currently in use. In this case, we need to release the global memory:
void CGDIDoc::Serialize(CArchive& ar)
{
if(ar.IsStoring())
{}
else
{
BITMAPFILEHEADER bf;
DWORD dwSize;
LPSTR lpDIB;
if(m_hDIB != NULL)
{
::GlobalFree(m_hDIB);
m_hDIB=NULL;
}
……


Reading data from a DIB file needs the following steps:
1) Read bitmap file header.
2) Verify that the the file format is correct.
3) From the bitmap file header, calculate the total memory needed, then allocate enough buffers.
4) Read the DIB data into the allocated buffers.

We can call CArchive::Read(…) to read bytes from the file into the memory. In the sample, first
bitmap file header (data contained in structure BITMAPFILEHEADER) is read:
……
if
(
ar.Read
(
(LPSTR)&bf,
sizeof(BITMAPFILEHEADER)
) != sizeof(BITMAPFILEHEADER)
)return;
……


Then the file type is checked. If it is DIB format, global memory is allocated, which will be used to
store DIB data:
……
if(bf.bfType != 'MB')return;
dwSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
m_hDIB=::GlobalAlloc(GHND, dwSize);
ASSERT(m_hDIB);
lpDIB=(LPSTR)::GlobalLock(m_hDIB);
ASSERT(lpDIB);
……


Next, the DIB data is read into the global memory:

……
if(ar.Read(lpDIB, dwSize) != dwSize)
{
::GlobalUnlock(m_hDIB);
::GlobalFree(m_hDIB);
m_hDIB=NULL;
}
else ::GlobalUnlock(m_hDIB);
}
}


This completes reading the bitmap file. The DIB data is stored in m_hDIB now.

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

BTW it should be some articles here in codeproject talking about.

Greetings.

--------
M.D.V.

If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?

Help me to understand what I'm saying, and I'll explain it better to you

Wink | ;)

GeneralRe: I Need some help creating HBITMAP Pin
Mark Salsbery31-Oct-07 5:03
Mark Salsbery31-Oct-07 5:03 
Questiongot problem in compiling Pin
scizj30-Oct-07 4:31
scizj30-Oct-07 4:31 
AnswerRe: got problem in compiling Pin
led mike30-Oct-07 4:38
led mike30-Oct-07 4:38 
AnswerRe: got problem in compiling Pin
David Crow30-Oct-07 4:39
David Crow30-Oct-07 4:39 
GeneralRe: got problem in compiling Pin
led mike30-Oct-07 6:02
led mike30-Oct-07 6:02 
AnswerRe: got problem in compiling Pin
Nelek30-Oct-07 22:04
protectorNelek30-Oct-07 22:04 
QuestionNaive Bayes Pin
rarayi30-Oct-07 4:02
rarayi30-Oct-07 4:02 
AnswerRe: Naive Bayes Pin
Nelek30-Oct-07 4:09
protectorNelek30-Oct-07 4:09 
AnswerRe: Naive Bayes Pin
led mike30-Oct-07 4:39
led mike30-Oct-07 4:39 
Questionhow can i forbid anyone else to change the time of the computer? Pin
lostangels30-Oct-07 3:37
lostangels30-Oct-07 3:37 
AnswerRe: how can i forbid anyone else to change the time of the computer? Pin
Nelek30-Oct-07 4:08
protectorNelek30-Oct-07 4:08 
AnswerRe: how can i forbid anyone else to change the time of the computer? Pin
led mike30-Oct-07 4:41
led mike30-Oct-07 4:41 
Questionhelp needed for MFC ritch GUI controls Pin
sarat12in30-Oct-07 3:28
sarat12in30-Oct-07 3:28 
AnswerRe: help needed for MFC ritch GUI controls Pin
Dewm Solo30-Oct-07 3:51
Dewm Solo30-Oct-07 3:51 
AnswerRe: help needed for MFC ritch GUI controls Pin
led mike30-Oct-07 4:43
led mike30-Oct-07 4:43 
Questionsend big message from client to server [modified] Pin
Russell'30-Oct-07 2:22
Russell'30-Oct-07 2:22 
AnswerRe: send big message from client to server Pin
JudyL_MD30-Oct-07 2:27
JudyL_MD30-Oct-07 2:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.