开发者

Windows Form, display indicator over a dynamically generated bitmap

开发者 https://www.devze.com 2023-04-04 09:31 出处:网络
I have a usercontrol that displays a gradient of colors which, once created will be constant. The usercontrol doesn\'t contain any controls, not sure if i need to add a pictur开发者_C百科ebox or dyna

I have a usercontrol that displays a gradient of colors which, once created will be constant.

The usercontrol doesn't contain any controls, not sure if i need to add a pictur开发者_C百科ebox or dynamically add one.

Over that image, I'd like to display a line that will display what the current result is. I have no problem creating the gradient image on the map, however I'd like to somehow cache it so everytime I update the indicator (call CurrentValue from parent form), it will put the indicator line above the gradient image. This is updating about 30 times a second, thus, as of how the code below is working, it's repainting the gradient everytime, which is flickering.

Here's a code sample:

namespace Maps.UserControls
{
    public partial class UserControlLegend : UserControl
    {
        private double m_CurrentValue;
        public double CurrentValue
        {
            get
            {
                return m_CurrentValue;
            }
            set
            {
                m_CurrentValue = value;
                RefreshValue();
            }
        }

        public UserControlLegend()
        {
            InitializeComponent();
        }

        private void UserControlLegend_Paint(object sender, PaintEventArgs e)
        {
            if (b == null)
            {
                g = e.Graphics;
                b = new System.Drawing.Bitmap(menuWidth, menuHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                // Code here that draws Menu

                // Cache bitmap here?
                g.Dispose();
            }
        }

        private void RefreshValue()
        {
            this.Refresh();
            g = this.CreateGraphics();
            g.DrawImage(b, 0, 0);
            //Code to Calcuate current Indicator Location

            int x3 = 0;
            // Draws current indicator correctly
            g.DrawLine(new Pen(new SolidBrush(Color.Black)), this.Width / 2 - 15, x3, this.Width / 2 - 5, x3);
            g.Dispose();
        }
    }
}


Explained above in comments, used a bitmap, and just set the x,y of the control.


First, I'd suggest you set your Control's property DoubleBuffered to True, so that flickering goes away. However, if you don't draw on the Control itself, that will be not useful at all. Drawing on a PictureBox is better, however, becuase it is automatically DoubleBuffered.

Second, you are painting into a new Bitmap every time, which is very bad in terms of memory, since the Bitmap is a few megabytes in size. I'd suggest you have a single Bitmap initialized in the constructor, and a single Graphics, created from that Bitmap in the constructor, too. Every time the paint accurs, just clear the old Graphics g and then draw onto it again and again. g Graphics and b Bitmap should be Disposed one time only, when the entire Control is Disposed.

This may inhance your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号