This is to add two more functions: (on the fly from MemoryStream)
1- DOWNLOAD image: Browser will be forced to open "Save As" dialog box.
2- EMAIL image: Both in body and attachment.
The Image is the one produced by Capture ScreenShot article written by Mr. Mudassar:
Ref: http://www.aspsnippets.com/Articles/Capture-Screenshot-Snapshot-Image-of-Website-Web-Page-in-ASPNet-using-C-and-VBNet.aspx
HTML
<form id="form1" runat="server">
<asp:TextBox ID="txtUrl" runat="server" Text="" />
<asp:Button ID="Button1" Text="Capture" runat="server" OnClick="Capture" />
<br />
<asp:Image ID="imgScreenShot" runat="server" Height="500" Width="700" Visible="false" />
<br />
<asp:Button Id="btn_Download" Text="Download Image" runat="server" />
<asp:Button ID="btn_SendEmail" Text="Send Email" runat="server" />
Namespaces
Imports System
Imports System.Linq
Imports System.Web
Imports System.IO
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Forms
Imports System.Net.Mail
Imports System.Net
Imports System.Net.Mime
VB
Protected Sub btn_Download_Click(sender As Object, e As EventArgs) Handles btn_Download.Click
Dim bytes As Byte() = Convert.FromBase64String(imgScreenShot.ImageUrl.Split(",")(1))
Response.Clear()
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "image/png"
Response.AppendHeader("Content-Disposition", "attachment; filename=" + "abc.png")
Response.BinaryWrite(bytes)
Response.Flush()
Response.End()
End Sub
Protected Sub btn_SendEmail_Click(sender As Object, e As EventArgs) Handles btn_SendEmail.Click
Using mm As New MailMessage("from@YourDomain.com", "to@RecipientDomain.com")
'Image:
Dim contentAsBytes As Byte() = Convert.FromBase64String(imgScreenShot.ImageUrl.Split(",")(1))
'Memory:
Dim msToBody As New MemoryStream()
msToBody.Write(contentAsBytes, 0, contentAsBytes.Length)
msToBody.Seek(0, SeekOrigin.Begin) 'Set the position to the beginning of the stream.
'Email
mm.Subject = "The image"
Dim emailBody As String = "<html><body><br /><img src='cid:MudassarScreenShot' width='1024px' height='768px'></body></html>"
'Email image as html Body:
Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(emailBody, Nothing, "text/html")
Dim imageResource As New LinkedResource(msToBody, "image/png")
imageResource.TransferEncoding = Mime.TransferEncoding.Base64
imageResource.ContentId = "MudassarScreenShot"
htmlView.LinkedResources.Add(imageResource)
mm.AlternateViews.Add(htmlView)
'Email image as Attachment: (we'll need to stream again!)
Dim msToAttachment As New MemoryStream()
msToAttachment.Write(contentAsBytes, 0, contentAsBytes.Length)
msToAttachment.Seek(0, SeekOrigin.Begin)
Dim ct As ContentType = New ContentType(MediaTypeNames.Application.Octet)
ct.Name = "abc.png"
Dim attachment As Attachment = New Attachment(msToAttachment, ct)
mm.Attachments.Add(attachment)
'Sending
Dim smtp As SmtpClient = New SmtpClient("smtp.YourDomain.com")
Dim myCredentials As NetworkCredential = New NetworkCredential("AuthUser@YourDomain.com", "password")
smtp.Credentials = myCredentials
smtp.Send(mm)
'Dispose:
imageResource.Dispose()
'Feedback
ClientScript.RegisterStartupScript(GetType(String), "alert", "alert('Email sent.');", True)
End Using
End Sub
Namespaces
using System.IO;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
using System.Net.Mime;
C#
// Downloading the image
protected void DownLoadImage(object sender, EventArgs e)
{
byte[] bytes = Convert.FromBase64String(imgScreenShot.ImageUrl.Split(',')[1]);
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "image/png";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + "abc.png");
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
//Send the image as attachment
protected void SendEmail(object sender, EventArgs e)
{
using (MailMessage mm = new MailMessage("from@YourDomain.com", "to@RecipientDomain.com"))
{
//Image:
byte[] contentAsBytes = Convert.FromBase64String(imgScreenShot.ImageUrl.Split(',')[1]);
//Memory:
MemoryStream msToBody = new MemoryStream();
msToBody.Write(contentAsBytes, 0, contentAsBytes.Length);
msToBody.Seek(0, SeekOrigin.Begin);
//Set the position to the beginning of the stream.
//Email
mm.Subject = "The image";
string emailBody = "<html><body><br /><img src='cid:MudassarScreenShot' width='1024px' height='768px'></body></html>";
//Email image as html Body:
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(emailBody, null, "text/html");
LinkedResource imageResource = new LinkedResource(msToBody, "image/png");
imageResource.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
imageResource.ContentId = "MudassarScreenShot";
htmlView.LinkedResources.Add(imageResource);
mm.AlternateViews.Add(htmlView);
//Email image as Attachment: (we'll need to stream again!)
MemoryStream msToAttachment = new MemoryStream();
msToAttachment.Write(contentAsBytes, 0, contentAsBytes.Length);
msToAttachment.Seek(0, SeekOrigin.Begin);
ContentType ct = new ContentType(MediaTypeNames.Application.Octet);
ct.Name = "abc.png";
Attachment attachment = new Attachment(msToAttachment, ct);
mm.Attachments.Add(attachment);
//Sending
SmtpClient smtp = new SmtpClient("smtp.YourDomain.com");
NetworkCredential myCredentials = new NetworkCredential("AuthUser@YourDomain.com", "password");
smtp.Credentials = myCredentials;
smtp.Send(mm);
//Dispose:
imageResource.Dispose();
//Feedback
ClientScript.RegisterStartupScript(typeof(string), "alert", "alert('Email sent.');", true);
}
}
Acknowledgement:
To Mr. Mudassar Ahmad Khan: Thank you for writing the great article referenced above.
To Mr. Azim: Thank you for your contribution on this thread. Giving the code necessary for DOWNLOAD button. And helping me figuring out the MemoryStream issue that led me to develop the rest of the answer.
Regards;
MDarwish