【问题标题】:How to resize image with different resolution如何调整不同分辨率的图像大小
【发布时间】:2020-11-15 22:40:16
【问题描述】:

我必须在照片库中显示图片@width=200 height=180,但是在上传图片时我必须调整它的大小,但问题是每张图片都有不同的分辨率。如何调整具有不同分辨率的图像大小以使图像保持不变。 这是我的代码:

private void ResizeImage()
{
    System.Drawing.Image ImageToUpload = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
    byte[] image = null;
    int h = ImageToUpload.Height;
    int w = ImageToUpload.Width;
    int r = int.Parse(ImageToUpload.VerticalResolution.ToString());
    int NewWidth = 200;//constant
    int NewHeight = 180;//constant
    byte[] imagesize = FileUpload1.FileBytes;
    System.Drawing.Bitmap BitMapImage = new System.Drawing.Bitmap(ImageToUpload, NewWidth, NewHeight);//this line gives horrible output
    MemoryStream Memory = new MemoryStream();
    BitMapImage.Save(Memory, System.Drawing.Imaging.ImageFormat.Jpeg);
    Memory.Position = 0;
    image = new byte[Memory.Length + 1];
    Memory.Read(image, 0, image.Length);
}

如果分辨率为 96,如果我设置 maxwidth=200,那么它的高度将为 150,那么只有图像看起来小而准确。我们不能以所需的方式调整图像大小以使其看起来准确吗?

【问题讨论】:

    标签: c#


    【解决方案1】:

    该函数将调整图像的大小以保持纵横比。

    public static Image Resize(Image originalImage, int w, int h)
    {
        //Original Image attributes
        int originalWidth = originalImage.Width;
        int originalHeight = originalImage.Height;
    
        // Figure out the ratio
        double ratioX = (double)w / (double)originalWidth;
        double ratioY = (double)h / (double)originalHeight;
        // use whichever multiplier is smaller
        double ratio = ratioX < ratioY ? ratioX : ratioY;
    
        // now we can get the new height and width
        int newHeight = Convert.ToInt32(originalHeight * ratio);
        int newWidth = Convert.ToInt32(originalWidth * ratio);
    
        Image thumbnail = new Bitmap(newWidth, newHeight);
        Graphics graphic = Graphics.FromImage(thumbnail);
    
        graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphic.SmoothingMode = SmoothingMode.HighQuality;
        graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
        graphic.CompositingQuality = CompositingQuality.HighQuality;
    
        graphic.Clear(Color.Transparent);
        graphic.DrawImage(originalImage, 0, 0, newWidth, newHeight);
    
        return thumbnail;
    }
    

    用法

    Image BitMapImage = Resize(ImageToUpload, NewWidth, NewHeight);
    

    【讨论】:

    • 再次感谢您,您能告诉我如何覆盖空白区域吗?你可能不明白我的问题。正如我所说,我已将高度和宽度固定为 200x150,您的代码是完美的,但在这里我将举一个例子,如果我上传分辨率为 150 的图像,那么新的宽度和高度将为 105x150,这意味着两者都有 95px 空白宽度的一侧。我该如何填写那部分?
    【解决方案2】:

    在这里,我将高度固定为 180 以保持纵横比。它将调整图像大小并保存到磁盘。返回值是我在 'background-size' css 中使用的百分比值。

    public float ResizePhoto(string filepath, string filename)
        {
            var path = Path.Combine(filepath, filename);
            var newPath = Path.Combine(filepath, "sml_" + filename);
            Image orgImage = Image.FromFile(path);
    
            float fixedHt = 180f;
            int destHeight, destWidth;
            float reqScale;
    
    
            if(orgImage.Height > fixedHt)
            {
                destHeight = (int)fixedHt;
                destWidth = (int)(fixedHt / orgImage.Height * orgImage.Width);
                reqScale = destWidth  / destHeight * 100;
            }
            else
            {
                destHeight = orgImage.Height;
                destWidth = orgImage.Width;
                reqScale = fixedHt / destHeight * 100;
            }
    
            Bitmap bmp = new Bitmap(destWidth, destHeight);
            bmp.SetResolution(orgImage.HorizontalResolution,orgImage.VerticalResolution);
            Graphics grPhoto = Graphics.FromImage(bmp);
    
            grPhoto.DrawImage(orgImage,
                new Rectangle(0, 0, destWidth,  destHeight),
                new Rectangle(0, 0, orgImage.Width, orgImage.Height),
                GraphicsUnit.Pixel);
    
            bmp.Save(newPath);
    
            return reqScale;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-07
      • 2013-03-17
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 2014-06-27
      • 2014-09-21
      • 1970-01-01
      相关资源
      最近更新 更多