简体   繁体   English

我为什么不使用PictureBox控件?

[英]Why should I not use the PictureBox control?

In his answer Rick Brewster stated that " PictureBox is often misunderstood." 在他的回答中,里克布鲁斯特表示“ PictureBox经常被误解。” and "You will probably almost never want to use it." 并且“你可能几乎不想使用它。”

Unfortunately he didn't explain what's wrong with PictureBox . 不幸的是,他没有解释PictureBox什么问题。 I use it to display and manipulate an image and it is kind of slow but what's the point of it if it's not for displaying pictures? 我用它来显示和操作一个图像,它有点慢,但如果它不是用于显示图片,它有什么意义呢?

It is a convenience control, useful for point-and-click UI design. 它是一种便捷控件,对于点击式UI设计非常有用。 But sure, it is very wasteful. 但可以肯定的是,它非常浪费。 Although it doesn't hold a candle to the Label control. 虽然它对Label控件没有任何影响。 You are burning up an entire Windows window, just to draw an image. 您正在烧毁整个Windows窗口,只是为了绘制图像。 Native window objects are very expensive system resources. 本机窗口对象是非常昂贵的系统资源。 The alternative is one line of code in your OnPaint() method, e.Graphics.DrawImage(). 替代方案是OnPaint()方法中的一行代码,e.Graphics.DrawImage()。

And it is not a very smart control either as Rick points out. Rick指出,这也不是一个非常聪明的控制。 It rescales the image to fit the control every single time it needs to paint itself. 它重新调整图像以适应控件每次需要自己绘制的时间。 And it doesn't optimize the pixel format of the image either, forcing GDI+ to make the pixel conversion every single time as well. 并且它也没有优化图像的像素格式,也迫使GDI +每次都进行像素转换。 The net effect can certainly be a slow UI, getting an image to draw 100x slower than necessary is certainly not unusual. 净效果当然可能是一个缓慢的UI,使图像绘制速度比必要慢100倍肯定不常见。 Otherwise the kind of trade-off that is difficult to make in a general-purpose control; 否则,在通用控制中难以做出的那种权衡; such optimizations don't come for free, potentially doubling the memory requirements. 这种优化不是免费的,可能会使内存需求翻倍。

For comparison, a Microsoft Office program like Outlook uses about 50 windows, most of them are toolbars. 相比之下,像Outlook这样的Microsoft Office程序使用大约50个窗口,其中大多数是工具栏。 That's what you slam together in Winforms in less than 10 minutes. 这就是你在不到10分钟的时间内在Winforms中一起吵架的原因。 Convenience has a price. 便利是有代价的。 So does an Office program, it takes a lot of programmers. Office程序也是如此,它需要很多程序员。

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

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