6

I'm saving by capturing screenshot via that code.

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);

Then send this saved screenshot as e-mail:

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); 

I want to do this : How can I send a screenshot directly as e-mail via smtp protocol without saving?

Renatas M.
  • 11,694
  • 1
  • 43
  • 62
MaxCoder88
  • 2,374
  • 6
  • 41
  • 59

2 Answers2

7

Save the Bitmap to a Stream. Then attach the Stream to your mail message. Example:

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");
James Johnston
  • 9,264
  • 9
  • 48
  • 76
  • 3
    Remember to surround these calls with a using block so that you don't have memory leaks, or call the .Dispose() methods to free the memory. – Digicoder Sep 23 '11 at 13:56
  • Sorry, I was editing my answer and when I finally posted it I saw your is the same. Do you prefer me to delete mine? :) Anyway, +1 for you – Marco Sep 23 '11 at 13:57
  • Yes - Digicoder raises an important point. The original code could be improved by using "using" blocks over all IDisposable objects. I left it out for simplicity, and also because properly setting up "using" blocks would require revising all the original code. – James Johnston Sep 23 '11 at 13:58
2

Use this:

using (MemoryStream ms = new MemoryStream())
{
    Ekran.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    using (Attachment att = new Attachment(ms, "attach_name"))
    {
        ....
        client.Send(msg);
    }
}
Marco
  • 56,740
  • 14
  • 129
  • 152