-1

I don't know how to repaint my picturebox. This is just a demonstration. The production code is too time consuming to put in the paint event. What I need is a way to capture the image that is drawn in the pucturebox with the graphics methods so I can quickly repaint it when needed.

Here's a demo:

public partial class Form1 : Form
{

    Image Outout;


    public Form1()
    {
        InitializeComponent();
        button1.Click += Button1_Click;
    }


    private void Button1_Click(Object sender, EventArgs e)
    {
        PrintPageEventArgs eOutput;
        Graphics g;
        string OutputText;
        Font PrintFont;


        OutputText = "CERTIFICATION";
        PrintFont = new Font("Arial", 16, FontStyle.Bold);
        g = pictureBox1.CreateGraphics();
        eOutput = new PrintPageEventArgs(g, new Rectangle(new Point(25, 25), new Size(new Point(825, 1075))), new Rectangle(new Point(0, 0), new Size(new Point(850, 1100))), new PageSettings());
        eOutput.Graphics.DrawString(OutputText, PrintFont, Brushes.Black, 0, 0);
        Outout = pictureBox1.Image;
        pictureBox1.Paint += PictureBox1_Paint;

    }


    private void PictureBox1_Paint(object sender, PaintEventArgs e)
    {
        pictureBox1.Image = Outout;
    }
}
itsme86
  • 19,266
  • 4
  • 41
  • 57
Github
  • 39
  • 6
  • 1
    `g = pictureBox1.CreateGraphics();` - Winforms graphics basic rule #1 : Never use `control.CreateGraphics`! Never try to cache a `Graphics` object! Either draw into a `Bitmap bmp` using a `Graphics g = Graphics.FromImage(bmp)` or in the `Paint` event of a control, using the `e.Graphics` parameter.. - The correct way is to keep a list of things to draw and whenever that list changes `Invalidate` the control you draw on. All drawing should be in the `Paint` event, using `e.Graphics` there! - Also: Your Paint event code is useless. – TaW Apr 16 '19 at 20:00
  • [Here](https://stackoverflow.com/questions/27623365/existing-graphics-into-bitmap/27647011?r=SearchResults&s=16|19.9168#27647011) and [here](https://stackoverflow.com/questions/30584465/use-picturebox-as-a-canvas-and-draw-text/30600471?r=SearchResults&s=11|31.0104#30600471) are two posts well worth studying. Also: You'd be amazed how fast a correctly written Paint event will be. But after a few tenthousand drawXXX calls you may start caching them in a Bitmap.. However a) not much erlier and b) not before you understand the basics of GDI+ graphics.. – TaW Apr 16 '19 at 20:13
  • ..((The caching of course is done either with DrawToBitmap or by using a Graphics object from a bitmap)) – TaW Apr 16 '19 at 20:15
  • That's a weird use of a PrintPageEventArgs. – LarsTech Apr 16 '19 at 20:50

1 Answers1

0

Thanks TaW for pointing me in the right direction. The example I posted is from an ERP system and the actual problem uses over a dozen objects plus the graphics is from a multipage report. So drawing in the paint event will not work. For starters, keeping a list of things to draw is pointless, since EVERYTHING needs to be drawn when the part number changes. Also the report generator needs to run twice, the first time just to calculate the number of pages and the second time to actually draw the pages. Also I cant use the PrintPreview of the PrintDocument because of its limitations. But you were spot on about using Graphics g = Graphics.FromImage(bmp). That's what I needed.

@LarsTech, you are correct, that is a weird use of the PrintPageEventArgs. That is merely a side effect when an issue embedded in a couple of events and several objects needs to be reduced in form to present a representation of a problem. If its reduced too small, the proposed solutions do not scale, therefore, do not work. If it is not reduced enough, it can be difficult to understand the actual problem, as people will present solutions to different aspects, some of which are artificial due to the reduction of the issue.

ANSWER

Drawing on the graphics created by the picturebox was not persistent. Drawing on a bitmap, however, worked perfectly as suggested by TaW. Thank you for your assistance!

        PrintPageEventArgs eOutput;
        Graphics g;
        string OutputText;
        Font PrintFont;
        Bitmap Output;




        OutputText = "CERTIFICATION";
        PrintFont = new Font("Times New Roman", 24, FontStyle.Regular);
        Output = new Bitmap(850, 1100);
        g = Graphics.FromImage(Output);
        eOutput = new PrintPageEventArgs(g, new Rectangle(new Point(25, 25), new Size(new Point(825, 1075))), new Rectangle(new Point(0, 0), new Size(new Point(850, 1100))), new PageSettings());
        eOutput.Graphics.DrawString(OutputText, PrintFont, Brushes.Black, 0, 0);
        pictureBox1.Image = Output;
Github
  • 39
  • 6