C#在多线程中将面板保存为图像 [英] C# Saving Panel as image at Multithread

查看:163
本文介绍了C#在多线程中将面板保存为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有任何问题将面板保存为带有UI线程的图像,但是当我将此面板保存在除UI线程之外的另一个线程时,我只有一个黑色矩形:

I don't have any problem saving panel as image with UI thread but i have only a black rectangle when i save this panel at another thread except UI thread :

using (Bitmap bmp = new Bitmap(panel1.Width, panel1.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
        {
            if (panel1.InvokeRequired)
            {
                panel1.BeginInvoke((MethodInvoker)delegate ()
                {
                    panel1.DrawToBitmap(bmp, new System.Drawing.Rectangle(Point.Empty, bmp.Size));

                });
                Bitmap bb = bmp.Clone(new System.Drawing.Rectangle(0, 0, 1016, 648), PixelFormat.Format24bppRgb);
                bb.Save(@"C:\sample.bmp", ImageFormat.Bmp);
            }
            else
            {
              panel1.DrawToBitmap(bmp, new System.Drawing.Rectangle(Point.Empty, bmp.Size));
              Bitmap bb = bmp.Clone(new System.Drawing.Rectangle(0, 0, 1016, 648), PixelFormat.Format24bppRgb);
              bb.Save(@"C:\sample.bmp", ImageFormat.Bmp);
            }

        }

此问题与锁定机制有关?或者我该如何解决这个问题?

This problem is related with locking mechanism? Or how can i solve this problem?

提前致谢。

推荐答案

通用答案(带说明)

BeginInvoke 是发送一个函数消息'此函数应该在不同的线程中执行'然后直接离开以继续在当前线程中执行。

BeginInvoke is function that send a message 'this function should be executed in different thread' and then directly leaves to continue execution in current thread.

当目标线程有空闲时间(处理之前发布的消息)时,该功能将在以后执行。

The function is executed at a later time, when the target thread has 'free time' (messages posted before are processed).

当您需要函数的结果时,请使用调用调用函数是'较慢',或更好地说它阻止当前线程,直到执行的函数完成。 (我刚在C#中测试过这个,但有可能,调用函数优先;例如当你调用 BeginInvoke 并且直接在调用到同一个线程后, Invoke 中的函数可能会在执行之前执行来自 BeginInvoke 的函数。)

When you need the result of the function, use Invoke. The Invoke function is 'slower', or better to say it blocks current thread until the executed function finishes. (I newer really tested this in C#, but it s possible, that the Invoke function is prioritized; e.g. when you call BeginInvoke and directly after it Invoke to the same thread, the function from Invoke will probably be executed before the function from BeginInvoke.)

当您需要先执行函数时,请使用此替代方法下一条指令被处理(当你需要调用函数的结果时)

Use this alternative when you need the function to be executed before the next instruction are processed (when you need the result of the invoked function).

简单(tl; dr ) :当您只需要设置值(例如设置编辑框文本)时,请使用 BeginInvoke ,但是当你需要一个结果时(例如从编辑框中获取文本)总是使用调用

Simple (tl;dr): When you need to need to only set a value (e.g. set text of edit box), use BeginInvoke, but when you need a result (e.g. get text from edit box) use always Invoke.

在您的情况下,您需要结果(要绘制的位图)因此您需要等待函数结束。 (还有其他可能的选项,但在这种情况下,简单的方法是更好的方法。)

In your case you need the result (bitmap to be drawn) therefore you need to wait for the function to end. (There are also other possible options, but in this case the simple way is the better way.)

这篇关于C#在多线程中将面板保存为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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