Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当图片消失时,如何重新绘制图片盒?_C#_.net_Winforms - Fatal编程技术网

C# 当图片消失时,如何重新绘制图片盒?

C# 当图片消失时,如何重新绘制图片盒?,c#,.net,winforms,C#,.net,Winforms,我不知道如何重新粉刷我的画盒。这只是一个示范。生产代码太耗时,无法放入绘制事件中。我需要的是一种用图形方法捕捉在pucturebox中绘制的图像的方法,以便在需要时快速重新绘制 下面是一个演示: public partial class Form1 : Form { Image Outout; public Form1() { InitializeComponent(); button1.Click += Button1_Click;

我不知道如何重新粉刷我的画盒。这只是一个示范。生产代码太耗时,无法放入绘制事件中。我需要的是一种用图形方法捕捉在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;
    }
}

谢谢TaW为我指明了正确的方向。我发布的示例来自一个ERP系统,实际问题使用了十几个对象,图形来自一个多页报告。因此,在绘制事件中绘制将不起作用。对于初学者来说,保留要绘制的内容列表是毫无意义的,因为当零件号更改时,所有内容都需要绘制。此外,报表生成器还需要运行两次,第一次仅用于计算页数,第二次用于实际绘制页面。此外,由于打印文档的限制,我无法使用打印预览。但是你对使用
Graphics g=Graphics.FromImage(bmp)
很在行。这就是我需要的

@LarsTech,你是对的,这是PrintPageEventArgs的一个奇怪用法。当嵌入在几个事件和几个对象中的问题需要简化为问题的表示形式时,这仅仅是一个副作用。如果其减少得太小,则建议的解决方案无法扩展,因此不起作用。如果减少的不够,就很难理解实际问题,因为人们会针对不同的方面提出解决方案,其中一些是人为的,因为问题的减少

答复

在picturebox创建的图形上绘制不是持久性的。然而,在位图上绘制,正如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;

g=pictureBox1.CreateGraphics()-Winforms graphics基本规则#1:切勿使用
控件。CreateGraphics
!切勿尝试缓存
图形
对象!使用
Graphics g=Graphics.FromImage(bmp)
或在控件的
Paint
事件中,使用
e.Graphics
参数将bmp绘制到
位图中正确的方法是保留要绘制的内容列表,并且每当该列表发生更改时,使所绘制的控件无效。所有图纸都应在
绘制
事件中,使用
e.Graphics
there!-另外:绘制事件代码是无用的。有两篇文章很值得研究。另外:你会惊讶于一个正确书写的绘画活动会有多快。但是在几分钟后,您可以开始在位图中缓存它们。。但是,a)没有太大的难度,b)在您了解GDI+图形的基础知识之前……((缓存当然是通过DrawToBitmap或使用位图中的图形对象完成的))这是PrintPageEventArgs的奇怪用法。