首页 > 解决方案 > 如何调整图像大小以填充新尺寸?

问题描述

我正在尝试拉伸图像以适应新尺寸,但我无法弄清楚如何使图像填充新尺寸,它只会创建更大的图像尺寸但它保持图像不变,我想要图像填充指定的宽度和高度。

 private Bitmap resizeImage(Image image, int width, int height, float HorizontalResolution, float VerticalResolution)
        {

            Rectangle destRect = new Rectangle(0, 0, width, height);

            Bitmap destImage = new Bitmap(width, height);

            destImage.SetResolution(HorizontalResolution, VerticalResolution);

            using (var graphics = Graphics.FromImage(destImage))
            {
                graphics.CompositingMode = CompositingMode.SourceCopy;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

                using (var wrapMode = new ImageAttributes())
                {
                    wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                    graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
                }
            }

            return destImage;
        }

标签: c#image

解决方案


由于您想将整个源图像拉伸到指定大小,只需使用不接受源坐标的简单五参数重载:

像这样:

graphics.DrawImage(image, 0, 0, width, height);

推荐阅读