通过C#发送屏幕截图

时间:2011-09-23 13:47:06

标签: c# smtp

我通过使用该代码捕获屏幕截图来保存。

Graphics Grf;
Bitmap Ekran = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppPArgb);
Grf = Graphics.FromImage(Ekran);
Grf.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
Ekran.Save("screen.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

然后将此保存的屏幕截图作为电子邮件发送:

SmtpClient client = new SmtpClient();
MailMessage msg = new MailMessage();
msg.To.Add(kime);
if (dosya != null)
{
   Attachment eklenecekdosya = new Attachment(dosya);
   msg.Attachments.Add(eklenecekdosya);
}
msg.From = new MailAddress("aaaaa@xxxx.com", "Konu");
msg.Subject = konu;
msg.IsBodyHtml = true;
msg.Body = mesaj;
msg.BodyEncoding = System.Text.Encoding.GetEncoding(1254);
NetworkCredential guvenlikKarti = new  NetworkCredential("bbbb@bbbb.com", "*****");
client.Credentials = guvenlikKarti;
client.Port = 587;
client.Host = "smtp.live.com";
client.EnableSsl = true;
client.Send(msg); 

我想这样做:如何通过smtp协议直接发送截图作为电子邮件而不保存?

3 个答案:

答案 0 :(得分:7)

将位图保存到流中。然后将Stream附加到您的邮件消息。例如:

System.IO.Stream stream = new System.IO.MemoryStream();
Ekran.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Position = 0;
// later:
Attachment attach = new Attachment(stream, "MyImage.jpg");

答案 1 :(得分:2)

您需要对64进行编码并创建MIME附件。参见:

Mail Attachment and its Content Transfer Encoding setting (BlackBerry problem)

答案 2 :(得分:2)

使用此:

using (MemoryStream ms = new MemoryStream())
{
    Ekran.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    using (Attachment att = new Attachment(ms, "attach_name"))
    {
        ....
        client.Send(msg);
    }
}