Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending screenshot via C#

Tags:

c#

smtp

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("[email protected]", "Konu");
msg.Subject = konu;
msg.IsBodyHtml = true;
msg.Body = mesaj;
msg.BodyEncoding = System.Text.Encoding.GetEncoding(1254);
NetworkCredential guvenlikKarti = new  NetworkCredential("[email protected]", "*****");
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?

like image 899
MaxCoder88 Avatar asked Sep 23 '11 13:09

MaxCoder88


People also ask

How do you take a screenshot in C?

To take a screenshot of your whole screen, you can press the Print Screen (or PrtSc) button on your keyboard. To automatically save your screenshot to the Pictures folder on your computer, press the Windows + Print Screen buttons at the same time. Press the Print Screen key to copy an image of your whole screen.

How do I send a screenshot from my computer?

Using the PRINT SCREEN key Pressing PRINT SCREEN captures an image of your entire screen and copies it to the Clipboard in your computer's memory. You can then paste (CTRL+V) the image into a document, email message, or other file.


2 Answers

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");
like image 83
James Johnston Avatar answered Sep 21 '22 14:09

James Johnston


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);
    }
}
like image 23
Marco Avatar answered Sep 22 '22 14:09

Marco