Click here to Skip to main content
12,997,042 members (129,430 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I created a application, which show the gradient effect on label background. At the meantime I want to draw some shapes on that label with graphic class. but the shapes are not visible on label background,If I removed the gradient effect from the label then it's visible..

So, how to draw the shapes on label which have gradient background color(without removing the gradient effect from label)??

My code for gradient effect is -:

using System;
using System.Drawing;
using System.Windows.Forms;
namespace ForPrintPre
    public partial class GradientEffect : Form
        Color c1=Color.Yellow, c2=Color.Green;
        Graphics grx;
        int countInt=0;
        public GradientEffect()
            grx = label1.CreateGraphics();
        private void button1_Click(object sender, EventArgs e)
            c1 = colorDialog1.Color;            
        private void button2_Click(object sender, EventArgs e)
            c2 = colorDialog1.Color;
        private void button3_Click(object sender, EventArgs e)
            if (comboBox1.Text == "Rectangle")
                grx.DrawRectangle(new Pen(Brushes.Black),new Rectangle(10,10,label1.Width-30,label1.Height-30));
            else if(comboBox1.Text=="Circle")
                grx.DrawArc(new Pen(Brushes.Black),10,10,label1.Width-30,label1.Height-30,0,360);
            else if (comboBox1.Text == "Line")
                grx.DrawLine(new Pen(Brushes.Black), 10, 10, label1.Width - 30, 10);
        private void label1_Paint(object sender, PaintEventArgs e)
            Color tem1 = Color.FromArgb(150, c1);
            Color tem2 = Color.FromArgb(150, c2);
            Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(label1.ClientRectangle, c1, c2, 20);

thanks in advance!!!
Posted 17-Feb-13 16:40pm
Sergey Alexandrovich Kryukov 17-Feb-13 22:45pm
This is a wrong way to show code sample. How do we that your methods are event handlers, of which events? You should always show how you add event handlers to the invocation lists of the event instances, in other words, all relevant "+=" operators.
You should not use auto-generated names. Rename all such names to makes them semantically sensible and observe naming conventions (auto-generated names violate them, auto generation just cannot do better).

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Please see my comment to the question.

The problem is quite apparent. The code is incorrect, because you do rendering on click events. The drawing happens, but it does not persist. You should do all the rendering in your handler of the event Paint, or in the overridden OnPaint method. Use only the instance of Graphics obtained from event arguments.

You gradient is done correctly, shapes are not. Your handler of the mouse event handlers should only modify some data (store it in a control class, for example), and trigger re-rendering by calling Control.Invalidate.

Please see my past answers:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my application?[^].


I'm not saying that you never should do drawing in mouse event handlers. Sometimes you can, but this is an advanced technique. You should understand that the rendered drawing will not persist unless you reproduce the drawing in OnPaint. My past answer explaining how WM_PAINT works should explain it for your.

Jayanta Chatterjee 17-Feb-13 23:28pm
Sir can you give me the explanation about WM_PAINT?
I searched,but I cannot understand what is it and how it's works.....
so please give me link or definition with example....
Sergey Alexandrovich Kryukov 17-Feb-13 23:41pm
Please find the explanation in my referenced answers.
In principle, you can work without understanding it, considering it as implementation detail, in other words, a black box.
Jayanta Chatterjee 17-Feb-13 23:44pm
Thanks Sir, I will try....
I find it in MSDN.... :-)
Sergey Alexandrovich Kryukov 17-Feb-13 23:52pm
Please try. Considering you already achieved gradient rendering, it's not too hard. Please don't forget to accept the answer (formally; the green button).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170622.1 | Last Updated 17 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100