C#.NET 中的 JPEG 2000 支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/590471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPEG 2000 support in C#.NET
提问by Gordon Thompson
It seems that .NET can't open JP2 (Jpeg 2000) files using the GDI library. I've searched on google but can't find any libraries or example code to do this.
.NET 似乎无法使用 GDI 库打开 JP2 (Jpeg 2000) 文件。我在谷歌上搜索过,但找不到任何库或示例代码来执行此操作。
Anybody got any ideas? I don't really want to pay for a library to do it unless I have to..
有人有任何想法吗?我真的不想付钱给图书馆来做这件事,除非我必须……
采纳答案by Gordon Thompson
Seems like we can do it using FreeImage(which is free)
似乎我们可以使用FreeImage(这是免费的)来做到这一点
FIBITMAP dib = FreeImage.LoadEx("test.jp2");
//save the image out to disk
FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, dib, "test.jpg", FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYNORMAL);
//or even turn it into a normal Bitmap for later use
Bitmap bitmap = FreeImage.GetBitmap(dib);
回答by Rowland Shaw
I was looking for something similar a while back, with a view to implementing one if I could; The responses to my questionimply that there is no documentedmethod to do this for GDI+ which the Image class in .Net uses.
不久前我正在寻找类似的东西,如果可以的话,我希望能实现一个;在我的问题的回答意味着没有记载的方法在.NET使用要做到这一点的GDI +这Image类。
I believe that if you're writing a WPF application, then you can extend the list of supported image formats through Windows Imanging Components codecs, and there maybe one out there already (ask your local friendly search engine?)
我相信如果您正在编写 WPF 应用程序,那么您可以通过Windows Imaging Components 编解码器扩展支持的图像格式列表,并且可能已经有一个(询问您当地的友好搜索引擎?)
There is an option to use an addon such as DotImagewhich supports JPEG2000, although there may be more "effort" involved in loading images.
可以选择使用诸如DotImage 之类的支持 JPEG2000的插件,尽管加载图像可能需要更多的“努力”。
回答by Taylor Leese
回答by Eric Bynum
For anyone coming across this old post, the above code from Gordon works great, but as jixtra pointed out, you will indeed get an exception: System.DllNotFoundException: 'Unable to load DLL 'FreeImage': The specified module could not be found.' when installing via nuget. I was able to get it working in .net 4.6.1 by installing the FreeImage-dotnet-core nuget package and manually adding the FreeImage.dll to the bin folder. You can download the dll here: http://freeimage.sourceforge.net/download.html.
对于遇到这篇旧帖子的任何人,来自 Gordon 的上述代码效果很好,但正如 jixtra 指出的那样,您确实会收到一个异常: System.DllNotFoundException: 'Unable to load DLL 'FreeImage': The specified module could not be found. ' 通过nuget安装时。通过安装 FreeImage-dotnet-core nuget 包并手动将 FreeImage.dll 添加到 bin 文件夹,我能够让它在 .net 4.6.1 中工作。您可以在此处下载 dll:http: //freeimage.sourceforge.net/download.html。
I needed a better quality image to use with tesseract so I made a few minor changes which made a huge difference to the quality of the new jpeg:
我需要一个质量更好的图像来与 tesseract 一起使用,所以我做了一些小改动,这对新 jpeg 的质量产生了巨大的影响:
var jp2Format = FREE_IMAGE_FORMAT.FIF_JP2;
var dib = FreeImage.LoadEx("test.jp2", ref jp2Format);
FreeImage.SetResolutionX(dib, 300);
FreeImage.SetResolutionY(dib, 300);
FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, dib, "test.jpg", FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYSUPERB);
回答by Vitaliy Shibaev
You can use Jpeg2000.Net libraryif you need a fully managed solution without unsafe blocks. Disclaimer: I am working on this library, the library is commercial.
如果您需要一个没有不安全块的完全托管的解决方案,您可以使用Jpeg2000.Net 库。免责声明:我正在开发这个库,该库是商业性的。
Here is the basic sample for decoding of JPEG 2000 image to TIFF:
以下是将 JPEG 2000 图像解码为 TIFF 的基本示例:
string fileName = ...; // path to JPEG 2000 image
using (var image = new J2kImage(fileName))
{
var options = new J2kDecodingOptions
{
UpsampleComponents = true
};
// Alternatively, you can decode only part of the image using J2kImage.DecodeArea method
var imageData = image.Decode(options);
imageData.Save(tiffFileName, J2kOutputFormat.Tiff);
}