How to Implement ASP. NET to display gradient Images,
First, let's get the final result:
To achieve the effect, first prepare an image with a height of 25 pixel and a gradient of 1 to 3 pixel. You can download it here.
Prepare the data as follows:
Dictionary<int, int> Datas { get { Dictionary<int, int> d = new Dictionary<int, int>(); d.Add(1, 35); d.Add(2, 45); d.Add(3, 20); return d; } }
OK, the data is ready. put three Label controls in aspx. Of course, you can display them in other controls or labels. Note that Width = "300 ", it is the gradient image width at 100%:
<asp:Label ID="Label1" runat="server" style="margin: 3px;" Text="" Width="300" BorderWidth="1"></asp:Label><br /> <asp:Label ID="Label2" runat="server" style="margin: 3px;" Text="" Width="300" BorderWidth="1"></asp:Label><br /> <asp:Label ID="Label3" runat="server" style="margin: 3px;" Text="" Width="300" BorderWidth="1"></asp:Label><br />
Display the data on the Label:
protected void Page_Load(object sender, EventArgs e) { Data_Binding(); } private void Data_Binding() { int totals = 100; foreach (KeyValuePair<int, int> kvp in Datas) { double rate = kvp.Value / (double)totals; double width = rate * 300; switch (kvp.Key) { case 1: this.Label1.Text = GradientImage(width, rate); break; case 2: this.Label2.Text = GradientImage(width, rate); break; case 3: this.Label3.Text = GradientImage(width, rate); break; } } } private string GradientImage(double width, double rate) { return " " + rate.ToString("p"); }
The above is how ASP. NET implements gradient images, hoping to help you learn.