简体   繁体   中英

GDI System.Drawing.Printing convert white to black

I have created a basic cad viewer in a .Net Windows Forms application. Rendering is done with GDI.

I'm trying to implement functionality to physically print it, but the drawings consist of mainly white text & lines (normally viewed against a black background) which obviously don't print.

Is there an easy way to force white elements to print as black without checking the colour of every single element?

Simply inverting the colours is no good as coloured entities need to remain intact.

I assume that you have some code that prepares the image before drawing on screen or printing. Can you pass that code a parameter to tell it which color you want for those items that change? So rather than specifically using Pens.White or Brushes.White , have some code at the beginning of the method set the correct brush based on the parameter.

Your only other option would be to get the bitmap from the Graphics object and convert all white pixels to black. It's doable, but probably not what you want.

My fix involved creating a couple of simple utility classes:

    public static Pen GetPrintablePen(Pen pen)
    {
        if (pen.Color.R == 255 && pen.Color.G == 255 && pen.Color.B == 255)
        {
            Pen newPen = (Pen)pen.Clone();
            newPen.Color = Color.Black;
            return newPen;
        }
        return pen;
    }

    public static SolidBrush GetPrintableBrush(SolidBrush brush)
    {
        if (brush.Color.R == 255 && brush.Color.G == 255 && brush.Color.B == 255)
        {
            SolidBrush newBrush = (SolidBrush)brush.Clone();
            newBrush.Color = Color.Black;
            return newBrush;
        }
        return brush;
    }

Credit to Jim for providing the nearest answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM