简体   繁体   中英

Asp.Net: Dynamic image resize

I want to create an image resize mechanism for my Asp.Net web site project like timthumb. I want to do this:

  • Upload image
  • Set a size
  • If I need another size of this image I can give "only" size without upload again.

How can I do this, do you have any suggestion?

Here is my resizing function:

 public Bitmap Resize(Bitmap image, int newWidth, int newHeight, string message)
    {
        try
        {
            Bitmap newImage = new Bitmap(newWidth, Calculations(image.Width, image.Height, newWidth));

            using (Graphics gr = Graphics.FromImage(newImage))
            {

                gr.SmoothingMode = SmoothingMode.AntiAlias;
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
                gr.DrawImage(image, new Rectangle(0, 0, newImage.Width, newImage.Height));

                var myBrush = new SolidBrush(Color.FromArgb(64, 205, 205, 205));

                double diagonal = Math.Sqrt(newImage.Width * newImage.Width + newImage.Height * newImage.Height);

                var containerBox = new Rectangle();

                containerBox.X = (int)(diagonal / 10);
                var messageLength = (float)(diagonal / message.Length * 1);
                containerBox.Y = -(int)(messageLength / 1.6);

                var stringFont = new Font("verdana", messageLength);

                var sf = new StringFormat();

                var slope = (float)(Math.Atan2(newImage.Height, newImage.Width) * 180 / Math.PI);

                gr.RotateTransform(slope);
                gr.DrawString(message, stringFont, myBrush, containerBox, sf);
                return newImage;
            }
        }
        catch (Exception exc)
        {
            throw exc;
        }
    }


    public int Calculations(decimal orjWidth, decimal orjHeight, int newWidth)
    {
        decimal height = 0;
        decimal ratio = 0;


        if (newWidth < orjWidth)
        {
            ratio = orjWidth / newWidth;
            height = orjHeight / ratio;

            return height.To<int>();
        }

        if (orjWidth <= newWidth)
        {
            ratio = newWidth / orjWidth;
            height = orjHeight * ratio;
            return height.To<int>();
        }

        return height.To<int>();
    }

That's done with jQuery scripting.

You can achieve the same background image dynamic resize effect by using some of available jQuery plugins. Eg.: http://srobbin.com/jquery-plugins/backstretch/

Also, that can be done by using plain CSS3: https://css-tricks.com/perfect-full-page-background-image/ Best regards, templateMonster Affiliate Team!

You're looking for the ImageResizer library & httpmodule

If you use NuGet, you can Install-Package ImageResizer.MvcWebConfig

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM