Freitag, 19. Juni 2009

Fill Background of a Control with Gradient

        /// <summary>

        /// Paints a gradient to the Control's background

        /// </summary>

        /// <param name="ctrl">Control that needs the brush up</param>

        /// <param name="color1">e.g. Color.FromArgb(120, 180, 255), or Color.Yellow</param>

        /// <param name="color2"></param>

        /// <param name="angle">0: horizontal, 45: top-left to bottom-right, etc.</param>

        private void PaintGradientBackground(Control ctrl, Color color1, Color color2, float angle)

        {

            Rectangle rect = new Rectangle(ctrl.Location, ctrl.Size);

 

            System.Drawing.Drawing2D.LinearGradientBrush gradBrush =

                new System.Drawing.Drawing2D.LinearGradientBrush(rect, color1, color2, angle);

 

            Bitmap bmp = new Bitmap(rect.Width, rect.Height);

 

            Graphics g = Graphics.FromImage(bmp);

            g.FillRectangle(gradBrush, rect);

 

            ctrl.BackgroundImage = bmp;

            ctrl.BackgroundImageLayout = ImageLayout.Stretch;

        }

Keine Kommentare:

Kommentar veröffentlichen