【问题标题】:ASP.NET: resize (height and width) an image uploaded to serverASP.NET:调整上传到服务器的图像大小(高度和宽度)
【发布时间】:2009-10-16 19:48:31
【问题描述】:

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

谢谢!

【问题讨论】:

    标签: c# asp.net resize-image


    【解决方案1】:

    试试下面的方法:

     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; }
    

    【讨论】:

    • 还有文件大小?我也想减小文件大小。
    【解决方案2】:

    你试过了吗?

    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 ;
    }
    

    【讨论】:

    【解决方案3】:

    我一直使用的 sn-p:

    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);
    }
    

    返回目标;

    【讨论】:

    猜你喜欢
    • 2013-04-28
    • 2017-12-08
    • 2014-04-04
    • 1970-01-01
    • 2016-03-30
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多