Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Converting System.Drawing.Image to a DirectDrawSurface

Converting System.Drawing.Image to a DirectDrawSurface

Scheduled Pinned Locked Moved Managed C++/CLI
graphicscsharpc++
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bfoo75
    wrote on last edited by
    #1

    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.

    B 1 Reply Last reply
    0
    • B bfoo75

      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.

      B Offline
      B Offline
      bfoo75
      wrote on last edited by
      #2

      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);

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups