使用Asp.net在电子邮件中发送图表控制图像 [英] Send Chart Control Image in Email using Asp.net

查看:94
本文介绍了使用Asp.net在电子邮件中发送图表控制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过电子邮件发送图表



i尝试了这段代码,但是当我打开电子邮件时,chart.png有0K并且没有打开。



i want to send the chart via email

i tried this code but when i open the email chart.png has 0K and don't open.

protected void Page_Load(object sender, EventArgs e)
  {

     SqlConnection conn = new SqlConnection(constr);
        conn.Open();
        string sql = "SELECT * from CareerIntrestTestChart where EmailId='mailid'";

        //SqlCommand cmd = new SqlCommand(sql, conn);
        //SqlDataAdapter mySQLadapter = new SqlDataAdapter(cmd);

        //Chart1.DataSource = cmd;

        DataSet ds = new DataSet();
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataAdapter mySQLadapter = new SqlDataAdapter(cmd);
        mySQLadapter.Fill(ds);
        Chart1.DataSource = ds;
       
        Chart1.Titles.Add("Career Intrest Chart");
        // set series members names for the X and Y values 
        Chart1.Series["Series1"].XValueMember = "ChOption";
        Chart1.Series["Series1"].YValueMembers = "ChValues";
       
        // data bind to the selected data source
        Chart1.DataBind();

       
        cmd.Dispose();


        
      
        MailMessage mail = new MailMessage();
        mail.To.Add("MailId");
        mail.From = new MailAddress("MailId");
        mail.Subject = "Chart Control Image";
        mail.IsBodyHtml = true;
        mail.Body = "Heello";
       
         MemoryStream s = new MemoryStream();
         Chart1.SaveImage(s);
       
        Attachment attach= new Attachment(s, "chart.png");
        mail.Attachments.Add(attach);
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.host.com";
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new System.Net.NetworkCredential
             ("SendMailId", "Pwd");
        smtp.EnableSsl = false;
        smtp.Send(mail);
}

推荐答案





保存后将图像映射到内存流,将流的位置设置为零:

Hi,

After saving the image to the memory stream, set the position of the stream to zero:
MemoryStream s = new MemoryStream();
Chart1.SaveImage(s, ChartImageFormat.Png);
s.Position = 0;



我还建议在将图表保存为图像时选择PNG格式(否则它可能不会保存为PNG)。



希望这会有所帮助。


I also recommend to choose the PNG format when saving the chart as image (otherwise it's perhaps not saved as PNG).

Hope this helps.


这篇关于使用Asp.net在电子邮件中发送图表控制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆