c# - 调整位图图像大小

标签 c# wpf image bitmap resize

我想在保存图片时缩小尺寸。 我怎样才能调整它的大小? 我使用此代码来重新生成图像:

Size size = new Size(surface.Width, surface.Height);
surface.Measure(size);
surface.Arrange(new Rect(size));
// Create a render bitmap and push the surface to it
RenderTargetBitmap renderBitmap =
    new RenderTargetBitmap(
        (int)size.Width,
        (int)size.Height, 96d, 96d,
        PixelFormats.Default);
renderBitmap.Render(surface);

BmpBitmapEncoder encoder = new BmpBitmapEncoder();
// push the rendered bitmap to it
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
// save the data to the stream
encoder.Save(outStream);

最佳答案

public static Bitmap ResizeImage(Bitmap imgToResize, Size size)
{
    try
    {
        Bitmap b = new Bitmap(size.Width, size.Height);
        using (Graphics g = Graphics.FromImage((Image)b))
        {
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(imgToResize, 0, 0, size.Width, size.Height);
        }
        return b;
    }
    catch 
    { 
        Console.WriteLine("Bitmap could not be resized");
        return imgToResize; 
    }
}

关于c# - 调整位图图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10839358/

相关文章:

wpf - 自动建议组合框 - MVVM

c# - 命令绑定(bind)的多个参数

wpf - 寻找 WPF Grid ColumnSpan 行为的解释

image - URI 的目标不存在 : 'package:image/image.dart'

android - 调整从图库中选取的图像大小

c# - 使用 RestSharp 设置 'Content-Type' header

c# - NOSQL数据库选型论坛

c# - Elasticsearch NEST 5.0 中的 FilterContainer

PHP 表单未将所有图像上传到文件夹

c# - EF Core HasData() 未在 OnModelCreating 内调用