PascalDeclercq1964 / Icebear

Create reports in minutes with .net. Generate as PDF or print. No designer, just plain and simple code
1 stars 2 forks source link

cannot convert type 'System.Drawing.Pen' to 'PdfSharp.Drawing.XPen' #5

Open CsoftTarun opened 4 years ago

CsoftTarun commented 4 years ago

Getting this error for XBrush, XImage, XPen while casting.

public void DrawRectangle(Pen pen, Brush brush, double X, double Y, double W, double H) { XPen penUsed = pen != null ? new XPen(XColor.FromName(pen.Color.Name), pen.Width) : null; XBrush brushUsed = (XBrush)brush; gfx.DrawRectangle(penUsed, brushUsed, X, Y, W, H); }

    public void DrawString(string valueToRender, Style style, double X, double Y, double W, double H, Alignment Alignment)
    {

        XTextFormatter tf = new XTextFormatter(gfx);
        tf.Alignment = (XParagraphAlignment)Alignment;

        Font font =  new Font(style.Font.FontFamily, style.Font.SizeInPoints, style.Font.Style, GraphicsUnit.World);
        tf.DrawString(valueToRender, (XFont)font, (XBrush)style.Brush, new XRect(X, Y, W, H));

    }
    public void DrawLine(Pen pen, double X, double Y, double W, double H)
    {
        gfx.DrawLine((XPen)pen, X, Y, X+W, Y+H);
    }

    public void DrawImage(Image image, double X, double Y, double W, double H)
    {
        gfx.DrawImage(image, X, Y, W, H);
    }

How to get solve this error?

seddik commented 2 years ago

I use XColor to recreate the whole Brush

SolidColorBrush solidbrush = mybrush as SolidColorBrush;
XBrush xbrush = new XSolidBrush(XColor.FromArgb(solidbrush.Color.A,solidbrush.Color.R,solidbrush.Color.G,solidbrush.Color.B));

same applies for the XPen, also keep in mind that SolidBrush and Brush aren't exactly the same, when we use Solid that means that our brush is based on a Color, in the other hand a Brush or a Pen can base on an Image, you can draw shapes and fill them with an image as a Foreground(Stroke) or Background(Fill), and not just a color.