Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
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()
        {
            InitializeComponent();
            grx = label1.CreateGraphics();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            c1 = colorDialog1.Color;            
            label1.Invalidate();
        }             
 
        private void button2_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            c2 = colorDialog1.Color;
            label1.Invalidate();
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            grx.Clear(label1.BackColor);
            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);
            e.Graphics.FillRectangle(b,label1.ClientRectangle);
        }
    }
}
 
thanks in advance!!!
Posted 17-Feb-13 17:40pm
Comments
Sergey Alexandrovich Kryukov at 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).
—SA

1 solution

Rate this: bad
good
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 vb.net application?[^].
 
[EDIT]
 
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.
 
—SA
  Permalink  
v3
Comments
Jayanta Chatterjee at 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 at 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.
—SA
Jayanta Chatterjee at 17-Feb-13 23:44pm
   
Thanks Sir, I will try....
I find it in MSDN.... :-)
Sergey Alexandrovich Kryukov at 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).
—SA

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

  Print Answers RSS
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 17 Feb 2013
Copyright © CodeProject, 1999-2014
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