Tuesday, July 12, 2005

C# Genrerate Thumbnail Source Code

protected void genThumbnailPhoto(string fileName, string path)
{
System.IO.FileInfo info = new System.IO.FileInfo(fileName);

using(Bitmap bitmap1 = new Bitmap(fileName, false))
{
int i= 0;
int TWidth;
int THeight;
if(bitmap1.Width >= 90 && bitmap1.Height >=90 )
{
if(bitmap1.Width > bitmap1.Height )
{
TWidth = 90;
i = bitmap1.Width/90;
THeight = bitmap1.Height / i;
}
else
{
THeight = 90;
i = bitmap1.Height/90;
TWidth = bitmap1.Width / i;
}
}
else
{
TWidth = bitmap1.Width;
THeight = bitmap1.Height;

}

using(System.Drawing.Image image =
System.Drawing.Image.FromFile(fileName))
using(Bitmap bitmap = new Bitmap(image, TWidth, THeight))
{
bitmap.Save(path + "/t__" + info.Name , image.RawFormat);
}

}


}

C# Return a web page in a string

public static string HttpConnection(string strUrl)
{
string strResponse = "";
try
{
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
System.Net.HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("big5");
System.IO.Stream stream = webResponse.GetResponseStream();
System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, encode);
strResponse = streamReader.ReadToEnd();

//-----release the reader and stream resource-----
streamReader.Close();
stream.Close();
}
catch(Exception exp)
{
throw exp;
}
return strResponse;
}

C# Get result of a web page with POST variables

public static string HttpPOSTConnection(string strUrl,string Param)
{
string strResponse = "";
try
{
string postData = Param;
byte[] byte1=System.Text.Encoding.Default.GetBytes(postData);
// Set the content type of the data being posted.
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
webRequest.Method = "POST";
webRequest.ContentType="application/x-www-form-urlencoded";
// Set the content length of the string being posted.
webRequest.ContentLength=byte1.Length;
System.IO.Stream newStream=webRequest.GetRequestStream();
newStream.Write(byte1,0,byte1.Length);

System.Net.HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("big5");
System.IO.Stream stream = webResponse.GetResponseStream();
System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.Default);
strResponse = streamReader.ReadToEnd();

//-----release the reader and stream resource-----
newStream.Close();
streamReader.Close();
stream.Close();
}
catch(Exception exp)
{
throw exp;
}
return strResponse;
}

C# Send mail, send web page by email

using System;
using System.Web.Mail;

namespace NEWS_RANK.util.net
{

public class Mail
{
private static string smtpserver = "127.0.0.1";


public static void sendMail(string To, string From, string Subject, string Body)
{
try
{
//-----Mail properties----
MailMessage mMessage = new MailMessage();
mMessage.From = From;
mMessage.To = To;
mMessage.Subject = Subject;
mMessage.Body = Body;
mMessage.BodyFormat = MailFormat.Html;
//-----Mail Send-----
SmtpMail.SmtpServer = smtpserver;
SmtpMail.Send(mMessage);
}
catch(Exception exp)
{
throw exp;
}
}


public static void sendMailHttpUrl(string To, string From, string Subject, string HttpUrl,string Query,string HttpMethod)
{
try
{
//-----Mail properties------
MailMessage mMessage = new MailMessage();
mMessage.From = From;
mMessage.To = To;
mMessage.Subject = Subject;
string mailContent = "";
if( HttpMethod != null && HttpMethod.ToLower().Equals("post") )
{
Uri baseUri = new Uri(HttpUrl);
string path = baseUri.GetLeftPart(System.UriPartial.Path);
string param = "";
if( Query != null && Query.Length > 1 )
param = Query;
mailContent = HttpUtil.HttpPOSTConnection(path,param);
}
else mailContent = HttpUtil.HttpConnection(HttpUrl);
mMessage.Body = mailContent;
mMessage.BodyFormat = MailFormat.Html;
//-----Mail Send-----
SmtpMail.SmtpServer = smtpserver;
SmtpMail.Send(mMessage);
}
catch(Exception exp)
{
throw exp;
}
}
}
}

Saving a web page to a file using C#

public static void HttpConnectionSaveToFile(string strUrl,string SavedPath)
{
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
System.Net.HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
System.IO.Stream stream = webResponse.GetResponseStream();
System.IO.BinaryReader streamReader = new System.IO.BinaryReader(stream);
byte[] b = streamReader.ReadBytes((int)webResponse.ContentLength);
//-----release the reader and stream resource-----
if (System.IO.File.Exists(SavedPath))
{
//Console.WriteLine("{0} already exists!", SavedPath);
System.IO.File.Delete(SavedPath);
}
System.IO.FileStream fs = new System.IO.FileStream(SavedPath, System.IO.FileMode.CreateNew);
// Create the writer for data.
System.IO.BinaryWriter w = new System.IO.BinaryWriter(fs);
// Write data to Test.data.
w.Write( b );
w.Close();
fs.Close();

streamReader.Close();
stream.Close();
}