VS.NET 2010 (and above) Users Click Here   

HOMECONTACT PRODUCTS DOWNLOADS PURCHASE TESTIMONIALS FORUMS COMPANY CONTACT
Home
Products
Downloads
Purchase
Licensing
Licensing FAQ
Software Updates
Support Forums
Testimonials
Feature Requests
Guarantee
About Us
Contact Us
Hosting Companies
Privacy Policy
   
Shopping Cart



aspNetMHT: Replacing images in the MHT document

by Dave 4. March 2011 15:45

I recently had an email request, asking if aspNetMHT could be used to switch out images.

In this particular instance, the customer wanted to convert a Url to a MHT document. However, the images were larger than desired. So, the requestor asked if aspNetMHT would allow images to be swapped out.

The following code example demonstrates this capability. In this example, we are going to examine the content-location value of each MHTPart. For simplicity sake, we are simply going to find all .png images, and swap them out with an image named "sample.png". In a production scenario, we would make some logic based decisions when swapping out the images.

Here is the code.


C#

static void Example()
{
	string url = "http://www.google.com";

	MHT m = new MHT();

	//enable logging for any troubleshooting
	m.Logger= new MHTLog("c:\\temp\\mht.log");
	m.Logger.Enabled =true;
	m.Logger.Overwrite=true;
	m.Ignore404=true;

	m.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";


	m.LoadUrl(url);
	m.Parse();

	ReplaceImages( m );

	m.SaveToFile("c:\\temp\\");

}


static void ReplaceImages(MHT m)
{
	MHTPartCollection parts = m.RetrieveAllParts();

	foreach( MHTPart part in parts )
	{
		if( part.ContentLocation != null )
		{
			string cl = part.ContentLocation.Value;
			if( cl != null )
			{
				cl = cl.ToLower();
				//as a simple test, replace all .png images
				if( cl.EndsWith(".png") )
				{
					//read in the replacement image
					FileStream fs = File.OpenRead("c:\\temp\\sample.png");
					byte[] data = new byte[ fs.Length ];
					fs.Read( data, 0, data.Length );
					fs.Close();
					part.Data = data;
				}
			}
		}
	}
}


VB.NET

Shared Sub Example()
   Dim url As String = "http://www.google.com"
   
   Dim m As New MHT()
   
   'enable logging for any troubleshooting
   m.Logger = New MHTLog("c:\temp\mht.log")
   m.Logger.Enabled = True
   m.Logger.Overwrite = True
   m.Ignore404 = True
   
   m.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)"
   
   
   m.LoadUrl(url)
   m.Parse()
   
   ReplaceImages(m)
   
   m.SaveToFile("c:\temp\")
End Sub 'Example
 


Shared Sub ReplaceImages(m As MHT)
   Dim parts As MHTPartCollection = m.RetrieveAllParts()
   
   Dim part As MHTPart
   For Each part In  parts
      If Not (part.ContentLocation Is Nothing) Then
         Dim cl As String = part.ContentLocation.Value
         If Not (cl Is Nothing) Then
            cl = cl.ToLower()
            'as a simple test, replace all .png images
            If cl.EndsWith(".png") Then
               'read in the replacement image
               Dim fs As FileStream = File.OpenRead("c:\temp\sample.png")
               Dim data(fs.Length) As Byte
               fs.Read(data, 0, data.Length)
               fs.Close()
               part.Data = data
            End If
         End If
      End If
   Next part
End Sub 'ReplaceImages


As always, if anyone has any questions, let me know.

Thanks!
Dave Wanta

 

Comments are closed

 

 

Testimonial

Thanks for the quick response – your support is the greatest. "

D. Simmons

Read more testimonials
ListNanny aspNetDNS aspNetEmail aspNetPOP3 aspNetMX IPMuncher aspNetMIME aspNetPING aspNetTraceRoute aspNetIMAP aspNetMHT