work067

 

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work067.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>使用GDI+生成图片</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace work067
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
{
            //使用GDI+生成图片

            //实例化一个位图
            System.Drawing.Bitmap image1 = new System.Drawing.Bitmap(400, 100);
            //从位图对象创建图像
            System.Drawing.Graphics graphics1 = System.Drawing.Graphics.FromImage(image1);
            //黑色填充图像
            graphics1.Clear(System.Drawing.Color.Gray);
            //定义字体
            System.Drawing.Font font1 = new System.Drawing.Font("宋体", 16);
            //定义画刷
            System.Drawing.Brush brush1 = new System.Drawing.SolidBrush(System.Drawing.Color.White);
            //绘制文字
            graphics1.DrawString("虾米大王,验证码",font1,brush1,50,30);
            //释放图像对象
            graphics1.Dispose();
            //页面清空
            Response.Clear();
            //设置页面模式
            Response.ContentType = "image/pjpeg";
            //将图片保存至页面输出流中
            image1.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
            //释放图片
            image1.Dispose();
            //告知页面结束
            Response.End();
        }
    }
}


work068



webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work068.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>演示随机生成登录验证码</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>



webform1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace work068
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
{
            //演示随机生成登录验证码

            //验证码的字符集合
            string checkCodeString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            //集合的长度
            int length = checkCodeString.Length;
            //建立字体,粗体
            System.Drawing.Font font1 = new System.Drawing.Font("宋体", 24, System.Drawing.FontStyle.Bold);
            //预备空画刷
            System.Drawing.Brush brush1 = null;
            //预备画刷颜色
            System.Drawing.Color brushColor = new System.Drawing.Color();
            //保存最后生成的验证码字符串
            string checkCode = string.Empty;
            //单个验证码
            string code = string.Empty;
            //建立位图
            System.Drawing.Bitmap image1 = new System.Drawing.Bitmap(100, 40);
            //从位图获取图像对象
            System.Drawing.Graphics graphic1 = System.Drawing.Graphics.FromImage(image1);
            //背景白色
            graphic1.Clear(System.Drawing.Color.White);
            //随机数
            Random random1 = new Random();
            //4位
            for (int i = 0; i < 4; i++)
            {
                //取模运算
                int current = random1.Next(DateTime.Now.Millisecond) % length;
                //验证码字符串集合中截取字符
                code = checkCodeString.Substring(current, 1);
                //累加至验证码字符串中
                checkCode += code;
                //随机生成颜色
                brushColor = System.Drawing.Color.FromArgb(random1.Next(255), random1.Next(255), random1.Next(255));
                //给画刷加颜色
                brush1 = new System.Drawing.SolidBrush(brushColor);
                //图像对象中写入字符串,字符起点20像素宽,中间5像素隔开
                graphic1.DrawString(code, font1, brush1, i * 20 + 5, 10);

            }

            //网页清空
            Response.Clear();
            //设置网页模式
            Response.ContentType = "image/pjpeg";
            //图片保存至网页流中
            image1.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
            //最后的验证码保存至会话层中
            Session["CheckCode"] = checkCode;
            //释放资源
            graphic1.Dispose();
            image1.Dispose();
            Response.End();
        }
    }
}


webform2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="work068.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>在登录界面中使用验证码</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table border="0">
            <tr>
                <td>用户名</td>
                <td>
                    <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="必填" ControlToValidate="txtUserName" Display="Dynamic"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>密码</td>
                <td>
                    <asp:TextBox ID="txtPwd" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="必填" ControlToValidate="txtPwd" Display="Dynamic"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>验证码</td>
                <td>
                    <asp:TextBox ID="txtCheckCode" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="必填" ControlToValidate="txtCheckCode" Display="Dynamic"></asp:RequiredFieldValidator>
                    <img src="WebForm1.aspx" width="100" height="40" style="cursor:pointer" onclick="javascript:this.src='WebForm1.aspx?id=' + Math.random() * 10000 " alt="点击刷新验证码"/>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="Button1" runat="server" Text="登录" OnClick="Button1_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>



webform2.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace work068
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
{
            Page.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;
            txtUserName.Text = "虾米大王";
            txtPwd.Text = "123";
        }

        protected void Button1_Click(object sender, EventArgs e)
{
            string checkCode = Session["CheckCode"].ToString();
            if (checkCode.ToUpper() == txtCheckCode.Text.ToUpper())
            {
                Response.Write("<script>window.alert('验证码正确,登录成功');</script>");
            }
            else
            {
                Response.Write("<script>window.alert('验证码错误,重新登录');</script>");
            }
        }
    }
}


work069



webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work069.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>使用微软office办公软件附带的OWC绘制报表的示例</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table border="1">
            <tr>
                <td>柱状图</td>
                <td>饼图</td>
                <td>折线图</td>
            </tr>
            <tr>
                <td>
                    <asp:Image ID="imgChartTypeColumnClustered" runat="server" />
                </td>
                <td>
                    <asp:Image ID="imgPie" runat="server" />
                </td>
                <td>
                    <asp:Image ID="imgLine" runat="server" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>



webform1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Owc11;

namespace work069
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        //首先去下载Microsoft.Office.Interop.Owc11.dll这个动态库,然后在项目中引用这个库,记得使用using包含进来。
        protected void Page_Load(object sender, EventArgs e)
{
            ShowChartTypeColumnClustered();
            ShowChartTypePie();
            ShowChartTypeLine();
        }

        /// <summary>
        /// 折线图
        /// </summary>
        private void ShowChartTypeLine()
{
            //创建图形容器对象
            Microsoft.Office.Interop.Owc11.ChartSpace chartSpace1 = new ChartSpace();
            //添加图形对象
            Microsoft.Office.Interop.Owc11.ChChart chChart1 = chartSpace1.Charts.Add(0);
            //设定类型
            chChart1.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeLine;
            //添加系列对象           
            Microsoft.Office.Interop.Owc11.ChSeries chSeries1 = chChart1.SeriesCollection.Add(0);
            //设定系列的名字
            chSeries1.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimSeriesNames,
                (int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral,
                "1\t2\t3\t4");
            //设定分类
            chSeries1.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories,
                (int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral,
                "1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11\t12");
            //设定值
            chSeries1.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues,
                (int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral,
                "300\t100\t643\t200\t300\t100\t643\t200\t300\t100\t643\t200");
            
            //
            chChart1.Axes[0].HasTitle = true;
            chChart1.Axes[0].Title.Caption = "季度";
            //
            chChart1.Axes[1].HasTitle = true;
            chChart1.Axes[1].Title.Caption = "销量";
            //折现顶点的标签
            chChart1.SeriesCollection[0].DataLabelsCollection.Add();
            chChart1.SeriesCollection[0].DataLabelsCollection[0].HasValue = true;

            //生成文件名
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "Line.jpg";
            //取得服务器路径,生成图片文件保存路径
            string filePath = Server.MapPath("~/images/") + fileName;
            //导出图片
            chartSpace1.ExportPicture(filePath, "jpg", 400, 400);
            //设置显示
            imgLine.ImageUrl = "../images/" + fileName;

        }

        /// <summary>
        /// 生成饼图
        /// </summary>
        private void ShowChartTypePie()
{
            //创建图片容器对象
            Microsoft.Office.Interop.Owc11.ChartSpace space1 = new ChartSpace();
            //添加图形对象
            Microsoft.Office.Interop.Owc11.ChChart chart1 = space1.Charts.Add(0);
            //设定类型,饼图
            chart1.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypePie;
            //图形对象边框颜色
            chart1.Border.Color = "Red";
            //图形对象是否有标题
            chart1.HasTitle = true;
            //标题名称
            chart1.Title.Caption = "手机上网方式统计";
            //标题字体大小
            chart1.Title.Font.Size = 12;
            //标题粗体
            chart1.Title.Font.Bold = true;
            //标题颜色
            chart1.Title.Font.Color = "Blue";
            //图形底部分类名称
            chart1.HasLegend = true;
            //分类的字体大小
            chart1.Legend.Font.Size = 10;
            //位于底部
            chart1.Legend.Position = Microsoft.Office.Interop.Owc11.ChartLegendPositionEnum.chLegendPositionBottom;
            //添加系列
            Microsoft.Office.Interop.Owc11.ChSeries series1 = chart1.SeriesCollection.Add(0);
            //设定值
            series1.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues,
                (int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral,
                "135\t556\t1120\t634");
            //设定目录名称
            series1.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories,
                (int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral,
                "其他\t联通4G\t电信4G\t移动4G");
            //
            series1.DataLabelsCollection.Add();
            //是否显示百分比
            series1.DataLabelsCollection[0].HasPercentage = true;
            //文件名
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "Pie.jpg";
            //文件路径
            string filePath = Server.MapPath("~/images/") + fileName;
            //导出图片
            space1.ExportPicture(filePath, "jpg", 400, 400);
            //显示图片
            imgPie.ImageUrl = "../images/" + fileName;
        }

        /// <summary>
        /// 生成柱状图
        /// </summary>
        private void ShowChartTypeColumnClustered()
{
            Microsoft.Office.Interop.Owc11.ChartSpace space1 = new ChartSpace();

            Microsoft.Office.Interop.Owc11.ChChart chart1 = space1.Charts.Add(0);

            chart1.Type = ChartChartTypeEnum.chChartTypeColumnClustered;

            chart1.Border.Color = "Red";

            chart1.HasTitle = true;

            chart1.Title.Caption = "虾米大王用OWC测试";

            chart1.Title.Font.Size = 12;

            chart1.Title.Font.Color = "Blue";

            chart1.HasLegend = true;

            chart1.Legend.Position = ChartLegendPositionEnum.chLegendPositionBottom;

            chart1.Axes[0].HasTitle = true;

            chart1.Axes[0].Title.Caption = "人物";

            chart1.Axes[1].HasTitle = true;

            chart1.Axes[1].Title.Caption = "数量";

            Microsoft.Office.Interop.Owc11.ChSeries series1 = chart1.SeriesCollection.Add(0);

            series1.SetData(ChartDimensionsEnum.chDimSeriesNames,
                (int)ChartSpecialDataSourcesEnum.chDataLiteral,
                "战斗力");

            series1.SetData(ChartDimensionsEnum.chDimValues,
                (int)ChartSpecialDataSourcesEnum.chDataLiteral,
                "800\t998\t880\t234");

            series1.SetData(ChartDimensionsEnum.chDimCategories,
                (int)ChartSpecialDataSourcesEnum.chDataLiteral,
                "卡卡罗特\t龟仙人\t孙悟空\t孙悟饭");

            series1.DataLabelsCollection.Add();
            series1.DataLabelsCollection[0].HasValue = true;

            series1 = chart1.SeriesCollection.Add(1);

            series1.SetData(ChartDimensionsEnum.chDimSeriesNames,
                (int)ChartSpecialDataSourcesEnum.chDataLiteral,
                "幽默指数");

            series1.SetData(ChartDimensionsEnum.chDimValues,
                (int)ChartSpecialDataSourcesEnum.chDataLiteral,
                "999\t580\t320\t960");

            series1.DataLabelsCollection.Add();
            series1.DataLabelsCollection[0].HasValue = true;

            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "ColumnClustered.jpg";

            string filePath = Server.MapPath("~/images/") + fileName;

            space1.ExportPicture(filePath, "jpg", 400, 400);

            imgChartTypeColumnClustered.ImageUrl = "../images/" + fileName;

        }


    }
}