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:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...