Introduction: In this article I will explain how
to take automatic snapshot/screenshot of desktop or a particular open window
and email it as attachment using asp.net (C#, VB)
Description:
In the previous article I have explained How to take snapshot (screenshot) using asp.net (C#, VB), Upload multiple files and save their
pathto database using Fileupload control in asp.net and Create Temporary table dynamically and
bind to Gridview in asp.net.
In this example I
want to take the screenshot in every 1 minute. To implement this I add Timer
control to .aspx and set interval (60000).
Note:
- 1 second = 1000 Millisecond
Html Markup Of page:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="60000">
</asp:Timer>
Note:
- First of all we have to add the reference of System.Windows.Forms library.
C# Code:
using
System.Windows.Forms;
using
System.Drawing.Imaging;
using
System.Drawing;
using
System.Net.Mail;
public void SendEmail(string
path)
{
try
{
MailMessage
mail = new MailMessage("EmailSender", "Email
TO");
mail.Subject = "Screen Shot";
Attachment
objAttachment = new Attachment(path);
mail.Attachments.Add(objAttachment);
SmtpClient
smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("EmailId", "Password");
smtp.EnableSsl = true;
mail.IsBodyHtml = true;
smtp.Send(mail);
}
catch (Exception ex)
{
}
}
protected void Timer1_Tick(object
sender, EventArgs e)
{
Bitmap
bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics
graphics = Graphics.FromImage(bitmap as System.Drawing.Image);
graphics.CopyFromScreen(5, 5, 5, 5,
bitmap.Size);
string
path =("E:/ss/" + Guid.NewGuid() + "ScreenShot.png");
bitmap.Save(path);
SendEmail(path);
}
VB Code:
Imports
System.Drawing.Imaging
Imports
System.Drawing
Imports
System.Windows.Forms
Imports
System.Net.Mail
Public Sub SendEmail(ByVal
path As String)
Try
Dim
mail As New MailMessage("Email
Sender", "Email To")
mail.Subject = "Screen Shot"
Dim
objAttachment As New
Attachment(path)
mail.Attachments.Add(objAttachment)
Dim
smtp As New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.Port = 587
smtp.Credentials = New System.Net.NetworkCredential("EmailId",
"Password")
smtp.EnableSsl = True
mail.IsBodyHtml = True
smtp.Send(mail)
Catch
ex As Exception
Throw
ex
End Try
End Sub
Protected Sub Timer1_Tick(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles
Timer1.Tick
Dim
bitmap As New Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height)
Dim
graphics__1 As Graphics
= Graphics.FromImage(TryCast(bitmap, System.Drawing.Image))
graphics__1.CopyFromScreen(5, 5, 5, 5,
bitmap.Size)
Dim
path As String
= ("E:/ss/" & Convert.ToString(Guid.NewGuid())
& "ScreenShot.png")
bitmap.Save(path)
SendEmail(path)
End Sub
Is this article helpful for you?
If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.
Thanks
ReplyDeleteYou are welcome.. keep visiting for more asp.net article
DeleteHi can you maybe Email me the source code at brandonlblignaut@gmail.com
ReplyDeleteAs I am struggling with this at the moment.