v华北&&平原v 2022-04-21 13:14 采纳率: 33.3%
浏览 31
已结题

网上借鉴的例子;C# 写ASP.NET 出现乱码,请指导下。

img


private void CreateImage()
{
int height = 500, width = 700;
Bitmap p_w_picpath = new Bitmap(width, height);
// 创建 Graphics 类对象
Graphics g = Graphics.FromImage(p_w_picpath);

    try
    {
        // 清空图片背景色
        g.Clear(Color.White);
        Font font = new Font("Arial", 10, FontStyle.Regular);
        Font font1 = new Font(" 宋体 ", 20, FontStyle.Bold);
        LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, p_w_picpath.Width, p_w_picpath.Height),
        Color.Blue, Color.BlueViolet, 1.2f, true);
        g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
        // Brush brush1 = new SolidBrush(Color.Blue);
        g.DrawString(
        " 江苏省发改委2022年成绩统计柱状图 ", font1, brush, new PointF(70, 30));
        // 画图片的边框线
        g.DrawRectangle(new Pen(Color.Blue), 0, 0, p_w_picpath.Width - 1, p_w_picpath.Height - 1);
        Pen mypen = new Pen(brush, 1);
        // 绘制线条
        // 绘制横向线条
        int x = 100;
        for (int i = 0; i < 14; i++)
        {
            g.DrawLine(mypen, x, 80, x, 340);
            x = x + 40;
        }
        Pen mypen1 = new Pen(Color.Blue, 2);
        x = 60;
        g.DrawLine(mypen1, x, 80, x, 340);
        // 绘制纵向线条
        int y = 106;
        for (int i = 0; i < 9; i++)
        {
            g.DrawLine(mypen, 60, y, 620, y);
            y = y + 26;
        }
        g.DrawLine(mypen1, 60, y, 620, y);
        //x 轴
        String[] n = { " 第一期 ", " 第二期 ", " 第三期 ", " 第四期 ", " 上半年 ", " 下半年 ", " 全年统计 " };
        x = 78;
        for (int i = 0; i < 7; i++)
        {
            g.DrawString(n[i].ToString(), font, Brushes.Blue, x, 348); // 设置文字内容及输出位置
            x = x + 78;
        }
        //y 轴
        String[] m = {"250","225", "200", "175", "150", "125", "100", " 75"," 50", " 25", " 0"};
        y = 72;
        for (int i = 0; i < 10; i++)
        {
            g.DrawString(m[i].ToString(), font, Brushes.Blue, 25, y); // 设置文字内容及输出位置
            y = y + 26;
        }
        int[] Count1 = new int[7];
        int[] Count2 = new int[7];
        //SqlConnection Con = new SqlConnection("Server=(Local);Database=committeeTraining;Uid=sa;Pwd=**");
        //Con.Open();
        //string cmdtxt2 = "SELECT * FROM ##Count where Company='" + this.ddlTaget.SelectedItem.Text.Trim() + "'";
        //SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con);
        //DataSet ds = new DataSet();
        //da.Fill(ds);
        Count1[0] = 26;
        Count1[1] =68;
        Count1[2] = 35;
        Count1[3] =14;
        Count1[4] = Count1[0] + Count1[1];//上半年的通过数
        Count1[5] = Count1[2] + Count1[3];//下半年的通过数
        Count1[6] = Count1[4] + Count1[5];//全年通过数

        Count2[0] = 39;
        Count2[1] = 111;
        Count2[2] = 71;
        Count2[3] = 40;
        Count2[4] = Count2[0] + Count2[1];//上半年报名数
        Count2[5] = Count2[2] + Count2[3];//下半年报名数
        Count2[6] = Count2[4] + Count2[5];//全年报名数
        // 绘制柱状图 .
        x = 80;
        Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
        SolidBrush mybrush = new SolidBrush(Color.Red);
        SolidBrush mybrush2 = new SolidBrush(Color.Green);
        // 第一期
        g.FillRectangle(mybrush, x, 340 - Count1[0], 20, Count1[0]);
        g.DrawString(Count1[0].ToString(), font2, Brushes.Red, x, 340 - Count1[0] - 15);
        x = x + 20;
        g.FillRectangle(mybrush2, x, 340 - Count2[0], 20, Count2[0]);
        g.DrawString(Count2[0].ToString(), font2, Brushes.Green, x, 340 - Count2[0] - 15);

        // 第二期
        x = x + 60;
        g.FillRectangle(mybrush, x, 340 - Count1[1], 20, Count1[1]);
        g.DrawString(Count1[1].ToString(), font2, Brushes.Red, x, 340 - Count1[1] - 15);

        x = x + 20;
        g.FillRectangle(mybrush2, x, 340 - Count2[1], 20, Count2[1]);
        g.DrawString(Count2[1].ToString(), font2, Brushes.Green, x, 340 - Count2[1] - 15);

        // 第三期
        x = x + 60;
        g.FillRectangle(mybrush, x, 340 - Count1[2], 20, Count1[2]);
        g.DrawString(Count1[2].ToString(), font2, Brushes.Red, x, 340 - Count1[2] - 15);
        x = x + 20;
        g.FillRectangle(mybrush2, x, 340 - Count2[2], 20, Count2[2]);
        g.DrawString(Count2[2].ToString(), font2, Brushes.Green, x, 340 - Count2[2] - 15);
        // 第四期
        x = x + 60;
        g.FillRectangle(mybrush, x, 340 - Count1[3], 20, Count1[3]);
        g.DrawString(Count1[3].ToString(), font2, Brushes.Red, x, 340 - Count1[3] - 15);
        x = x + 20;
        g.FillRectangle(mybrush2, x, 340 - Count2[3], 20, Count2[3]);
        g.DrawString(Count2[3].ToString(), font2, Brushes.Green, x, 340 - Count2[3] - 15);
        // 上半年
        x = x + 60;
        g.FillRectangle(mybrush, x, 340 - Count1[4], 20, Count1[4]);
        g.DrawString(Count1[4].ToString(), font2, Brushes.Red, x, 340 - Count1[4] - 15);
        x = x + 20;
        g.FillRectangle(mybrush2, x, 340 - Count2[4], 20, Count2[4]);
        g.DrawString(Count2[4].ToString(), font2, Brushes.Green, x, 340 - Count2[4] - 15);
        // 下半年
        x = x + 60;
        g.FillRectangle(mybrush, x, 340 - Count1[5], 20, Count1[5]);
        g.DrawString(Count1[5].ToString(), font2, Brushes.Red, x, 340 - Count1[5] - 15);
        x = x + 20;
        g.FillRectangle(mybrush2, x, 340 - Count2[5], 20, Count2[5]);
        g.DrawString(Count2[5].ToString(), font2, Brushes.Green, x, 340 - Count2[5] - 15);
        // 全年
        x = x + 60;
        g.FillRectangle(mybrush, x, 340 - Count1[6], 20, Count1[6]);
        g.DrawString(Count1[6].ToString(), font2, Brushes.Red, x, 340 - Count1[6] - 15);
        x = x + 20;
        g.FillRectangle(mybrush2, x, 340 - Count2[6], 20, Count2[6]);
        g.DrawString(Count2[6].ToString(), font2, Brushes.Green, x, 340 - Count2[6] - 15);

        // 绘制标识
        Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Regular);
        g.DrawRectangle(new Pen(Brushes.Blue), 170, 400, 250, 50); // 绘制范围框
        g.FillRectangle(Brushes.Red, 270, 410, 20, 10); // 绘制小矩形
        g.DrawString(" 报名人数 ", font3, Brushes.Red, 292, 408);
        g.FillRectangle(Brushes.Green, 270, 430, 20, 10);
        g.DrawString(" 通过人数 ", font3, Brushes.Green, 292, 428);
         //System.IO.MemoryStream ms = new System.IO.MemoryStream();
   
         //  Pen p = new Pen ( Color.Black , 1 ) ;
        g.DrawRectangle(mypen1, 1, 1, 598, 248);
        p_w_picpath.Save ( Response . OutputStream , ImageFormat.Gif) ;
        //Response.ClearContent();
        //Response.ContentType = "p_w_picpath/Jpeg";
        //Response.BinaryWrite(ms.ToArray());
    }
    finally
    {
    //    g.Dispose();
    //    p_w_picpath.Dispose();
        ;
    }
}

// System.IO.MemoryStream ms = new
//System.IO.MemoryStream();
//p_w_picpath.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
//Response.ClearContent();
//Response.ContentType = "p_w_picpath/Jpeg";
//Response.BinaryWrite(ms.ToArray());

protected void Button1_Click(object sender, System.EventArgs e)
{
    CreateImage();
}
  • 写回答

2条回答 默认 最新

  • Null_Reference 2022-04-21 14:50
    关注
    
                    Response.AddHeader("Content-Disposition", "attachment;FileName=" + HttpUtility.UrlEncode("1.gif"));
                    Response.Flush();
                    Response.End();
    

    加上这一句

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月29日
  • 已采纳回答 4月21日
  • 创建了问题 4月21日

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程