图片消失后如何重新绘制图片框?

时间:2019-04-16 19:58:49

标签: c# .net winforms

我不知道如何重绘图片框。这只是一个示范。生产代码太耗时,无法进行绘画事件。我需要的是一种使用图形方法捕获在pucturebox中绘制的图像的方法,以便我可以在需要时快速重绘。

这是一个演示:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

感谢TaW向我指出正确的方向。我发布的示例来自ERP系统,实际问题使用了十几个对象,而图形来自多页报告。因此,绘画事件中的绘制将无法进行。对于初学者而言,保留要绘制的事物列表是没有意义的,因为零件编号发生变化时需要绘制所有内容。报表生成器还需要运行两次,第一次是为了计算页面数,第二次是实际绘制页面。另外,由于其限制,我不能使用PrintDocument的PrintPreview。但是您发现使用Graphics g = Graphics.FromImage(bmp)的机会。这就是我所需要的。

@LarsTech,您是对的,这是PrintPageEventArgs的怪异用法。当嵌入在几个事件和几个对象中的问题需要减少形式以表示问题的表示时,这仅仅是一个副作用。如果减小的幅度过小,则建议的解决方案将无法扩展,因此无法正常工作。如果减少的不够多,可能会很难理解实际问题,因为人们会针对不同方面提出解决方案,其中一些是由于减少问题而人为设计的。

答案

在图片框创建的图形上绘制不是持久的。但是,根据TaW的建议,在位图上绘制效果很好。谢谢您的协助!

        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;
相关问题