Converting System.Drawing.Image to a DirectDrawSurface
-
Hi - I've saved a System.Drawing.Image to a file stream from C# and now I'm trying to load it in Managed C++... I have some other data that is included inbetween the bitmap data in the filestream and when I blt the surface to the backbuffer, the surface thats supposed to hold the bitmap data is entirely black. :-/
Tiles[i].Init(lpDD7); //Creates the DD7 Surface
bool Walkable = br->ReadBoolean();
MemoryStream^ ms = gcnew MemoryStream(br->ReadBytes(TILE_BITMAPSIZE));
Image^ bm = Image::FromStream(ms, true, true);
Graphics^ g = Graphics::FromImage(bm);
IntPtr^ hDC = g->GetHdc();
HDC src = static_cast<HDC>(hDC->ToPointer());
HDC dst;Tiles[i].Walkable = Walkable;
Tiles[i].Surface->GetDC(&dst);
BitBlt(dst, 0, 0, 32, 32, src, 0, 0, SRCCOPY);
Tiles[i].Surface->ReleaseDC(dst);Heres the INIT function
void Init(LPDIRECTDRAW7 lpDD7) { DDSURFACEDESC2 ddsd; INIT\_DXSTRUCT(ddsd); ddsd.dwFlags = DDSD\_CAPS | DDSD\_HEIGHT | DDSD\_WIDTH; ddsd.ddsCaps.dwCaps = DDSCAPS\_OFFSCREENPLAIN; ddsd.dwWidth = 32; ddsd.dwHeight = 32; HRESULT hr = lpDD7->CreateSurface(&ddsd, &Surface, NULL); }
Thanks.
-
Hi - I've saved a System.Drawing.Image to a file stream from C# and now I'm trying to load it in Managed C++... I have some other data that is included inbetween the bitmap data in the filestream and when I blt the surface to the backbuffer, the surface thats supposed to hold the bitmap data is entirely black. :-/
Tiles[i].Init(lpDD7); //Creates the DD7 Surface
bool Walkable = br->ReadBoolean();
MemoryStream^ ms = gcnew MemoryStream(br->ReadBytes(TILE_BITMAPSIZE));
Image^ bm = Image::FromStream(ms, true, true);
Graphics^ g = Graphics::FromImage(bm);
IntPtr^ hDC = g->GetHdc();
HDC src = static_cast<HDC>(hDC->ToPointer());
HDC dst;Tiles[i].Walkable = Walkable;
Tiles[i].Surface->GetDC(&dst);
BitBlt(dst, 0, 0, 32, 32, src, 0, 0, SRCCOPY);
Tiles[i].Surface->ReleaseDC(dst);Heres the INIT function
void Init(LPDIRECTDRAW7 lpDD7) { DDSURFACEDESC2 ddsd; INIT\_DXSTRUCT(ddsd); ddsd.dwFlags = DDSD\_CAPS | DDSD\_HEIGHT | DDSD\_WIDTH; ddsd.ddsCaps.dwCaps = DDSCAPS\_OFFSCREENPLAIN; ddsd.dwWidth = 32; ddsd.dwHeight = 32; HRESULT hr = lpDD7->CreateSurface(&ddsd, &Surface, NULL); }
Thanks.
Solved the problem... the Bitmap class can be cast into an HBITMAP & then selected as an HDC to be copied into the surface. Heres the fixed code:
MemoryStream^ ms = gcnew MemoryStream(br->ReadBytes(TILE_BITMAPSIZE));
Bitmap^ bm = gcnew Bitmap(Image::FromStream(ms, true, true));
HBITMAP hbm = (HBITMAP)bm->GetHbitmap().ToPointer();HDC src = CreateCompatibleDC(NULL);
SelectObject(src, hbm);
HDC dst;BitBlt(dst, 0, 0, Width, Height, src, 0, 0, SRCCOPY);