【问题标题】:Resize image in asp.net Size readonly error在 asp.net 中调整图像大小只读错误
【发布时间】:2013-12-03 23:46:46
【问题描述】:

我想调整通过文件上传器传来的图像大小。为此,我被提及以下内容:

How to set Bitmap.Width and Bitmap.height

(贾维德·阿克拉姆的回答)

编写如下代码:

Dim imgSmall As Bitmap = New Bitmap(FUpload.PostedFile.InputStream, False)
imgSmall.Size = New Size(250, 300)

但给我错误:

Size is read-only property.

在这种情况下如何调整图像大小?

【问题讨论】:

    标签: c# asp.net .net vb.net visual-studio-2008


    【解决方案1】:

    它是只读的;您必须从中创建一个新的位图。试试这个:-

    internal static Image ScaleByPercent(Image image, Size size, float percent)
    {
        int sourceWidth  = image.Width,
            sourceHeight = image.Height;
    
        int destWidth  = (int)(sourceWidth * percent),
            destHeight = (int)(sourceHeight * percent);
    
        if (destWidth <= 0)
        {
            destWidth = 1;
        }
    
        if (destHeight <= 0)
        {
            destHeight = 1;
        }
    
        var resizedImage = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
        resizedImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
    
        // get handle to new bitmap
        using (var graphics = Graphics.FromImage(resizedImage))
        {
            InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    
            // create a rect covering the destination area
            var destRect = new Rectangle(0, 0, destWidth, destHeight);
            var brush    = new SolidBrush(drawing.Color.White);
            graphics.FillRectangle(brush, destRect);
    
            // draw the source image to the destination rect
            graphics.DrawImage(image,
                                destRect,
                                new Rectangle(0, 0, sourceWidth, sourceHeight),
                                GraphicsUnit.Pixel);
        }
    
        return resizedImage;
    }
    

    这是来自我正在生产的网站;如果您愿意,我可以向您发送代码,说明如何在调整大小时保持正确的纵横比(即“大小”参数中传递的内容)

    希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      • 2014-01-25
      • 1970-01-01
      • 2014-02-17
      • 1970-01-01
      相关资源
      最近更新 更多