Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've ended up creating the Bitmap in C# and passing the object to C++/CLI. As already mentioned by Hans and Vincent you have to avoid GetHDC. So my workaround reads in pseudo code as follows:</p> <p><strong>Layer.cs C#:</strong></p> <pre><code>public Bitmap Draw() { var bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppArgb); using (var graphics = Graphics.FromBitmap(bitmap) { // First: Setup rendering settings like SmoothingMode, TextRenderingHint, ... // Layer specific drawing code goes here... } return bitmap; } </code></pre> <p><strong>NativeRenderer.cs C++:</strong></p> <pre><code>void NativeRenderer::RenderFromBitmapCSharp(System::Drawing::Bitmap^ bitmap) { // Create and lock empty native bitmap Bitmap *gdiBitmap = new Bitmap(bitmap-&gt;Width, bitmap-&gt;Height, PixelFormat32bppARGB); Rect rect(0, 0, bitmap-&gt;Width, bitmap-&gt;Height); BitmapData bitmapData; gdiBitmap-&gt;LockBits(&amp;rect, Gdiplus::ImageLockModeRead | Gdiplus::ImageLockModeWrite, PixelFormat32bppARGB, &amp;bitmapData); // Lock managed bitmap System::Drawing::Rectangle rectangle(0, 0, bitmap-&gt;Width, bitmap-&gt;Height); System::Drawing::Imaging::BitmapData^ pBitmapData = bitmap-&gt;LockBits(rectangle, System::Drawing::Imaging::ImageLockMode::ReadOnly, System::Drawing::Imaging::PixelFormat::Format32bppArgb); // Copy from managed to unmanaged bitmap ::memcpy(bitmapData.Scan0, pBitmapData-&gt;Scan0.ToPointer(), bitmap-&gt;Width * bitmap-&gt;Height * 4); bitmap-&gt;UnlockBits(pBitmapData); gdiBitmap-&gt;UnlockBits(&amp;bitmapData); // Draw it _graphics-&gt;DrawImage(gdiBitmap, 0, 0, bitmap-&gt;Width, bitmap-&gt;Height); } </code></pre> <p>Hope that is helpful to others - have not found any code snippet on the web which actually does converting managed to unmanaged GDI+ Bitmap.</p> <p>Thank you all for your comments.</p> <p>Cheers, P</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload