// Use asp.net to draw a puzzle (which can be used for various voting procedures)
// Compared with asp, asp.net has more powerful functions. using gdi +, you can easily implement many graphics Functions that you previously could not do.
// First, create the library mess. mdb in c: \ and create the table title.
// Create two fields, title (char type) and point (int type)
// Very satisfied 281
// 297 satisfied
// Merge 166
// Not satisfied: 416
// I have also written a line chart and a bar chart. I am writing them all into a class. You can contact me if you need: mailto: ouyang76@263.net
<% @ Page Language = "C #" %>
<% @ Import namespace = "System. Data" %>
<% @ Import namespace = "System. Data. OleDb" %>
<% @ Import namespace = "System. Drawing" %>
<% @ Import namespace = "System. Drawing. Imaging" %>
<Script language = "c #" runat = "server">
Public void page_load (Object obj, EventArgs e)
{
// Specify the connection string as a constant.
Const String strconn = "Provider = Microsoft. Jet. OLEDB.4.0;" +
"Data Source = c: \ mess. mdb ";
OleDbConnection conn = new OleDbConnection (strconn );
Conn. Open ();
String SQL = "select * from title ";
OleDbCommand cmd = new OleDbCommand (SQL, conn );
DataSet ds = new DataSet ();
OleDbDataAdapter adapter1 = new OleDbDataAdapter (cmd );
Adapter1.Fill (ds );
Conn. Close ();
Float total = 0.0f, tmp;
Int iloop;
For (iloop = 0; iloop <ds. Tables [0]. Rows. Count; iloop ++)
{
Tmp = Convert. ToSingle (ds. Tables [0]. Rows [iloop] ["point"]); // converts it to a single precision, and it is impossible to vote for half a vote. You can also write it as Convert. ToInt32.
Total + = tmp;
}
// Response. Write (Convert. ToString (total ));
Font fontlegend = new Font ("verdana", 9), fonttitle = new Font ("verdana", 10, FontStyle. Bold); // set the Font
// Fonttitle indicates the font of the title.
Int width = 230; // white background width
Const int bufferspace = 15;
Int legendheight = fontlegend. Height * (ds. Tables [0]. Rows. Count + 1) + bufferspace;
Int titleheight = fonttitle. Height + bufferspace;
Int height = width + legendheight + titleheight + bufferspace; // the white background height.
Int pieheight = width;
Rectangle pierect = new Rectangle (0, titleheight, width, pieheight );
// Add various random colors
ArrayList colors = new ArrayList ();
Random rnd = new Random ();
For (iloop = 0; iloop <ds. Tables [0]. Rows. Count; iloop ++)
Colors. Add (new SolidBrush (Color. FromArgb (rnd. Next (255), rnd. Next (255), rnd. Next (255 ))));
Bitmap objbitmap = new Bitmap (width, height); // create a bitmap instance
// Bitmap objbitmap = new Bitmap (230,500); // create a bitmap instance
Graphics objgraphics = Graphics. FromImage (objbitmap );
Objgraphics. FillRectangle (new SolidBrush (Color. White), 0, 0, width, height); // draw a White background
Objgraphics. FillRectangle (new SolidBrush (Color. LightYellow), pierect); // draw a bright yellow background
// The following is a pie chart (several rows of rows are drawn)
Float currentdegree = 0.0f;
For (iloop = 0; iloop <ds. Tables [0]. Rows. Count; iloop ++)
{
Objgraphics. FillPie (SolidBrush) colors [iloop], pierect, currentdegree,
Convert. ToSingle (ds. Tables [0]. Rows [iloop] ["point"])/total * 360 );
Currentdegree + = Convert. ToSingle (ds. Tables [0]. Rows [iloop] ["point"])/total * 360;
}
// --- Generate the main title as follows
SolidBrush blackbrush = new SolidBrush (Color. Black );
String title = "this" Programmer base camp "introduced a supporting special publication. Are you satisfied with this special publication? ";
StringFormat stringFormat = new StringFormat ();
StringFormat. Alignment = StringAlignment. Center;
StringFormat. LineAlignment = StringAlignment. Center;
Objgraphics. DrawString (title, fonttitle, blackbrush,
New Rectangle (0, 0, width, titleheight), stringFormat );
// List fields and votes
Objgraphics. DrawRectangle (new Pen (Color. Black, 2), 0, height-legendheight, width, legendheight );
For (iloop = 0; iloop <ds. Tables [0]. Rows. Count; iloop ++)
{
Objgraphics. FillRectangle (SolidBrush) colors [iloop], 5, height-legendheight + fontlegend. Height * iloop + 5, 10, 10 );
Objgraphics. drawString (String) ds. tables [0]. rows [iloop] ["title"]) + "-" + Convert. toString (ds. tables [0]. rows [iloop] ["point"]), fontlegend, blackbrush,
20, height-legendheight + fontlegend. Height * iloop + 1 );
}
//
Objgraphics. DrawString ("total votes:" + Convert. ToString (total), fontlegend, blackbrush, 5, height-fontlegend. Height );
// The total height of the image-the Height of a row of font, that is, the height of the row of the base row (Height-fontlegend. height)
Response. ContentType = "image/jpeg ";
Objbitmap. Save (Response. OutputStream, ImageFormat. Jpeg );
// Objbitmap. Save ("myyyyyyyyyyy.jpg", ImageFormat. Jpeg); // output to the file
Objgraphics. Dispose ();
Objbitmap. Dispose ();
}
</Script>