如何设置图片的200x200背景?

时间:2019-05-13 20:21:37

标签: c# .net image background resize

我当前正在创建一个应用,该应用可让您上传一些图像,然后调整其大小(100x180),并在其上放置200x200的白色背景。

我已经想到了如何调整图像大小并创建白色背景,但是我无法弄清楚如何将它们放在一起。

以下是调整大小和白色背景的代码:

 public static Bitmap ResizeImage(Image image, int width, int height)
    {
        var destRect = new Rectangle(0, 0, width, height);
        var destImage = new Bitmap(width, height);

        destImage.SetResolution(image.HorizontalResolution, image.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;
    }

private Bitmap DrawFilledRectangle(int x, int y)
    {
        Bitmap bmp = new Bitmap(x, y);
        using (Graphics graph = Graphics.FromImage(bmp))
        {
            Rectangle ImageSize = new Rectangle(0, 0, x, y);
            graph.FillRectangle(Brushes.White, ImageSize);
        }
        return bmp;
    }

预期结果:

a busy cat

谢谢大家!

0 个答案:

没有答案