在C#中使用jpegtran,jpegoptim或其他JPEG优化/压缩 [英] Using jpegtran, jpegoptim, or other jpeg optimization/compression in C#

查看:1052
本文介绍了在C#中使用jpegtran,jpegoptim或其他JPEG优化/压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有100的(也许1000年)。我需要优化图像文件大小尽可能没有松动的图像质量。

I've got 100's (maybe 1000's) of products with 10-30 images of each product coming to an online store I've put together. I need to optimize the images' file sizes as much as possible without loosing image quality.

我没有直接使用jpegtran,jpegoptim,或任何其他的JPEG优化,但我已经注意到,punypng缩小文件大小倒在较大的JPEG图像无损约4-6%。

I haven't used jpegtran, jpegoptim, or any other jpeg optimizer directly but I have noticed that punypng shrinks file sizes down about 4-6% on the larger jpeg images LOSSLESSLY.

元数据已经从图像的上传(通过jumpoader)期间中剥离,所以这是不是一种选择/问题了。

Meta data is already stripped from the images during upload (via jumpoader) so that is not an option/problem anymore.

有没有办法让JPEG优化之一从C#代码运行吗?

Is there any way to get one of the jpeg optimizers to run from C# code?

请注意:我使用GoDaddy的共享与IIS7和.Net 3.5

Note: I'm using shared Godaddy hosting with IIS7 and .Net 3.5

推荐答案

如果你不举办 t等与临时文件一塌糊涂,我建议使用C ++ / CLI。

If you don't like to mess with temporary files, I'd advise to use C++/CLI.

创建Visual Studio中的C ++ / CLI DLL项目。创建一个静态管理类,只要你想从C#中使用它们定义功能:

Create a C++/CLI dll project in visual studio. Create one static managed class, and define the functions as you want to use them from C#:

public ref class JpegTools
{
public:
     static array<byte>^ Optimize(array<byte>^ input)
};

您定义可以从C#直接调用这些功能,你可以与所有的C ++提供实现这些

These functions you define can be directly called from C#, and you can implement them with all that C++ offers.

阵列^对应一个C#的字节数组。你需要使用pin_ptr<>引脚的字节数组在内存中,这样你就可以通过对数据到您所选择的非托管的Jpeg辅助函数。 C ++ / CLI有编组管理类型本地类型的充分支持。你也可以用gc_new分配新的数组,返回CLI兼容的类型。如果你有马歇尔字符串从C#和C ++作为本练习的一部分,使用MFC / ATL的CString的类型。

array^ corresponds to a C# byte array. You'll need to use pin_ptr<> to pin the byte array in memory, so you can pass on the data to the unmanaged Jpeg helper function of your choice. C++/CLI has ample support for marshalling managed types to native types. You can also allocate new array with gc_new, to return CLI compatible types. If you have to marshall strings from C# to C++ as part of this excercise, use Mfc/Atl's CString type.

您可以静态链接的所有JPEG代码到DLL。 A C ++的dll可以混合纯天然和C ++ / CLI代码。在我们的C ++ / CLI的项目,通常只有接口的源文件了解CLI类型,所有用C ++类,其余的工作。

You can statically link all the jpeg code into the dll. A C++ dll can be mixed pure native and C++/CLI code. In our C++/CLI projects, typically only the interface source files know about CLI types, all the rest work with with C++ types.

还有一些开销工作​​得到持续这种方式,不过好处是,你的代码是编译时typechecked,以及所有与unmanged代码和内存的交易得到处理在C ++的一面。它的实际工作这么好,我用C ++ / CLI单元测试本地C ++代码几乎直接与NUnit的。

There's some overhead work to get going this way, but the upside is that your code is compile-time typechecked, and all the dealings with unmanged code and memory are dealt with on the C++ side. It actually works so well that I used C++/CLI to unit test native C++ code almost directly with NUnit.

祝你好运!

这篇关于在C#中使用jpegtran,jpegoptim或其他JPEG优化/压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆