Use Label or DrawString in UserControl - c#

I am writing my own control that will contain a panel with text, images and other media. What is the best way to render the text and images. The control may contain long texts and many images.
Should I add the text as labels and images as PictureBox or should I use the DrawString and DrawImage methods to render the text and images?
What's the best way to do this?

If you use labels, then you get all the labelly goodness for free.
If you use DrawString, then it'll probably be (a little bit) faster, but it's a lot more complicated if you need to deal with things like the text changing.
The OnPaint handler is a always a tricky one to write, and invalidating the client area is tricky to do efficiently.
Why not let the labels handle it all for you?

I would use DrawString and DrawImage - you have less resources to worry about, but with added complexity.
I don't think it's that bad drawing your own strings and images once you get into it.
This is a nice introduction to it:
https://web.archive.org/web/20150116060854/http://bobpowell.net/

Related

Creating a picturebox/mapview type control with clickable rectangles and animated icons

I'm looking to add a 'mapview' type control to my project.
It must have a 'main map' image with clickable transparent rectangles with borders and icons/images that can be animated when an event occurs.
What would be the best way of achieving this using windows forms in C#?
My first thought was to use a picture box with other items on top of it but I might run into problems with transparency etc.
Are there any libraries or anything out there that would be able to achieve this?
No need for a library, really:
I would go for a regular doublebuffered Panel subclass or even a PictureBox subclass for the board/map along with a movable Label or Panel subclass fpr the rectangles/items.
Important: Make sure the Labels are not just 'put on top' of the PictureBox but really nested!! (lbl.Parent = pbox). Then transparency will work just fine..
Since PictueBox is not a 'container', to nest a control in it you need code. But since you probably want to create them dynamically this is not an issue anyway.
This assumes that the rectangles are not overlapping! For overlapping controls transparency in winforms will not work.
The clearer you understand the 'animate when event' part the easier the rest of the code will be..
Since you mention 'animation', a word of warning: Simple animation, especially in reponse to a user action is doable; for more classy animation you may run into the limits of winforms.

Text rendering: resizing text to fit inside a box?

I'm working on a text rendering system and I'm trying to implement a mechanism for automatically scaling down the text size to fit inside a specified box.
With word wrapping disabled, the task is nice and simple. All I have to do is check whether the width or height is proportionally larger, then scale the text down by that same ratio.
Unfortunately, with word wrapping enabled, the task gets much more complicated. If the text is larger than the box and I scale it down, the smaller text size might be able to fit more words onto each line, which might change the overall bounding box so that a different scale makes it fit the box better. Then applying that different scale might again mean there's a better way to word wrap it, and so on.
Most of the other solutions I've been able to find are basically:
while (isTooBig) { DecrementFontSize(); }
That would work, but could be very inefficient, so I was wondering if anyone knows of any lower level algorithms that deal directly with the text parsing? Failing that, do you know of any open source programs which include this feature so I can get their source code and see how they do it?
Edit: I'm doing this in Unity3D, but that doesn't really have any bearing on my problem since I'm implementing my own text rendering system.

How to handle the size of controls in WinForm application for different font size/localized text?

I am facing one same problem for trying to support any of the followings in a WinForm application:
An option that allow users to change font and font size in all memos/labels/buttons/edits/combos/grids/..etc controls.
Localization where the length/size of the text changes in different languages (and would need a larger font size)
"Windows Color and Appearance" settings which let you change font and font size
The problem is that the size/location/layout of the controls would be all messed up due to the changing size. Either a control can't show all its content, or an autosized control would overlap other controls, or gets clipped by the bound of a parent control, or have problems with wordwrap, or...etc.
The controls are usually some memos/labels/buttons/edits/combos/grids/..etc simply placed on a panel/groupbox/tab/..etc, or combinations of such controls nested in other container controls. I wonder if there are some easy standard less-painful way to handle this problem. (Also, wonder if there are some easy or standard way to make controls pick up the font setting automatically instead of assigning the fonts manually.)
Thanks in advance.
I would suggest placing controls in containers designed for the purpose...such as a TableLayoutPanel. Perhaps even nested panels, if your layout is complex. With that, you can allow most things to AutoSize, and control positions based on percentages of overall width/height.
As far as I could understand, you want to change the font size for all your controls, and fear that those changes would ruin your layout.
First you need to figure out how are you going to handle larger/longer text. You can wrap strings or trim the tails, or you can make your layout larger, while keeping the proportions of your controls the same. Other than that, I don't think there's any easy solution for handling arbitrary changes to font-size/language.

Recommendations for painting a custom text viewer

I want to develop a file viewing program for a specific read-only file format and for the most part it will just be scrollable text. The ultra-simple way is of course to use an existing text display controls, but I've come to the conclusion that I want a graphical sort of "custom colored highlighting", text coloring, and maybe other things painted in. So I was planning to handle the painting myself. I take it that attempting to line up my own graphics on top of a label or rich text box would be a bad idea, so I was planning to just paint everything except the scroll bar... unless these labels/rich text controls are a lot more extensible in some way that I don't know about?
Assuming I go the painting route, I'm not 100% sure of the specifics. Do I paint directly into a Panel? Or is there a better GUI control to paint into? Also, I think it will be better if I don't buffer the screen because I think repainting the contents on validation will be easy/efficient... but repainting from a buffer might be even faster... will it save me a lot of trouble if I just have a screen buffer... is this significantly inefficient? Is my plan of painting directly into a Panel, unbuffered, a good idea or is there a preferred method that I'm passing up?
Try to use WebBrowser control orRichTextBox (make formatted html or rtf from your text).

WPF control for image manipulation

I need to load an image in a WPF window, an be able to read and modify individual pixels (in an efficient way), zoom the image (and scroll it), get the value RGB/grayscale of the pixel under the cursor, select areas (I guess knowing the cursor position and being able to modify pixels I could draw myself the square which represents the selected area)...
What is the best combination of WPF controls and classes to accomplish this?
I've been trying to do it loading a System.Windows.Media.Imaging.BitmapImage and putting it into a System.Windows.Controls.Image, but it's taking much longer than I expected.
Thank you very much
I once used this WPF Interactive Image Cropping Control. Go check it out, it should at the very least give you a good place to start. Oh, and welcome to Stackoverflow. :)

Categories

Resources