Passing Graphics object between native C++ and C#
I'm currently working on a Paint .NET-like application. I have mulitple types of layers which are implemented in C#. These layers are drawn into a .NET Graphics object that is provided by a WinForms user control - it is similar to the WPF canvas control. The layer base class has a Draw method that is implemented as follows:
public void Draw(IntPtr hdc)
{
using (var graphics = Graphics.FromInternalHDC(hdc)
{
// First: Setup rendering settings like SmoothingMode, TextRenderingHint, ...
// Layer specific drawing code goes here...
}
}
For performance and decompiling issues I'm doing the composition of the layers in a mixed mode assembly since I'm also applying effects like bevel or drop shadow. The wrapper, of course written in C++/CLI, gets directly called from the canvas control and forwards the metadata of each layer and the target Graphics object (Graphics object from my C# written canvas user control) to a native C++ class.
C++/CLI Wrapper:
public ref class RendererWrapper
{
public:
void Render(IEnumerable<Layer^>^ layersToDraw, Graphics^ targetGraphics)
{
// 1) For each layer get metadata (position, size AND Draw delegate)
// 2) Send layer metadata to native renderer
// 3) Call native renderer Render(targetGraphics.GetHDC()) method
// 4) Release targetGraphics HDC
};
}
Native C++ Renderer:
class NativeRenderer
{
void NativeRenderer::Render(vector<LayerMetaData> metaDataVector, HDC targetGraphicsHDC)
{
Graphics graphics(targetGraphicsHDC);
// Setup rendering settings (SmoothingMode, TextRenderingHint, ...)
for each metaData in metaDataVector
{
// Create bitmap and graphics for current layer
Bitmap* layerBitmap = new Bitmap(metaData.Width, metaData.Height, Format32bppArgb);
Graphics* layerGraphics = new Graphics(layerBitmap);
// Now the interesting interop part
// Get HDC from layerGraphics
HDC lgHDC = layerGraphics->GetHDC();
// Call metaData.Delegate and pass the layerGraphics HDC to C#
// By this call we are ending up in the Draw method of the C# Layer object
metaData.layerDrawDelegate(lgHDC);
// Releasing HDC - leaving interop...
layerGraphics->ReleaseHDC(lgHDC);
// Apply bevel/shadow effects
// Do some other fancy stuff
graphics.DrawImage(layerBitmap, metaData.X, metaData.Y, metaData.Width, metaData.Height);
}
}
}
So far so good. The above code works nearly as expected, but...
Problem
The only thing is that my current implementation is lacking of anti aliasing and semi transparency when rendering PNG with shadows for example. So I just have only 2 values for the Alpha channel: Transparent or full visible Color at 255. This side effect makes drawing PNGs with alpha channel and fonts looking very ugly. I cannot get the same smooth and nice semi transparent anti aliasing any more like before when I worked with pure C# code.
BUT: When drawing a string in the native Graphics object directly,
layerGraphics->DrawString(...);
anti aliasing and semi transparency are back for good. So the problem is only evident when passing the Graphics HDC to .NET.
Questions
Is there any solution/workaround for this problem? I've tried to create the Bitmap directly in the C# Layer class and return the IntPtr for the HBITMAP to the native code. This approach is working, but in this case I have another problem since I cannot find a perfect solution for converting HBITMAP to GDI+ Bitmap with alpha channel (white pixel noise is surrounding the edges when drawing fonts).
Thanks for your input! :)
Demo Solution
Attached you'll find a demo solution here: Sources
In this demo solution I'm testing 3 different rendering methods (all implemented in NativeRenderer.cpp), while the FIRST ONE shows the described problems:
1) RenderViaBitmapFromCSharp() - a) Creates a new bitmap in C++, creates a new Graphics object in C++, calls the C# drawing code by passing the C++ Graphics object HDC - Fails
But: b) Drawing directly from C++ works via the created bitmap too
2) RenderDirectlyFromCSharp() - Creates a new Graphics object from C# Graphics handle in C++ , calls the C# drawing code by passing the C++ Graphics object HDC - Works
3) RenderDirectlyFromCPP() - Creates a new Graphics object from C# Graphics handle in C++, draws the text directly in C++ - Works
Graphics graphics(targetGraphicsHDC);
You are creating a new Graphics object. So it won't have its properties setup like its original did. Properties like TextRenderingHint are not properties of a GDI device context, they are specific to Graphics.
Fairly ugly problem, you'll need to re-initialize the Graphics object the way it was done in the calling code. That's two chunks of code that are far removed from each other. Avoiding the conversion to HDC and back is the only really decent way to side-step the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With