Sending a screenshot via C #

I save by taking a screenshot with this 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 email this saved screenshot:

 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 email via smtp without saving?

+6
source share
3 answers

Save bitmap to stream. Then attach Stream to your email 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"); 
+7
source

You need to pin 64 encoding and create a MIME application. Cm:

Configuring the mail application and its encoding content transfer (BlackBerry problem)

+2
source

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); } } 
+2
source

Source: https://habr.com/ru/post/897898/


All Articles