Click here to Skip to main content
15,900,110 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: Saving Bitmap image data to HGLOBAL Pin
Mark Salsbery21-Jan-10 6:19
Mark Salsbery21-Jan-10 6:19 
GeneralRe: Saving Bitmap image data to HGLOBAL Pin
alleyes21-Jan-10 9:20
professionalalleyes21-Jan-10 9:20 
GeneralRe: Saving Bitmap image data to HGLOBAL Pin
alleyes20-Jan-10 7:34
professionalalleyes20-Jan-10 7:34 
GeneralRe: Saving Bitmap image data to HGLOBAL Pin
Mark Salsbery20-Jan-10 7:40
Mark Salsbery20-Jan-10 7:40 
GeneralRe: Saving Bitmap image data to HGLOBAL Pin
alleyes20-Jan-10 7:51
professionalalleyes20-Jan-10 7:51 
QuestionFade TabPage between TabControl's tab selection Pin
roshihans15-Jan-10 19:02
roshihans15-Jan-10 19:02 
QuestionI need an ebay program Pin
totolcm13-Jan-10 2:22
totolcm13-Jan-10 2:22 
QuestionCreating image file from pointer to HGLOBAL Pin
alleyes10-Jan-10 12:28
professionalalleyes10-Jan-10 12:28 
In an app that is used to capture images, the information used to store the image data is referenced by a pointer to HGLOBAL. That is then used to re-create the image so it can be written to disk with a call to GlobalFree.

I have two problems:

I need to attach a file header so I can write the image to a file.
The image data is of native type DWORD so I cant use the .NET classes without massaging the image data.

What I have done thus far:

BITMAPINFOHEADER* imgHeader = (BITMAPINFOHEADER*)GlobalLock(gPtrImage);

array<BYTE>^ imgArray = gcnew array<BYTE>(imgHeader->biSizeImage);

if (!imgHeader)
{
GlobalFree(gPtrImage);
return;
}

BITMAPFILEHEADER* bfh = new BITMAPFILEHEADER();
bfh->bfType = 0x4d42; //BM
bfh->bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + imgHeader->biSize + imgHeader->biSizeImage);
bfh->bfReserved1 = 0;
bfh->bfReserved2 = 0;
bfh->bfOffBits = (DWORD) (bfh->bfSize - imgHeader->biSizeImage);

LPDWORD Pixels = (LPDWORD)((LPBYTE)imgHeader + sizeof(BITMAPINFOHEADER));


Marshal::Copy(static_cast<IntPtr>(bfh), imgArray, 0, sizeof(BITMAPFILEHEADER));
Marshal::Copy(static_cast<IntPtr>(Pixels), imgArray, (sizeof(BITMAPFILEHEADER) + 1), imgHeader->biSizeImage);

GlobalUnlock(gPtrImage);

System::Environment::SpecialFolder saveFolder = System::Environment::SpecialFolder::MyDocuments;
SaveFileDialog^ sfd = gcnew SaveFileDialog();
sfd->InitialDirectory = System::Environment::GetFolderPath(saveFolder);
sfd->Filter = "Device Independent Bitmaps |*.bmp|All files|*.*";

if (sfd->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
System::IO::Stream^ imgFileStream = sfd->OpenFile();

if (imgFileStream != nullptr)
{
imgFileStream->Write(imgArray, 0, imgArray->Length);
imgFileStream->Close();
}
}

GlobalFree(gPtrImage);
gPtrImage = NULL;

The file size is a lot larger than I would expect for a 640*480 32 bit BMP and it can't be displayed.

The image is fine when written to a picturebox, the data is then unlocked but NOT freed. As a test, I save the image of the picturebox i.e PictureBox->Image and that is fine. The data needs to to be handed off to another application that requires native code so I need to re-create the image from the pointer to HGLOBAL and that's where it falls down

What might be wrong with this?

Thanks for any help
AnswerRe: Creating image file from pointer to HGLOBAL Pin
Mark Salsbery12-Jan-10 9:59
Mark Salsbery12-Jan-10 9:59 
GeneralRe: Creating image file from pointer to HGLOBAL Pin
alleyes12-Jan-10 10:25
professionalalleyes12-Jan-10 10:25 
GeneralRe: Creating image file from pointer to HGLOBAL [modified] Pin
Mark Salsbery12-Jan-10 10:34
Mark Salsbery12-Jan-10 10:34 
GeneralRe: Creating image file from pointer to HGLOBAL Pin
alleyes12-Jan-10 10:52
professionalalleyes12-Jan-10 10:52 
GeneralRe: Creating image file from pointer to HGLOBAL Pin
alleyes20-Jan-10 3:02
professionalalleyes20-Jan-10 3:02 
GeneralRe: Creating image file from pointer to HGLOBAL Pin
Mouzam Basheer23-May-16 20:40
Mouzam Basheer23-May-16 20:40 
QuestionGetting screen width and height Pin
m_mun7-Jan-10 4:12
m_mun7-Jan-10 4:12 
AnswerRe: Getting screen width and height Pin
Luc Pattyn7-Jan-10 4:19
sitebuilderLuc Pattyn7-Jan-10 4:19 
AnswerRe: Getting screen width and height Pin
Andreoli Carlo17-Jan-10 22:41
professionalAndreoli Carlo17-Jan-10 22:41 
QuestionSystem::String ^ to LPCWSTR conversion? Pin
Mattzimmerer6-Jan-10 22:27
Mattzimmerer6-Jan-10 22:27 
AnswerRe: System::String ^ to LPCWSTR conversion? Pin
Richard MacCutchan7-Jan-10 0:25
mveRichard MacCutchan7-Jan-10 0:25 
GeneralRe: System::String ^ to LPCWSTR conversion? Pin
Mattzimmerer7-Jan-10 9:14
Mattzimmerer7-Jan-10 9:14 
GeneralRe: System::String ^ to LPCWSTR conversion? Pin
Richard MacCutchan7-Jan-10 9:54
mveRichard MacCutchan7-Jan-10 9:54 
AnswerRe: System::String ^ to LPCWSTR conversion? [modified] Pin
Mark Salsbery7-Jan-10 9:35
Mark Salsbery7-Jan-10 9:35 
QuestionRe: System::String ^ to LPCWSTR conversion? Pin
Mattzimmerer7-Jan-10 9:59
Mattzimmerer7-Jan-10 9:59 
AnswerRe: System::String ^ to LPCWSTR conversion? Pin
Mark Salsbery7-Jan-10 12:56
Mark Salsbery7-Jan-10 12:56 
GeneralRe: System::String ^ to LPCWSTR conversion? Pin
Mattzimmerer7-Jan-10 22:01
Mattzimmerer7-Jan-10 22:01 

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.