Thursday, February 19, 2009

Capture Imgae from Url and Resize it

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Net;

public partial class CaptureImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int Height, Width;
string strImage = "";
Height = 200;
Width = 200;
Image imgPhotoVert = null;

strImage = http://images.play.com/covers/3472012x.jpg.Replace("../", "").Replace("~/", "");
Stream str = null;
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(strImage);
HttpWebResponse wRes = (HttpWebResponse)(wReq).GetResponse();
str = wRes.GetResponseStream();
imgPhotoVert = Image.FromStream(str);
str.Dispose();

Image imgPhoto = null;
imgPhoto = FixedSize(imgPhotoVert, Width, Height);
imgPhoto.Save(Response.OutputStream, ImageFormat.Jpeg);
imgPhoto.Dispose();
imgPhotoVert.Dispose();
}
static Image FixedSize(Image imgPhoto, int Width, int Height)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;

float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;

nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);

//if we have to pad the height pad both the top and the bottom
//with the difference between the scaled height and the desired height
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = (int)((Width - (sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = (int)((Height - (sourceHeight * nPercent)) / 2);
}

int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

Graphics grPhoto = Graphics.FromImage(bmPhoto);
//------- Experimenting -------------

//-----------------------------------
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);

grPhoto.Dispose();
return bmPhoto;
}
}

Websitescreenshot

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Reflection;
using mshtml;

///
/// Summary description for Websitescreenshot
///

///
namespace GetSiteThumpnail
{
public class Websitescreenshot
{

private int S_Height;
private int S_Width;
private int F_Height;
private int F_Width;
private string MyURL;
private WebBrowser MyBrowser;
private string URL;
private int Height;
private int Width;
private System.Drawing.Bitmap oThumbNail;

public int ScreenHeight
{
get { return S_Height; }
set { S_Height = value; }
}
public int ScreenWidth
{
get { return S_Width; }
set { S_Width = value; }
}
public int ImageHeight
{
get { return F_Height; }
set { F_Height = value; }
}
public int ImageWidth
{
get { return F_Width; }
set { F_Width = value; }
}
public string WebSite
{
get { return MyURL; }
set { MyURL = value; }
}
public Websitescreenshot(string WebSite, int ScreenWidth, int ScreenHeight, int ImageHeight, int ImageWidth)
{
this.WebSite = WebSite;
this.ScreenWidth = ScreenWidth;
this.ScreenHeight = ScreenHeight;
this.ImageHeight = ImageHeight;
this.ImageWidth = ImageWidth;
}
public Bitmap GetBitmap()
{
//WebPageBitmap Shot = new WebPageBitmap(this.WebSite, this.ScreenWidth, this.ScreenHeight);
WebPageBitmap(this.WebSite, this.ScreenWidth, this.ScreenHeight);
Thread m_thread = new Thread(new ThreadStart(GetIt));
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
m_thread.Join();
//Shot.GetIt();
Bitmap Pic = (Bitmap)oThumbNail; //DrawBitmap(this.ImageHeight, this.ImageWidth);
return Pic;
}

public void WebPageBitmap(string url, int width, int height)
{
this.Height = height;
this.Width = width;
this.URL = url;
}

public void GetIt()
{
MyBrowser = new WebBrowser();
MyBrowser.ScrollBarsEnabled = false;
//MyBrowser.Size = new Size(this.Width, this.Height);
MyBrowser.ClientSize = new Size(this.Width, this.Height);
MyBrowser.Navigate(this.URL);
MyBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
MyBrowser.Dispose();
}
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser MyBrowser = (WebBrowser)sender;
Bitmap myBitmap = new Bitmap(Width, Height);
Rectangle DrawRect = new Rectangle(0, 0, Width, Height);
MyBrowser.DrawToBitmap(myBitmap, DrawRect);
System.Drawing.Image imgOutput = myBitmap;
oThumbNail = new Bitmap(this.ImageWidth, this.ImageHeight, imgOutput.PixelFormat);
oThumbNail.SetResolution(imgOutput.HorizontalResolution, imgOutput.VerticalResolution);
Graphics g = Graphics.FromImage(oThumbNail);
g.Clear(Color.White);
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
Rectangle oRectangle = new Rectangle(0, 0, this.ImageWidth, this.ImageHeight);
g.DrawImage(imgOutput, oRectangle);
//try
//{
// return oThumbNail;
//}
//catch
//{
// throw;
//}
//finally
//{
imgOutput.Dispose();
//MyBrowser.Dispose();
//}

}


private void GetHtml()
{
WebBrowser web = new WebBrowser();
web.Navigate(this.URL);
IHTMLDocument2 doc = web.Document.DomDocument as IHTMLDocument2;
IHTMLElement2 img = doc.images;

}

}

}

Calling this class function in the given format:-
private void CaptureScreen(string url)
{
try
{
string path = null;
path = Server.MapPath("~/images/orderdeals/");
string picName = null;
Random RandomClass = new Random();
int RandomNumber = RandomClass.Next();
picName = "order-" + Request.QueryString["id"].ToString() + "-" + RandomNumber + ".jpg"; //Format order-dealid-RandomNumber;
//int width = Int32.Parse(txtWidth.Text);
//int height = Int32.Parse(txtHeight.Text);
Websitescreenshot screenshot = new Websitescreenshot(url, 1024, 800, 800, 1024);
//Bitmap bmp = screenshot.GetBitmap();
System.Drawing.Image img = screenshot.GetBitmap();
img.Save(path + picName, ImageFormat.Jpeg);
//bmp.Save(path + "google.jpg");
//imgDeal.ImageUrl = Page.ResolveUrl("~/processimage.aspx?FilePath=../images/orderdeals/" + picName + "&height=464&width=933");
Response.Write(picName);
}
catch (Exception ex)
{

}
}