ASP.NET:调整上传到服务器的图像的大小(高度和宽度)

发布于 2024-08-07 11:56:45 字数 72 浏览 3 评论 0原文

如何在服务器上调整我刚刚上传的图像的大小?我使用 C# 和 .NET Framework 3.5 SP1。

谢谢!

How can I resize an image on server that I just uploaded? I using C# with .NET Framework 3.5 SP1.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

︶ ̄淡然 2024-08-14 11:56:45

尝试以下方法:

 public string ResizeImageAndSave(int Width, int Height, string imageUrl, string destPath)
    {
        System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(imageUrl);
        double widthRatio = (double)fullSizeImg.Width / (double)Width;
        double heightRatio = (double)fullSizeImg.Height / (double)Height;
        double ratio = Math.Max(widthRatio, heightRatio);
        int newWidth = (int)(fullSizeImg.Width / ratio);
        int newHeight = (int)(fullSizeImg.Height / ratio);
        //System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
        System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
        //DateTime MyDate = DateTime.Now;
        //String MyString = MyDate.ToString("ddMMyyhhmmss") + imageUrl.Substring(imageUrl.LastIndexOf("."));
        thumbNailImg.Save(destPath, ImageFormat.Jpeg);
        thumbNailImg.Dispose();
        return "";
    }
    public bool ThumbnailCallback() { return false; }

Try the following method:

 public string ResizeImageAndSave(int Width, int Height, string imageUrl, string destPath)
    {
        System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(imageUrl);
        double widthRatio = (double)fullSizeImg.Width / (double)Width;
        double heightRatio = (double)fullSizeImg.Height / (double)Height;
        double ratio = Math.Max(widthRatio, heightRatio);
        int newWidth = (int)(fullSizeImg.Width / ratio);
        int newHeight = (int)(fullSizeImg.Height / ratio);
        //System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
        System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
        //DateTime MyDate = DateTime.Now;
        //String MyString = MyDate.ToString("ddMMyyhhmmss") + imageUrl.Substring(imageUrl.LastIndexOf("."));
        thumbNailImg.Save(destPath, ImageFormat.Jpeg);
        thumbNailImg.Dispose();
        return "";
    }
    public bool ThumbnailCallback() { return false; }
方圜几里 2024-08-14 11:56:45

你试过这个吗?

public Image resize( Image img, int width, int height )
    {
        Bitmap b = new Bitmap( width, height ) ;
        Graphics g = Graphics.FromImage( (Image ) b ) ;
 g.DrawImage( img, 0, 0, width, height ) ;
    g.Dispose() ;

    return (Image ) b ;
}

Have you tried this?

public Image resize( Image img, int width, int height )
    {
        Bitmap b = new Bitmap( width, height ) ;
        Graphics g = Graphics.FromImage( (Image ) b ) ;
 g.DrawImage( img, 0, 0, width, height ) ;
    g.Dispose() ;

    return (Image ) b ;
}
饮湿 2024-08-14 11:56:45

我总是使用的片段:

var target = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
target.SetResolution(source.HorizontalResolution,
source.VerticalResolution);

using (var graphics = Graphics.FromImage(target))
{
    graphics.Clear(Color.White);
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

    graphics.DrawImage(source,
        new Rectangle(destX, destY, destWidth, destHeight),
        new Rectangle(sourceX, sourceY, source.Width, source.Height),
        GraphicsUnit.Pixel);
}

返回目标;

the snippet I always use:

var target = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
target.SetResolution(source.HorizontalResolution,
source.VerticalResolution);

using (var graphics = Graphics.FromImage(target))
{
    graphics.Clear(Color.White);
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

    graphics.DrawImage(source,
        new Rectangle(destX, destY, destWidth, destHeight),
        new Rectangle(sourceX, sourceY, source.Width, source.Height),
        GraphicsUnit.Pixel);
}

return target;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文