Saving Bitmap image data to HGLOBAL [modified]
-
alleyes wrote:
I can copy the bitmap data to this HGLOBAL using managed Bitmap classes?
You can't directly copy from managed to unmanaged memory. You can use Marshal::Copy though. You can also wrap the pointer in an UnmanagedMemoryStream object so you can use any managed functionality that uses a Stream object. For example, you could write a BMP file from a Bitmap to memory in one line of code instead of building the headers manually.
alleyes wrote:
If copying to the memory block is the lock still in place or do you call an GlobalUnLock first.
You use the pointer while it's locked.
Mark Salsbery Microsoft MVP - Visual C++ :java:
I think I have the solution:
MemoryStream^ ms = gcnew MemoryStream();
MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp);
array<Byte>^ bits = gcnew array<Byte>(MyHeader->biSizeImage);
bits = ms->GetBuffer();
ms->Close();
Marshal::Copy(bits, 0, static_cast<IntPtr>(MyHGlobal), bits->Length);Where: MyHeader->biSizeImage is the member from the BITMAPINFOHEADER MyHGlobal is the handle to the locked memory block. I then throw an AccessViolationException at the copy call. I think that perhaps the array size may be bigger than the memory block. I appreciate your help so far - I'm close!
-
I think I have the solution:
MemoryStream^ ms = gcnew MemoryStream();
MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp);
array<Byte>^ bits = gcnew array<Byte>(MyHeader->biSizeImage);
bits = ms->GetBuffer();
ms->Close();
Marshal::Copy(bits, 0, static_cast<IntPtr>(MyHGlobal), bits->Length);Where: MyHeader->biSizeImage is the member from the BITMAPINFOHEADER MyHGlobal is the handle to the locked memory block. I then throw an AccessViolationException at the copy call. I think that perhaps the array size may be bigger than the memory block. I appreciate your help so far - I'm close!
I said UnmanagedMemoryStream, not MemoryStream. And you don't cast an HGLOBAL to a pointer - GlobalLock returns the pointer. And Bitmap::Save() saves everything - BITMAPFILEHEADER, BITMAPINFO, and the pixel bits. You're trying to make it harder than it is :)
void *pMyGlobalMemory = GlobalLock(MyHGlobal);
UnmanagedMemoryStream^ ms = gcnew UnmanagedMemoryStream(IntPtr(pMyGlobalMemory), GlobalSize(MyHGlobal));
MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp);
GlobalUnlock(MyHGlobal);Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I said UnmanagedMemoryStream, not MemoryStream. And you don't cast an HGLOBAL to a pointer - GlobalLock returns the pointer. And Bitmap::Save() saves everything - BITMAPFILEHEADER, BITMAPINFO, and the pixel bits. You're trying to make it harder than it is :)
void *pMyGlobalMemory = GlobalLock(MyHGlobal);
UnmanagedMemoryStream^ ms = gcnew UnmanagedMemoryStream(IntPtr(pMyGlobalMemory), GlobalSize(MyHGlobal));
MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp);
GlobalUnlock(MyHGlobal);Mark Salsbery Microsoft MVP - Visual C++ :java:
I get an error when trying that: Error C2664: 'System::IO::UnmanagedMemoryStream::UnmanagedMemoryStream(unsigned char *,__int64)' : cannot convert parameter 1 from 'System::IntPtr' to 'unsigned char *' I didn't consider the UnmanagedMemoryStream
Mark Salsbery wrote:
You're trying to make it harder than it is Smile
Yeah it seems that way :doh:
-
I get an error when trying that: Error C2664: 'System::IO::UnmanagedMemoryStream::UnmanagedMemoryStream(unsigned char *,__int64)' : cannot convert parameter 1 from 'System::IntPtr' to 'unsigned char *' I didn't consider the UnmanagedMemoryStream
Mark Salsbery wrote:
You're trying to make it harder than it is Smile
Yeah it seems that way :doh:
Oops wrong constructor :)
unsigned char *pMyGlobalMemory = (unsigned char *)GlobalLock(MyHGlobal);
UnmanagedMemoryStream^ ms = gcnew UnmanagedMemoryStream(pMyGlobalMemory, GlobalSize(MyHGlobal));Note you're still resonsible for making sure the allocated global memory is large enough to write to. GlobalSize() will give you the allocated size, but if you don't know how many bytes will be written, you'll need to write to a growable memory stream first, check how many bytes were written, compare it to the globally allocated size, and reallocate if you need more room.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I said UnmanagedMemoryStream, not MemoryStream. And you don't cast an HGLOBAL to a pointer - GlobalLock returns the pointer. And Bitmap::Save() saves everything - BITMAPFILEHEADER, BITMAPINFO, and the pixel bits. You're trying to make it harder than it is :)
void *pMyGlobalMemory = GlobalLock(MyHGlobal);
UnmanagedMemoryStream^ ms = gcnew UnmanagedMemoryStream(IntPtr(pMyGlobalMemory), GlobalSize(MyHGlobal));
MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp);
GlobalUnlock(MyHGlobal);Mark Salsbery Microsoft MVP - Visual C++ :java:
I made a small change: void* pMyGlobal = GlobalLock(MyHGlobal); UnmanagedMemoryStream^ ms = gcnew UnmanagedMemoryStream((unsigned char*)(pMyGlobal), GlobalSize(gPtrImage)); MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp); It builds but then I get ExternalException was unhandled in GDI+
-
I made a small change: void* pMyGlobal = GlobalLock(MyHGlobal); UnmanagedMemoryStream^ ms = gcnew UnmanagedMemoryStream((unsigned char*)(pMyGlobal), GlobalSize(gPtrImage)); MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp); It builds but then I get ExternalException was unhandled in GDI+
alleyes wrote:
It builds but then I get ExternalException was unhandled in GDI+
I can't debug that for you from here, but I suspect not enough allocated on the HGLOBAL. So maybe something like this:
MemoryStream^ ms = gcnew MemoryStream(); MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp); if (GlobalSize(MyHGlobal) < ms->Length) { GlobalRealloc(MyHGlobal, ms->Length, 0); } void \*pMyGlobalMemory = GlobalLock(MyHGlobal); UnmanagedMemoryStream^ ums = gcnew UnmanagedMemoryStream((unsigned char \*)pMyGlobalMemory, GlobalSize(MyHGlobal)); ms->WriteTo(ums); GlobalUnlock(MyHGlobal);
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
alleyes wrote:
It builds but then I get ExternalException was unhandled in GDI+
I can't debug that for you from here, but I suspect not enough allocated on the HGLOBAL. So maybe something like this:
MemoryStream^ ms = gcnew MemoryStream(); MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp); if (GlobalSize(MyHGlobal) < ms->Length) { GlobalRealloc(MyHGlobal, ms->Length, 0); } void \*pMyGlobalMemory = GlobalLock(MyHGlobal); UnmanagedMemoryStream^ ums = gcnew UnmanagedMemoryStream((unsigned char \*)pMyGlobalMemory, GlobalSize(MyHGlobal)); ms->WriteTo(ums); GlobalUnlock(MyHGlobal);
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
alleyes wrote:
It builds but then I get ExternalException was unhandled in GDI+
I can't debug that for you from here, but I suspect not enough allocated on the HGLOBAL. So maybe something like this:
MemoryStream^ ms = gcnew MemoryStream(); MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp); if (GlobalSize(MyHGlobal) < ms->Length) { GlobalRealloc(MyHGlobal, ms->Length, 0); } void \*pMyGlobalMemory = GlobalLock(MyHGlobal); UnmanagedMemoryStream^ ums = gcnew UnmanagedMemoryStream((unsigned char \*)pMyGlobalMemory, GlobalSize(MyHGlobal)); ms->WriteTo(ums); GlobalUnlock(MyHGlobal);
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
NotSupportedException unhandled. Stream does not support writing? Since when? :confused:
Sorry try another constructor: ...gcnew UnmanagedMemoryStream((unsigned char *)pMyGlobalMemory, GlobalSize(MyHGlobal), GlobalSize(MyHGlobal), FileAccess.ReadWrite);
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Sorry try another constructor: ...gcnew UnmanagedMemoryStream((unsigned char *)pMyGlobalMemory, GlobalSize(MyHGlobal), GlobalSize(MyHGlobal), FileAccess.ReadWrite);
Mark Salsbery Microsoft MVP - Visual C++ :java:
Ugh! Still getting an AccessViolation Exception. That can either mean not enough memory available or is that this memory can't be written to. I see the CanWrite, CanSeek flags are TRUE. Capacity and Length are both the same - 1658934
modified on Thursday, January 21, 2010 9:22 AM
-
Ugh! Still getting an AccessViolation Exception. That can either mean not enough memory available or is that this memory can't be written to. I see the CanWrite, CanSeek flags are TRUE. Capacity and Length are both the same - 1658934
modified on Thursday, January 21, 2010 9:22 AM
Did you use FileAccess.ReadWrite? I'm only guessing here since I don't know what line the exception occurs on...
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Did you use FileAccess.ReadWrite? I'm only guessing here since I don't know what line the exception occurs on...
Mark Salsbery Microsoft MVP - Visual C++ :java:
Hi, This is what I have so far:
MemoryStream^ ms = gcnew MemoryStream();
MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp);
ms->Seek(0, SeekOrigin::Begin);if (GlobalSize(MyGlobal) < ms->Length)
{
GlobalReAlloc(MyGlobal, (LONG)ms->Length, 0);
}void* pMyGlobal = (UCHAR*)MyGlobal;
UnmanagedMemoryStream^ ums = gcnew UnmanagedMemoryStream((UCHAR*)pMyGlobal, GlobalSize(MyGlobal), GlobalSize(MyGlobal), FileAccess::ReadWrite);
ums->Position = 0;
ms->WriteTo(ums);It occurs on the line: ms->WriteTo(ums); This is even without setting the Position of the streams. I did this because just before the Call to WriteTo, the position was set to Length.
-
Hi, This is what I have so far:
MemoryStream^ ms = gcnew MemoryStream();
MyImage->Save(ms, System::Drawing::Imaging::ImageFormat::Bmp);
ms->Seek(0, SeekOrigin::Begin);if (GlobalSize(MyGlobal) < ms->Length)
{
GlobalReAlloc(MyGlobal, (LONG)ms->Length, 0);
}void* pMyGlobal = (UCHAR*)MyGlobal;
UnmanagedMemoryStream^ ums = gcnew UnmanagedMemoryStream((UCHAR*)pMyGlobal, GlobalSize(MyGlobal), GlobalSize(MyGlobal), FileAccess::ReadWrite);
ums->Position = 0;
ms->WriteTo(ums);It occurs on the line: ms->WriteTo(ums); This is even without setting the Position of the streams. I did this because just before the Call to WriteTo, the position was set to Length.
Where did this come from? void* pMyGlobal = (UCHAR*)MyGlobal; Casting a handle to a pointer is bad. For an HGLOBAL it's bad unless you know for sure GMEM_FIXED was used to allocate the memory. Much safer to use GlobalLock to get the pointer.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Where did this come from? void* pMyGlobal = (UCHAR*)MyGlobal; Casting a handle to a pointer is bad. For an HGLOBAL it's bad unless you know for sure GMEM_FIXED was used to allocate the memory. Much safer to use GlobalLock to get the pointer.
Mark Salsbery Microsoft MVP - Visual C++ :java:
Sorry I threw you there. The lock is already done earlier during the creating of the original images. Didn't make sense to call another GlobalLock on it again. The call to GlobalAlloc is GlobalAlloc(GMEM_MOVEABLE, Size) You have to call GlobalLock on the existing memory again?
-
Sorry I threw you there. The lock is already done earlier during the creating of the original images. Didn't make sense to call another GlobalLock on it again. The call to GlobalAlloc is GlobalAlloc(GMEM_MOVEABLE, Size) You have to call GlobalLock on the existing memory again?
alleyes wrote:
The lock is already done earlier during the creating of the original images. Didn't make sense to call another GlobalLock on it again. The call to GlobalAlloc is GlobalAlloc(GMEM_MOVEABLE, Size)
Then you need to use the pointer returned by the GlobalLock() call.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Sorry I threw you there. The lock is already done earlier during the creating of the original images. Didn't make sense to call another GlobalLock on it again. The call to GlobalAlloc is GlobalAlloc(GMEM_MOVEABLE, Size) You have to call GlobalLock on the existing memory again?
alleyes wrote:
Didn't make sense to call another GlobalLock on it again.
There's nothing wrong with locking again as long as you have an equal number of unlock calls. GlobalRealloc on a locked handle can only reallocate memory in the same place so has more chance of failing, since there may not be enough room in that place. For best results the handle should be unlocked before reallocating then locked again when you need the pointer.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
alleyes wrote:
Didn't make sense to call another GlobalLock on it again.
There's nothing wrong with locking again as long as you have an equal number of unlock calls. GlobalRealloc on a locked handle can only reallocate memory in the same place so has more chance of failing, since there may not be enough room in that place. For best results the handle should be unlocked before reallocating then locked again when you need the pointer.
Mark Salsbery Microsoft MVP - Visual C++ :java: