【问题标题】:Display small image catalog in asp在asp中显示小图像目录
【发布时间】:2011-09-26 08:04:43
【问题描述】:

您好,我想将产品放入目录,如何将图片裁剪成小图并保存小图和原始图?

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    试试看这里:

    C# Thumbnail Image With GetThumbnailImage

    或这里:

    Convert Bitmap to Thumbnail

    Create thumbnail and reduce image size

    Create thumbnail image

    更新

    例如,您可以将图像保存到文件夹,然后您可以执行以下操作:

    var filename = "sourceImage.png";
    
    using(var image = Image.FromFile(filename))
    {
        using(var thumbnail = image.GetThumbnailImage(20/*width*/, 40/*height*/, null, IntPtr.Zero))
        {
            thumbnail.Save("thumb.png");
        }
    }
    

    更新

    要按比例调整大小,请尝试以下操作:

    public Bitmap ProportionallyResizeBitmap (Bitmap src, int maxWidth, int maxHeight)
    {
    
    // original dimensions
    int w = src.Width;
    int h = src.Height;
    
    // Longest and shortest dimension
    int longestDimension = (w>h)?w: h;
    int shortestDimension = (w<h)?w: h;
    
    // propotionality
    float factor = ((float)longestDimension) / shortestDimension;
    
    // default width is greater than height
    double newWidth = maxWidth;
    double newHeight = maxWidth/factor;
    
    // if height greater than width recalculate
    if ( w < h )
    {
        newWidth = maxHeight / factor;
        newHeight = maxHeight;
    }
    
    // Create new Bitmap at new dimensions
    Bitmap result = new Bitmap((int)newWidth, (int)newHeight);
    using ( Graphics g = Graphics.FromImage((System.Drawing.Image)result) )
    g.DrawImage(src, 0, 0, (int)newWidth, (int)newHeight);
    
    return result;
    }
    

    【讨论】:

    • 我可以立即缩小图像还是在保存后才缩小图像?
    • 我可以在不知道原始图像大小的情况下按比例剪切图像吗?
    • 非常感谢您对我的帮助
    猜你喜欢
    • 2019-12-14
    • 2018-01-23
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多