Has anyone made a complete conversion from wpf Pen
to gdi+ one?
It doesn't sounds complicated at first: use constructor with corresponding brush. But there are so many small details: different brushes (5 in wpf and 5 in gdi+ with different names, properties, etc) and also pen properties itself.
Perhaps there is much simple solution, like ToString()
/Parse()
one or via serialization or perhaps a dedicated method or hidden class. Don't want to go long and wrong if(type is ...)
way.
Here is one possible approach (to demonstrate, may not work)
using System.Windows.Media;
using GDI = System.Drawing;
public static GDI.Pen ToGDI(this Pen pen)
{
var brush = pen.Brush;
var thickness = pen.Thickness;
if(brush is SolidColorBrush)
{
var color = ((SolidColorBrush)brush).Color;
return new GDI.Pen(new GDI.SolidBrush(Colors.FromArgb(color.A, color.R, color.G, color.B)), (float)thickness);
}
else if(brush is ...)
{
...
}
}
You can't convert from a WPF pen to a GDI pen or vice versa, because they are two entirely different systems for drawing to windows. The best you can do is create a new pen with properties as close as possible to the original pen.
The reason for this is that WPF doesn't use GDI to draw window contents (WPF/GDI interop situations notwithstanding). Instead, WPF uses its own renderer, based if I'm not mistaken on Direct2D and Direct3D. Thus, a pen in WPF isn't the same kind of thing as in GDI, where it exists as an actual graphics drawing primitive object.
If you need to have GDI pens that serve as counterparts to existing WPF pens, you don't really have much of a choice but to write a bunch of if
or switch
driven code to set up and return new System.Drawing.Pen objects. There's no other way around it.
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