There are not many classes involved in drawing images. If you want to draw images, especially those dynamically changing data sources, controlling the coordinates is critical.
This demo uses the Oracle database as the Data source:
Create Table ivan_test
(
Major varchar2 (50 ),
Grade number,
ID number (10)
)
Add in HTML:
<Asp: button id = "btncolumn" runat = "server" text = "drawcolumn" onclick = "btncolumn_click"/> <br/>
<Asp: Image id = "imgcolumn" runat = "server"/>
CS :() protected void btncolumn_click (Object sender, eventargs E)
...{
Bitmap IMG = new Bitmap (300,300 );
Graphics G = graphics. fromimage (IMG );
Stringformat drawformat = new system. Drawing. stringformat (stringformatflags. directionvertical );
Stringformat drawformat1 = new system. Drawing. stringformat (stringformatflags. displayformatcontrol );
G. Clear (colortranslator. fromhtml ("# f0f0f0 "));
G. drawstring ("student grade column Chart", new font ("Arial", 1, fontstyle. Bold), brushes. Black, 100, 0, drawformat1 );
// Get datasource
String SQL = "select * From ivan_test ";
Dal. isdapp01 cdal = New DAL. isdapp01 ();
Dataset DS = cdal. executequery (SQL );
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
...{
Int grade = convert. toint32 (Ds. Tables [0]. Rows [I] [1]); // grade
String major = Ds. Tables [0]. Rows [I] [0]. tostring ();
// Pay attention to coordinate control
G. fillrectangle (New solidbrush (getcolor (I), (I * 35) + 15,150-grade, 20,
Grade + 50 );
G. drawrectangle (new pen (color. Black), (I * 35) + 15,150-grade, 20,
Grade + 50 );
G. drawstring (Major, new font ("Arial", 12, fontstyle. Bold), brushes. Red, (I * 35) + 20,200, drawformat );
G. drawstring (grade. tostring (), new font ("Arial", 12, fontstyle. Bold), brushes. Red, (I * 35) + 20,130-grade );
}
// Show chart
String filepath = server. mappath ("Images") + "//" + "column.jpg ";
If (file. exists (filepath ))
...{
File. Delete (filepath );
}
IMG. Save (filepath, imageformat. JPEG );
IMG. Dispose ();
G. Dispose ();
This. imgcolumn. imageurl = filepath;
}