Saturday, December 1, 2007

Get download page source of web-page into your .Net code

Here I’m downloading the page source of yahoo page and display in my page.

string strurl = "";

strurl = "http://www.yahoo.com";

StringBuilder sb=new StringBuilder();

try

{

System.Net.HttpWebRequest webreq = (System.Net.HttpWebRequest)System.Net.WebRequest.
Create(strurl);

System.Net.HttpWebResponse webres = (System.Net.HttpWebResponse)webreq.GetResponse();

StreamReader sr = new StreamReader(webres.GetResponseStream(), Encoding.Default);

while ((temp = sr.ReadLine()) != null)

{

sb.Append(temp + "\n\r");

}

string htmlcontent = sb.ToString();

Response.Write(htmlcontent);

}

catch (Exception ex)

{

Response.Write(ex.Message);

}

Image manipulation in ASP.NET: Loading & Resizing

How to load an image from your system and return it to the system or browser using the correct mime type by detecting the image format of the loaded file. You can also resize the image making a simplistic thumbnail.

Here I use File-browser for uploading image and get the size of the image to be resulted one. I save in physical drive.

if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0))

{

string fn = FileUpload1.PostedFile.FileName;

try

{

originalimg = System.Drawing.Image.FromFile(fn);

}

catch (Exception ex)

{

Response.Write(ex.Message);

}

if (ddlHeight.SelectedValue != "0")

{

height = Convert.ToInt32(ddlHeight.SelectedValue);

}

else

{

height = 100;

}

if (ddlWidth.SelectedValue != "0")

{

width = Convert.ToInt32(ddlWidth.SelectedValue);

}

else

{

width = 100;

}


thumb = originalimg.GetThumbnailImage(width, height, null, inp);


//Response.ContentType = "image/jpeg"; //Image Type

//thumb.Save(Response.OutputStream, ImageFormat.Jpeg); //It's going to display ur image in page itself


thumb.Save("D:\\Images\\abc.jpg", ImageFormat.Jpeg); // to save image in location


originalimg.Dispose();

thumb.Dispose();

}