直接绘制到 PictureBox
Draw directly to PictureBox
我正在开发一个 屏幕共享应用程序 ,它不断运行循环并从套接字接收小帧。下一步是将它们绘制到 PictureBox 中。
当然,我使用线程是因为我不想冻结 ui.
这是我的代码:
Bitmap frame = byteArrayToImage(buff) as Bitmap;//a praticular bitmap im getting from a socket.
Bitmap current = (Bitmap)pictureBox1.Image;
var graphics = Graphics.FromImage(current);
graphics.DrawImage(frame, left, top);//left and top are two int variables of course.
pictureBox1.Image = current;
但现在我收到一个错误:
Object is already in use elsewhere.
这一行var graphics = Graphics.FromImage(current);
试图Clone
它,创建一个New Bitmap(current)
。仍然没有成功。
Invalidate() 您的 PictureBox 以便它重新绘制自己:
Bitmap frame = byteArrayToImage(buff) as Bitmap;
using (var graphics = Graphics.FromImage(pictureBox1.Image))
{
graphics.DrawImage(frame, left, top);
}
pictureBox1.Invalidate();
如果你需要它是线程安全的,那么:
pictureBox1.Invoke((MethodInvoker)delegate {
Bitmap frame = byteArrayToImage(buff) as Bitmap;
using (var graphics = Graphics.FromImage(pictureBox1.Image))
{
graphics.DrawImage(frame, left, top);
}
pictureBox1.Invalidate();
});
我正在开发一个 屏幕共享应用程序 ,它不断运行循环并从套接字接收小帧。下一步是将它们绘制到 PictureBox 中。 当然,我使用线程是因为我不想冻结 ui.
这是我的代码:
Bitmap frame = byteArrayToImage(buff) as Bitmap;//a praticular bitmap im getting from a socket.
Bitmap current = (Bitmap)pictureBox1.Image;
var graphics = Graphics.FromImage(current);
graphics.DrawImage(frame, left, top);//left and top are two int variables of course.
pictureBox1.Image = current;
但现在我收到一个错误:
Object is already in use elsewhere.
这一行var graphics = Graphics.FromImage(current);
试图Clone
它,创建一个New Bitmap(current)
。仍然没有成功。
Invalidate() 您的 PictureBox 以便它重新绘制自己:
Bitmap frame = byteArrayToImage(buff) as Bitmap;
using (var graphics = Graphics.FromImage(pictureBox1.Image))
{
graphics.DrawImage(frame, left, top);
}
pictureBox1.Invalidate();
如果你需要它是线程安全的,那么:
pictureBox1.Invoke((MethodInvoker)delegate {
Bitmap frame = byteArrayToImage(buff) as Bitmap;
using (var graphics = Graphics.FromImage(pictureBox1.Image))
{
graphics.DrawImage(frame, left, top);
}
pictureBox1.Invalidate();
});