Figure 2 Page_Load Event Handler
void Page_Load(Object sender, EventArgs e)
{
// Create a Bitmap instance that's 468x60
const int width = 486, height = 60;
Bitmap objBitmap = new Bitmap(width, height);
Graphics objGraphics = Graphics.FromImage(objBitmap);
// Create a black background for the border
objGraphics.FillRectangle(new SolidBrush(Color.Black), 0, 0, width, height);
// Create a LightBlue background
objGraphics.FillRectangle(new SolidBrush(Color.LightBlue), 5, 5,
width - 10, height - 10);
// Create the advertising pitch
String adPitch = "Subscribe to MSDN Magazine!";
// Specify the font and alignment
Font fontBanner = new Font("Verdana", 16, FontStyle.Bold |
FontStyle.Italic);
// center align the advertising pitch
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
// Draw the advertising pitch
objGraphics.DrawString(adPitch, fontBanner, new SolidBrush(Color.Black),
new Rectangle(0, 0, width, height), stringFormat);
}
Figure 3 Display Image Saved to File System
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script language="C#" runat="server">
void CreateMSDNBanner(String strFilename)
{
// Create a Bitmap instance that's 468x60, and a Graphics instance
const int width = 486, height = 60;
Bitmap objBitmap = new Bitmap(width, height);
Graphics objGraphics = Graphics.FromImage(objBitmap);
// Create a black background for the border
objGraphics.FillRectangle(new SolidBrush(Color.Black), 0, 0, width, height);
// Create a LightBlue background
objGraphics.FillRectangle(new SolidBrush(Color.LightBlue), 5, 5,
width - 10, height - 10);
// Create the advertising pitch
Font fontBanner = new Font("Verdana", 16, FontStyle.Bold |
FontStyle.Italic);
StringFormat stringFormat = new StringFormat();
// center align the advertising pitch
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
String adPitch = "Subscribe to MSDN Magazine!";
objGraphics.DrawString(adPitch, fontBanner, new
SolidBrush(Color.Black), new Rectangle(0, 0, width, height),
stringFormat);
// Save the image to a file
objBitmap.Save(strFilename, ImageFormat.Jpeg);
// clean up...
objGraphics.Dispose();
objBitmap.Dispose();
}
void Page_Load(Object sender, EventArgs e)
{
// Create the MSDN Magazine Image
CreateMSDNBanner("C:\\Inetpub\\wwwroot\\MSDN\\banner.jpg");
}
</script>
<html><body>
<-- We can show the banner any place in the HTML with a
simple img tag...-->
<p align="center">
<a href="https://msdn.microsoft.com/msdnmagazine/">
<img src="/MSDN/banner.jpg" border="0" alt="Subscribe to MSDN
Today!"> </a></p>
<h1>BlahBlahBlah</h1>
BlahBlahBlah
<p align="center">
<a href="https://msdn.microsoft.com/msdnmagazine/">
<img src="/MSDN/banner.jpg" border="0" alt="Subscribe to MSDN Today!"></a></p>
</body></html>
Figure 5 Display Image Streamed to Browser
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
// Since we are outputting a Jpeg, set the ContentType appropriately
Response.ContentType = "image/jpeg"; // Create a Bitmap instance that's 468x60, and a Graphics instance
const int width = 486, height = 60;
Bitmap objBitmap = new Bitmap(width, height);
Graphics objGraphics = Graphics.FromImage(objBitmap);
// Create a black background for the border
objGraphics.FillRectangle(new SolidBrush(Color.Black), 0, 0, width, height);
// Create a LightBlue background
objGraphics.FillRectangle(new SolidBrush(Color.LightBlue), 5, 5,
width - 10, height - 10);
// Create the advertising pitch
Font fontBanner = new Font("Verdana", 16, FontStyle.Bold |
FontStyle.Italic);
StringFormat stringFormat = new StringFormat();
// center align the advertising pitch
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
String adPitch = "Subscribe to MSDN Magazine!";
objGraphics.DrawString(adPitch, fontBanner, new SolidBrush(Color.Black),
new Rectangle(0, 0, width, height), stringFormat);
// Save the image to the OutputStream
objBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
// clean up...
objGraphics.Dispose();
objBitmap.Dispose();
}
</script>
Figure 6 HTML Page Displaying Advertising Banner
<!-- We can show the banner any place in the HTML with a
simple img tag... --!>
<p align="center">
<a href="https://msdn.microsoft.com/msdnmagazine/">
<img src="/MSDN/msdnBannerOption2.aspx" border="0" alt="Subscribe
to MSDN Today!"></a></p>
<h1>BlahBlahBlah</h1>
BlahBlahBlah
<p align="center">
<a href="https://msdn.microsoft.com/msdnmagazine/">
<img src="/MSDN/msdnBannerOption2.aspx" border="0" alt="Subscribe
to MSDN Today!"></a></p>
Figure 7 Filling the DataSet
void CreatePieChart(String tableName, String dataColumnName,
String labelColumnName, String title, int width)
{
// We need to connect to the database and grab information for the
// particular columns for the particular table
// 1. Create a connection
const String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C:\\MSDN\\GrocerToGo.mdb";
OleDbConnection myConnection = new OleDbConnection(connString);
myConnection.Open();
// 2. Create a command object for the query
String sql = "SELECT [" + dataColumnName + "], [" + labelColumnName +
"] FROM " + tableName;
OleDbCommand myCommand = new OleDbCommand(sql, myConnection);
// 3. Create/Populate the DataSet
DataSet ds = new DataSet();
OleDbDataAdapter myAdapter = new OleDbDataAdapter(myCommand);
myAdapter.Fill(ds);
// close the connection
myConnection.Close();
Figure 8 Drawing the Pie Chart
float currentDegree = 0.0F;
// Create a Bitmap instance
Bitmap objBitmap = new Bitmap(width, height);
Graphics objGraphics = Graphics.FromImage(objBitmap);
SolidBrush blackBrush = new SolidBrush(Color.Black);
// Put a white background in
objGraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width,
height);
for (iLoop = 0; iLoop < ds.Tables[0].Rows.Count; iLoop++)
{
objGraphics.FillPie((SolidBrush) colors[iLoop], pieRect,
currentDegree,
Convert.ToSingle(ds.Tables[0].Rows[iLoop][dataColumnName]) /
total * 360);
// increment the currentDegree
currentDegree += Convert.ToSingle(ds.Tables[0].Rows
[iLoop][dataColumnName]) / total * 360;
}
Figure 9 Displaying the Legend
// Create the legend
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][labelColumnName]) + " - " +
Convert.ToString(ds.Tables[0].Rows[iLoop][dataColumnName]),
fontLegend, blackBrush,
20, height - legendHeight + fontLegend.Height * iLoop + 1);
}
// display the total
objGraphics.DrawString("Total: " + Convert.ToString(total), fontLegend,
blackBrush, 5, height - fontLegend.Height - 5);
Figure 10 Using CreatePieChart
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script language="C#" runat="server">
// Insert CreatePieChart function here�
void Page_Load(Object sender, EventArgs e)
{
CreatePieChart("Products", "UnitPrice", "ProductName",
"Price Breakdown", 300);
}
</script>
|