2011-09-23 18 views
6

Sto salvando catturando screenshot tramite quel codice.Invio dello screenshot tramite 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, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy); 
Ekran.Save("screen.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 

poi inviare questa schermata salvata come 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); 

io voglio fare questo: Come posso inviare uno screenshot direttamente come e-mail tramite il protocollo smtp senza salvare?

risposta

7

Salva la bitmap su un flusso. Quindi collega il flusso al tuo messaggio di posta. Esempio:

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"); 
+3

Ricordarsi di circondare queste chiamate con un blocco utilizzando in modo che non si dispone di perdite di memoria, o chiamare il .Dispose() per liberare il memoria. – Digicoder

+0

Scusa, stavo modificando la mia risposta e quando finalmente l'ho pubblicata ho visto che la tua è la stessa cosa. Preferisci che elimini il mio? :) Ad ogni modo, +1 per te – Marco

+0

Sì - Il digitalizzatore solleva un punto importante. Il codice originale potrebbe essere migliorato usando i blocchi "using" su tutti gli oggetti IDisposable. L'ho lasciato fuori per semplicità, e anche perché impostare correttamente i blocchi "using" richiederebbe la revisione di tutto il codice originale. –

2

Utilizzare questa:

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

Non dimenticare che l'allegato è usa e getta ... –

+0

@James: sì, hai ragione :) – Marco