通过C#发送屏幕截图

前端之家收集整理的这篇文章主要介绍了通过C#发送屏幕截图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
通过该代码捕获屏幕截图来保存.
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,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协议直接发送屏幕截图作为电子邮件而不保存?

解决方法

将位图保存到流.然后将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");

猜你在找的C#相关文章