Question

I have a PictureBox control whose Paint event I use to display some illustrations depending on app state. I need to set the following properties:

private void PictureBox_Paint (object sender, System.Windows.Forms.PaintEventArgs e)
{
    e.Graphics.CompositingMode    = CompositingMode.SourceOver;
    e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
    e.Graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
    e.Graphics.SmoothingMode      = SmoothingMode.HighQuality;
    e.Graphics.TextRenderingHint  = TextRenderingHint.AntiAlias;
}

This works fine but these properties are set every time the Pain event is fired which is pretty frequent. Is there a way to centrally set these properties? Please note that the size of the PictureBox control can change if the user re-sizes the window.

I know I can associate an image with the PictureBox control and keep a reference to the graphics object for that image instead. But I wanted to find out if the above is possible without an explicit image.

Was it helpful?

Solution

I don't think there is a better solution, unless as @Sinatr pointed out, you need the same settings for multiple Paint events. But that is just refactoring out to a common call.

You cannot get in front of the creation of that Graphics object, it's created by the graphics subsystem, so you're already doing the best you can...resetting the properties on-the-fly.

OTHER TIPS

Maybe consider using Mapper Pattern. It is frequently used in Enterprise Applications, but I think it is useful in home projects.

The well known library is Automapper but you can simply implement your own mapper without redundant functions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top