Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- // Last week I posted the point where I was stumped working on getting a CPU-side bitmap
- // from DirectWrite rendered text. At that time the most promising route I had found was through
- // rendering to a D2D render target, backing that render target with a D3D texture, which would then
- // let me get to the bits. I did solve that version, thanks to help from @rygorous. It took a lot
- // of code and brought in a lot of dependencies but it worked...
- //
- // Then @mmozeiko sent me a code snippet that showed how to interface with GDI. His code was simpler than
- // what I was doing and had no extra dependencies. I stripped out just the very little part that I needed,
- // avoiding all the sub-classing towards into which COM style interfaces generally drive you.
- //
- // This is the result. -Allen
- // ... Get the factory, font face, whatever else you need ...
- // Magic sauce that I was missing when I made the last post:
- // DWrite GDI Interop
- IDWriteGdiInterop *dwrite_gdi_interop = 0;
- error = dwrite_factory->GetGdiInterop(&dwrite_gdi_interop);
- // DWrite Bitmap Render Target
- int32_t width = 200;
- int32_t height = 200;
- IDWriteBitmapRenderTarget *render_target = 0;
- error = dwrite_gdi_interop->CreateBitmapRenderTarget(0, width, height, &render_target);
- // Render Glyph
- int32_t index = 10;
- RECT bounding_box = {0};
- DWRITE_GLYPH_RUN glyph_run = {0};
- glyph_run.fontFace = font_face;
- glyph_run.fontEmSize = point_size*96.f/72.f;
- glyph_run.glyphCount = 1;
- glyph_run.glyphIndices = &index;
- error = render_target->DrawGlyphRun(100.f, 100.f,
- DWRITE_MEASURING_MODE_NATURAL, &glyph_run, rendering_params, RGB(255, 255, 255),
- &bounding_box);
- DWCheck(error);
- // Then read it through good ol' GDI
- HDC dc = render_target->GetMemoryDC();
- HBITMAP bitmap = (HBITMAP)GetCurrentObject(dc, OBJ_BITMAP);
- DIBSECTION dib = {0};
- GetObject(bitmap, sizeof(dib), &dib);
- // ... do whatever you want with your new bitmap ...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement