【问题标题】:How do I repaint my picturebox when the picture disappears?图片消失时如何重新绘制图片框?
【发布时间】:2019-09-07 00:06:44
【问题描述】:

我不知道如何重新绘制我的图片框。这只是一个演示。生产代码太耗时,无法放入绘制事件。我需要一种方法来捕获使用图形方法在 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;
    }
}

【问题讨论】:

  • g = pictureBox1.CreateGraphics(); - Winforms 图形基本规则#1:永远不要使用control.CreateGraphics!永远不要尝试缓存 Graphics 对象!使用Graphics g = Graphics.FromImage(bmp) 或在控件的Paint 事件中使用e.Graphics 参数绘制Bitmap bmp 987654331@ 您绘制的控件。所有绘图都应该在Paint 事件中,在那里使用e.Graphics! - 另外:你的 Paint 事件代码没用。
  • Herehere 是两个值得研究的帖子。另外:您会惊讶于正确编写的 Paint 事件的速度有多快。但是经过几万次 drawXXX 调用后,您可能会开始将它们缓存在位图中。但是 a) 不会有太多错误,b) 在您了解 GDI+ 图形的基础知识之前不会...
  • ..((缓存当然是使用 DrawToBitmap 或使用位图中的 Graphics 对象))
  • PrintPageEventArgs 的使用很奇怪。

标签: c# .net winforms


【解决方案1】:

感谢 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;

【讨论】:

    猜你喜欢
    • 2017-10-18
    • 1970-01-01
    • 2013-05-14
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 2016-10-05
    相关资源
    最近更新 更多