有没有办法在.NET中快速将jpeg调整为其原始大小的1/8?

时间:2022-05-30 21:19:18

And hopefully with as little loss as possible, as I've heard the implementation put forward by a guy named Guido Vollbeding. Standard GDI+ doesn't seem to have this capability, and all I've found so far are either specs or fully-integrated tools. A lean, pluggable .NET component would be highly desirable. What are common pitfalls if it comes to that I have implement it myself?

希望尽可能少的损失,因为我听说过一个名叫Guido Vollbeding的人提出的实施。标准GDI +似乎没有这种功能,到目前为止我发现的都是规格或完全集成的工具。精简,可插拔的.NET组件是非常需要的。如果我自己实现它会有什么常见的陷阱?

Every ideas and guidances are appreciated!

每一个想法和指导都受到赞赏!

EDIT: My aim is to bypass the obvious cycle of decompressing into .NET Bitmap, resizing it there then compressing it again. This article seems to point to the right way, but so far I've never seen any .NET implementation

编辑:我的目标是绕过明显的解压缩循环到.NET Bitmap,在那里调整大小然后再压缩它。本文似乎指出了正确的方法,但到目前为止,我从未见过任何.NET实现

3 个解决方案

#1


This is pretty easy using the .NET Bitmap class:

使用.NET Bitmap类非常简单:

void ResizeImage(string inFname, string outFname, float scale)
{
    Bitmap bmpOrig = new Bitmap(inFname);
    int w = (int)(scale * bmpOrig.Width);
    int h = (int)(scale * bmpOrig.Height);
    Bitmap bmpNew = new Bitmap(bmpOrig, new Size(w, h));
    bmpNew.Save(outFname, ImageFormat.Jpeg);
}

You'll need to add a reference to System.Drawing, and use both System.Drawing and System.Drawing.Imaging in your code:

您需要添加对System.Drawing的引用,并在代码中使用System.Drawing和System.Drawing.Imaging:

I can't say how much loss that will have.

我不能说会有多少损失。

#2


I don't know of any algorithms that specifically handle 1/(2^n), but if you can find one you will need to be able to lock the bits on the bitmap (if you want any reasonable form of speed). Using set/get pixel is a poor idea.

我不知道任何专门处理1 /(2 ^ n)的算法,但是如果你能找到一个,你需要能够锁定位图上的位(如果你想要任何合理的速度形式)。使用set / get像素是一个糟糕的主意。

Clickety

#3


Load it into memory as a bitmap, then get its data and use unsafe code, written like it was C++ to resize it. Here is where I learned how to resize images quickly (I don't think that link resizes them, but it shows you how to do write fast imaging code in .Net)

将其作为位图加载到内存中,然后获取其数据并使用不安全的代码,就像C ++一样编写它来调整大小。这是我学习如何快速调整图像大小的地方(我不认为链接会调整它们的大小,但它会向您展示如何在.Net中编写快速成像代码)

Setting a breakpoint and stepping through your code in the Disassembly window will tell you how you are doing. Avoid any array access, just use pointers, array access is the kiss of death to tight performance loops in .Net (because there is a check on whether the index is within the bounds and then a conditional jump to throw an exception for Every. Single. Access.)

在Disassembly窗口中设置断点并单步执行代码将告诉您如何操作。避免任何数组访问,只使用指针,数组访问是.Net中紧密性能循环的死亡之吻(因为检查索引是否在边界内,然后条件跳转为Every抛出异常。 。访问。)

#1


This is pretty easy using the .NET Bitmap class:

使用.NET Bitmap类非常简单:

void ResizeImage(string inFname, string outFname, float scale)
{
    Bitmap bmpOrig = new Bitmap(inFname);
    int w = (int)(scale * bmpOrig.Width);
    int h = (int)(scale * bmpOrig.Height);
    Bitmap bmpNew = new Bitmap(bmpOrig, new Size(w, h));
    bmpNew.Save(outFname, ImageFormat.Jpeg);
}

You'll need to add a reference to System.Drawing, and use both System.Drawing and System.Drawing.Imaging in your code:

您需要添加对System.Drawing的引用,并在代码中使用System.Drawing和System.Drawing.Imaging:

I can't say how much loss that will have.

我不能说会有多少损失。

#2


I don't know of any algorithms that specifically handle 1/(2^n), but if you can find one you will need to be able to lock the bits on the bitmap (if you want any reasonable form of speed). Using set/get pixel is a poor idea.

我不知道任何专门处理1 /(2 ^ n)的算法,但是如果你能找到一个,你需要能够锁定位图上的位(如果你想要任何合理的速度形式)。使用set / get像素是一个糟糕的主意。

Clickety

#3


Load it into memory as a bitmap, then get its data and use unsafe code, written like it was C++ to resize it. Here is where I learned how to resize images quickly (I don't think that link resizes them, but it shows you how to do write fast imaging code in .Net)

将其作为位图加载到内存中,然后获取其数据并使用不安全的代码,就像C ++一样编写它来调整大小。这是我学习如何快速调整图像大小的地方(我不认为链接会调整它们的大小,但它会向您展示如何在.Net中编写快速成像代码)

Setting a breakpoint and stepping through your code in the Disassembly window will tell you how you are doing. Avoid any array access, just use pointers, array access is the kiss of death to tight performance loops in .Net (because there is a check on whether the index is within the bounds and then a conditional jump to throw an exception for Every. Single. Access.)

在Disassembly窗口中设置断点并单步执行代码将告诉您如何操作。避免任何数组访问,只使用指针,数组访问是.Net中紧密性能循环的死亡之吻(因为检查索引是否在边界内,然后条件跳转为Every抛出异常。 。访问。)