AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Monday, 21 December 2015

Create thumbnail in ASP.NET

You can resize image this with no effect on the picture quality and you can save it in any format such as .jpeg, .png , .bmp and .gif etc. You can use this code any where in asp.net.


public System.Drawing.Image GenerateThumnail(System.Drawing.Image imgPhoto, int Width, int Height)
    {
        int sourceWidth = int.Parse(imgPhoto.Width.ToString());
        int sourceHeight = int.Parse(imgPhoto.Height.ToString());
        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 (nPercentH < nPercentW)
        {
            nPercent = nPercentH;
            destX = System.Convert.ToInt16((Width -
                          (sourceWidth * nPercent)) / 2);
        }
        else
        {
            nPercent = nPercentW;
            destY = System.Convert.ToInt16((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);
        bmPhoto.MakeTransparent();
 
        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.Clear(Color.Transparent);
        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;
    }

0 comments :

Post a Comment

  • Popular Posts
  • Comments