Using GDI + to draw graphics in asp.net

Source: Internet
Author: User
Tags getcolor
asp.net

Using GDI + to draw graphics in asp.net

gdi+--the next generation of configuration interfaces all graphical user interface (GUI) applications interact with hardware devices (displays, printers, scanners), which can be represented as reading data, for example. Instead of using the application to communicate directly with the device, it is necessary to separate the user interface code for each of the settings. To avoid this kind of redo work, we can use a third group between the application and the setup, which will be transferred and sent to the data of the facility, and the data that is to be sent to the program. And this group is GDI +. GDI + is the portal to the. NET framework for interacting with the graphics. GDI + is a group of C + +, located in a library called Gdiplus.dll. Gdiplus.dll is a built-in part of Windows XP and the Windows Server 2003 operating system. Well, there's not much nonsense here, so let's use GDI + to draw a picture in the Web form.

I think we've all seen it like this, and of course it's not standard, and here's the code to paste it into the fun you can try!

Namespace Sky_msdndatagrid

{

Using System;

Using System.Collections;

Using System.ComponentModel;

Using System.Data;

Using System.Drawing;

Using System.Drawing.Imaging;

Using System.Web;

Using System.Web.SessionState;

Using System.Web.UI;

Using System.Web.UI.WebControls;

Using System.Web.UI.HtmlControls;

<summary>

A summary description of the Aspxchart.

</summary>

public class AspxChart:System.Web.UI.Page

{

Private Bitmap Bitmap;

Private Graphics Graphics;

Private int[] arrvalues;

Private string[] arrValueNames;

private void Page_Load (object sender, System.EventArgs e)

{

arrvalues = new Int[6];

arrValueNames = new String[6];

Arrvalues[0] = 100;

ARRVALUES[1] = 135;

ARRVALUES[2] = 115;

ARRVALUES[3] = 125;

ARRVALUES[4] = 75;

ARRVALUES[5] = 120;

Arrvaluenames[0] = "January";

ARRVALUENAMES[1] = "February";

ARRVALUENAMES[2] = "March";

ARRVALUENAMES[3] = "April";

ARRVALUENAMES[4] = "May";

ARRVALUENAMES[5] = "June";

This. Init_bitmap ();

This. Draw_rectangle ();

This. Draw_pie ();

To save the drawing images in Gif format to the output stream of the front page.

Bitmap. Save (this. Response.outputstream,imageformat.gif);

}

<summary>

An initial motion that will be drawn (conceivably a piece of paint).

</summary>

private void Init_bitmap ()

{

Bitmap = new Bitmap (400,200);

Graphics = graphics.fromimage (bitmap);

Graphics. Clear (Color.White);

Graphics. DrawString ("X company sales in the first half", New Font ("neo-Detail", 16,fontstyle.underline), Brushes.black,new PointF (5,5));

}

Code generated #region Web Form design Tools

Override protected void OnInit (EventArgs e)

{

//

CodeGen: This is the call required for the ASP.net Web Form design tool.

//

InitializeComponent ();

Base. OnInit (e);

}

<summary>

This is the necessary way to support the design tool-please do not use the Code editor to modify

The content of this method.

</summary>

private void InitializeComponent ()

{

This. Load + = new System.EventHandler (this. Page_Load);

}

#endregion

<summary>

Draw a rectangle on top of the canvas

</summary>

private void Draw_rectangle ()

{

int i;

PointF Symbolleg = new PointF (335,20);

PointF Descleg = new PointF (360,16);

for (i = 0; i < arrvaluenames.length; i++)

{

Graphics. FillRectangle (New SolidBrush (GetColor (i)), symbolleg.x,symbolleg.y,20,10);

Graphics. DrawRectangle (pens.black,symbolleg.x,symbolleg.y,20,10);

Graphics. DrawString (Arrvaluenames[i]. ToString (), New Font ("newly-specified body", 8), Brushes.black,descleg);

Symbolleg.y + 15;

Descleg.y + 16;

}

for (i = 0; i < arrvalues.length; i++)

{

Graphics. FillRectangle (New SolidBrush (GetColor (i)), (i*35) + 15,200-arrvalues[i],20,arrvalues[i]);

Graphics. DrawRectangle (Pens.black, (i*35) + 15,200-arrvalues[i],20,arrvalues[i]);

}

}

<summary>

Draw a cookie on top of the canvas

</summary>

private void Draw_pie ()

{

int i;

Currentangle represents the totalangle of the maximum angle totalvalues represents the maximum sales

float Sglcurrentangle = 0,sgltotalangle = 0,sgltotalvalues = 0;

Calculate Maximum Sales

for (i = 0; i < arrvalues.length; i++)

Sgltotalvalues + = Arrvalues[i];

for (i = 0; i < arrvalues.length; i++)

{

Value of the month: Sales per month/maximum sales * 360

Sglcurrentangle = arrvalues[i]/sgltotalvalues * 360;

Graphics. FillPie (New SolidBrush (GetColor (i)), 240,95,100,100,sgltotalangle,sglcurrentangle);

Graphics. Drawpie (Pens.black,240,95,100,100,sgltotalangle,sglcurrentangle);

Sgltotalangle + = Sglcurrentangle;

}

}

#region Helper Function

Private Color getcolor (int itemindex)

{

Color color;

Switch (itemindex)

{

Case 0:

color = Color.Blue;

Break

Case 1:

color = color.red;

Break

Case 2:

color = Color.yellow;

Break

Case 3:

color = color.purple;

Break

Case 4:

color = Color.orange;

Break

Case 5:

color = Color.brown;

Break

Default

color = Color.Blue;

Break

}

return color;

}

#endregion

} }



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.