Why should I not use the PictureBox control?
Asked Answered
R

1

6

In his answer Rick Brewster stated that "PictureBox is often misunderstood." and "You will probably almost never want to use it."

Unfortunately he didn't explain what's wrong with 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?

Raster answered 8/11, 2013 at 15:58 Comment(4)
Perhaps you should ask Rick about what he meant. Don't over-assess the +/- if you don't understand the exact question/answer; there are quite a few (pretty bad) answers/questions with lots of +s.Crepuscular
I found this article that gives a little insight. bobpowell.net/pictureboxhowto.aspxPrying
When you use just 1 or 2 pictureboxes on your form, it's OK to use it, there is nothing wrong with it. It's just for convenience. I'm sure that if you try implementing your own PictureBox that can do the same what the standard PictureBox can do, you would be stuck at some feature because it's not easy as simply draw the image yourself.Expellant
BTW, Rick later (Aug. 2017) added a comment to his answer (linked in the question above), to clarify what he said about PictureBox.Actuality
C
5

It is a convenience control, useful for point-and-click UI design. But sure, it is very wasteful. Although it doesn't hold a candle to the Label control. You are burning up an entire Windows window, just to draw an image. Native window objects are very expensive system resources. The alternative is one line of code in your OnPaint() method, e.Graphics.DrawImage().

And it is not a very smart control either as Rick points out. 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. The net effect can certainly be a slow UI, getting an image to draw 100x slower than necessary is certainly not unusual. 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. That's what you slam together in Winforms in less than 10 minutes. Convenience has a price. So does an Office program, it takes a lot of programmers.

Catastrophism answered 8/11, 2013 at 16:16 Comment(5)
But then? What alternative you have? How can you show an image and have full control on it in your form? Isn't what you propose pretty expensive too (relying on the Paint event) and without allowing you the full control PictureBox provides?Crepuscular
Well, no, it will be a lot faster when you pre-scale the image, convert it to 32bppPArgb and draw it with DrawImage(). Programmers use code to control their program, it isn't exclusive to the Winforms designer. Just less convenient.Catastrophism
Honestly, I don't understand what you mean with "Programmers use code to control their program, it isn't exclusive to the Winforms designer"... But perhaps I didn't make myself clear: one thing is just showing a picture (you can use e.Graphics.Drawimage()), other thing is having the full control a "control" provides you, what PictureBox delivers. if you want just to add a bunch of pictures in certain positions, use e.Graphics; if you want to include 2/3 pictures and perform some control-like actions at runtime (rely on events, change its position, use its handle to call an API, etc.)...Crepuscular
... why not using a PictureBox (better: how can e.Graphics represent a reliable alternative at all)? I am asking all this for pure curiosity. I am a programmer focused on algorithm-related issues; I have quite a few experience on various controls but usually data-related ones. I don't have too much experience on the drawing side of things; but I have used PictureBox quite a few times and I don't think that it is so computational expensive and that replicating all its functionalities (if possible) is worthy at all.Crepuscular
I am working on displaying cctv frames as filmstrip thumbnails, I used pictureBox inside flowlayout and later on inside panel (900 frames). the memory consumption is not acceptable(very very unacceptable), finally I use ObjectListView and use DrawImage for those 900 frames...60mb memory max.Leisurely

© 2022 - 2024 — McMap. All rights reserved.