C# 使用sql动态设置图表系列';t使用Itextsharp.pdf以png图像形式下载时显示图表

C# 使用sql动态设置图表系列';t使用Itextsharp.pdf以png图像形式下载时显示图表,c#,sql,charts,C#,Sql,Charts,我不熟悉.net,因为我正在使用sql db以编程方式创建图表,因为我有两个按钮,一个用于搜索,另一个用于将图表下载到png图像。在“搜索”按钮中,我用程序创建了一系列图表,其中第二个按钮用于将图表下载到png格式,而这正是我被困数小时的地方 搜索方法如下: public void searchdataByCity() { try { string abc = locationDropDown.SelectedItem.ToSt

我不熟悉.net,因为我正在使用sql db以编程方式创建图表,因为我有两个按钮,一个用于搜索,另一个用于将图表下载到png图像。在“搜索”按钮中,我用程序创建了一系列图表,其中第二个按钮用于将图表下载到png格式,而这正是我被困数小时的地方

搜索方法如下:

 public  void searchdataByCity()
    {
        try
        {
            string abc = locationDropDown.SelectedItem.ToString();
            string query = "select max(title),count(title)as counts from indeed where city like '%" + abc + "%'  having count(title) > 1 union select max(title),count(title)as counts from mitula where city like '%" + abc + "%'  having count(title) > 1 union select max(title),count(title)as counts from trovoit where city like '%" + abc + "%'  having count(title) > 1 union select max(title),count(title)as counts from glassdoor where city like '%" + abc + "%'  having count(title) > 1";
            Series series1 = Chart2.Series["Series1"];
            con.Open();
            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataReader rdr = cmd.ExecuteReader();
            while(rdr.Read())
            {
                series1.Points.AddXY(rdr[0].ToString(),rdr[1].ToString());
            }


            con.Close();            
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.Write(e.Message);
        }
        finally
        {
            con.Close();
        }
    }
 protected void DownloadCustomizeData_Click(object sender, EventArgs e)
    {
        if (Session["fullname"] == null)
        {
            Response.Redirect("LoginForm.aspx");
        }
        else
        {

            download_chartImage(Chart2);
        }
    }
下载按钮如下:

 public  void searchdataByCity()
    {
        try
        {
            string abc = locationDropDown.SelectedItem.ToString();
            string query = "select max(title),count(title)as counts from indeed where city like '%" + abc + "%'  having count(title) > 1 union select max(title),count(title)as counts from mitula where city like '%" + abc + "%'  having count(title) > 1 union select max(title),count(title)as counts from trovoit where city like '%" + abc + "%'  having count(title) > 1 union select max(title),count(title)as counts from glassdoor where city like '%" + abc + "%'  having count(title) > 1";
            Series series1 = Chart2.Series["Series1"];
            con.Open();
            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataReader rdr = cmd.ExecuteReader();
            while(rdr.Read())
            {
                series1.Points.AddXY(rdr[0].ToString(),rdr[1].ToString());
            }


            con.Close();            
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.Write(e.Message);
        }
        finally
        {
            con.Close();
        }
    }
 protected void DownloadCustomizeData_Click(object sender, EventArgs e)
    {
        if (Session["fullname"] == null)
        {
            Response.Redirect("LoginForm.aspx");
        }
        else
        {

            download_chartImage(Chart2);
        }
    }
下载图片如下:

 private void download_chartImage(Chart chartimg)
    {
        Document Doc = new Document(PageSize.A4.Rotate());

        PdfWriter.GetInstance(Doc, Response.OutputStream);

        Doc.Open();

        using (MemoryStream memoryStream = new MemoryStream())

        {

            chartimg.SaveImage(memoryStream, ChartImageFormat.Png);
            iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(memoryStream.GetBuffer());

            img.ScalePercent(75f);

            Doc.Add(img);

            Doc.Close();



            Response.ContentType = "application/pdf";

            Response.AddHeader("content-disposition", "attachment;filename=Chart.pdf");

            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            Response.Write(Doc);

            Response.End();

        }

    }

有谁能帮我解决一下,我如何将chart2对象访问到下载按钮方法中,以png格式下载图表?代码答案将不胜感激。

您不想要PDF吗?下载_chartImage(Chart chartimg)是否应该将PNG而不是PDF返回给客户端?它将PNG下载为PDF格式。主要问题是,由于此问题,正在下载的图表不可见,如何将chart2对象访问到下载按钮中。