.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Image resizing in Csharp

Posted By:nishithraj       Posted Date: December 08, 2009    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

Images can be resized by the following code piece in Csharp.

The     ImageWidth and ImageHeight indicate the current image's (image uploaded) height and width. And the destHeight & destWidth indicates the destination height and width that we require.

Anchor position will be automatically traced once the user resizes the image from the user interface.

 
System.Drawing.Image CurrentImage, int Width, int Height,
    AnchorPosition Anchor)
{ 
 
    int ImageWidth = CurrentImage.Width;
    int ImageHeight = CurrentImage.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)ImageWidth);
    nPercentH = ((float)Height / (float)ImageHeight); 
 
    if (nPercentH < nPercentW)
    {
        nPercent = nPercentW;
        switch (Anchor)
        {
            case AnchorPosition.Top:
                destY = 0;
                break;
            case AnchorPosition.Bottom:
                destY = (int)(Height - (ImageHeight * nPercent));
                break;
            default:
                destY = (int)((Height - (ImageHeight * nPercent)) / 2);
                break;
        }
    }
    else
    {
        nPercent = nPercentH;
        switch (Anchor)
        {
            case AnchorPosition.Left:
                destX = 0;
                break;
            case AnchorPosition.Right:
                destX = (int)(Width - (ImageWidth * nPercent));
                break;
            default:
                destX = (int)((Width - (ImageWidth * nPercent)) / 2);
                break;
        } 
 
    }  
 
    int destWidth = (int)(ImageWidth * nPercent);
    int destHeight = (int)(ImageHeight * nPercent);  
 
    Bitmap bmPhoto = new Bitmap(Width, Height,
        PixelFormat.Format24bppRgb);
    bmPhoto.SetResolution(CurrentImage.HorizontalResolution,
        CurrentImage.VerticalResolution); 
 
    Graphics grPhoto = Graphics.FromImage(bmPhoto);
    grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; 
 
    grPhoto.DrawImage(CurrentImage,
        new Rectangle(destX, destY, destWidth, destHeight),
        new Rectangle(sourceX, sourceY, ImageWidth, ImageHeight),
        GraphicsUnit.Pixel);
    grPhoto.Dispose();
    return bmPhoto;
}
 


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend