简体   繁体   English

C#将位图加载到PictureBox中 - 慢并阻止UI ..任何替代方案?

[英]C# Load Bitmap into PictureBox - slow and blocks UI .. any alternatives?

I am loading 640x480 Bitmaps into a picture box one after the other. 我将640x480位图一个接一个地加载到一个图片框中。 When I do that my UI gets blocked. 当我这样做时,我的UI被阻止了。 For example, if I was typing something in a text box which is on the same form where my picture box is, I would not be able to see the key that pressed right away, because the bitmap loading makes the UI very slow .. How would one handle that ? 例如,如果我在文本框中键入内容,该文本框位于我的图片框所在的文本框中,我将无法立即看到按下的键,因为位图加载会使UI非常慢。会有人处理吗? is there any way around it ? 它周围有什么办法吗? Any sample code would be great. 任何示例代码都会很棒。

Thanks 谢谢

Use background worker so the gui is not freezed (async invoke the image display). 使用后台工作程序使gui不会冻结(异步调用图像显示)。 http://www.dreamincode.net/forums/topic/112547-using-the-backgroundworker-in-c%23/ http://www.dreamincode.net/forums/topic/112547-using-the-backgroundworker-in-c%23/

Your second request is a bit more tricky, I guess short answer would be not to rely on gdi+ if that's what you are doing be cause it's known to be slow. 你的第二个请求有点棘手,我想简短的回答就是不要依赖gdi +,如果这就是你正在做的事情,因为它已知很慢。 How exactly do you load the image into the imagebox? 您是如何将图像加载到图像框中的?

A Picturebox is actually a fairly 'heavy' control in what it provides; Picturebox实际上是一个相当“重”的控件,它提供的东西; it may not be the appropriate thing to use here. 在这里使用它可能不合适。 You might consider a much simpler container control, or drawing on the surface of the form itself. 您可以考虑更简单的容器控件,或绘制表单本身的表面。

If you want to consider BackgroundWorker or any other threaded technique here, keep in mind that the drawing itself must happen on the UI thread; 如果您想在此处考虑BackgroundWorker或任何其他线程技术,请记住,绘图本身必须在UI线程上进行; there's no way around that. 没有办法解决这个问题。

If the loading of the images from disk is the source of the latency, you might consider loading the images into an in-memory bitmap on another thread, then signaling somehow to indicate that a new item is ready to be drawn. 如果从磁盘加载图像是延迟的来源,您可以考虑将图像加载到另一个线程上的内存中位图,然后以某种方式发信号以指示新项目已准备好被绘制。 You would then invalidate the drawing surface, and add the new item as appropriate. 然后,您将使绘图表面无效,并根据需要添加新项目。

Also; 也; if you are doing any scaling to the images, doing this in the background thread would be appropriate - that way, the drawing code itself only needs to draw an unscaled rect; 如果你正在对图像进行任何缩放,那么在后台线程中执行此操作是合适的 - 这样,绘图代码本身只需要绘制一个未缩放的矩形; Using the GDI+ DrawUnscaled functionality to copy a bitmap to an area of the exact same size is actually quite fast. 使用GDI + DrawUnscaled功能将位图复制到完全相同大小的区域实际上非常快。


To get into anything more specific, like actual code, I would want to see code for how you are doing it now. 为了得到更具体的内容,比如实际的代码,我想看看你现在如何做的代码。 I'm not even sure you are 'drawing' the images in the first place, rather than simply setting Picture / Image properties. 我甚至不确定你是在“绘制”图像,而不是简单地设置Picture / Image属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM