C#如何以位图形式打开图像文件,但要按比例缩小



如何将图像文件打开为按比例缩小的位图?

例如,在java中,我可以这样做:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap myBitmap = BitmapFactory.decodeFile(fname, options);

(这将以位图的形式加载图像文件,但按比例缩小2,意味着它只加载图像文件的一半像素(

我如何在c#中做同样的事情?

使用如何调整图像C#的大小以及替换decodeFile:的扩展方法

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
static public class ImageHelper
{
static public Bitmap LoadBitmap(this string filename, double scale = 1)
{
var image = Image.FromFile(filename);
return ResizeImage(image, (int)(image.Width * scale), (int)(image.Height * scale));
}
// ResizeImage code here
}

用法:

string filename = "path";
var bitmap1 = filename.LoadBitmap(0.5);
var bitmap2 = "path".LoadBitmap(0.5);
var bitmap3 = ImageHelper.LoadBitmap("path", 0.5);

最新更新