图片消失后如何重绘图片框? [英] How do I repaint my picturebox when the picture disappears?

查看:45
本文介绍了图片消失后如何重绘图片框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是一个演示:

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

推荐答案

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

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,您是对的,这是PrintPageEventArgs的怪异用法.当嵌入在几个事件和几个对象中的问题需要减少形式以表示问题的表示时,这仅仅是一个副作用.如果将其减小得太小,则建议的解决方案将无法扩展,因此不起作用.如果减少的不够多,可能会很难理解实际的问题,因为人们会针对不同方面提出解决方案,其中一些是由于减少问题而人为设计的.

@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.

答案

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

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;

这篇关于图片消失后如何重绘图片框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆