Sunday, September 26, 2010

How to create a good quality thumbnail from posted image file

public void SaveImage(object sender, EventArgs e) 
{ 
    string imageName = this.fupNewImage.FileName; 
    System.Drawing.Image postedImage = System.Drawing.Image.FromStream(this.fupNewImage.PostedFile.InputStream); 
    if (postedImage.Width == 750 && postedImage.Height == 482) 
    { 
        System.Drawing.Image thumbnail = this.CreateThumbnail(this.fupNewImage.PostedFile.InputStream); 
        thumbnail.Save("thumbnailPath"); 
    } 
}
public System.Drawing.Image CreateThumbnail(Stream source)
{
    System.Drawing.Image imgToResize = System.Drawing.Image.FromStream(source);
    Size size = new Size(156, 100);
    int sourceWidth = imgToResize.Width;
    int sourceHeight = imgToResize.Height;
    float nPercent = 0;
    float nPercentW = 0;
    float nPercentH = 0;
    nPercentW = ((float)size.Width / (float)sourceWidth);
    nPercentH = ((float)size.Height / (float)sourceHeight);
    if (nPercentH < nPercentW)
        nPercent = nPercentH;
    else
        nPercent = nPercentW;
    int destWidth = (int)(sourceWidth * nPercent);
    int destHeight = (int)(sourceHeight * nPercent);
    Bitmap b = new Bitmap(destWidth, destHeight);
    Graphics g = Graphics.FromImage((System.Drawing.Image)b);
    g.InterpolationMode = InterpolationMode.High;
    g.SmoothingMode = SmoothingMode.HighQuality;
    g.Clear(Color.Transparent);
    g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
    g.Dispose();
    return (System.Drawing.Image)b;
}

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...