I'm try to create a simple WinForms viewer to show DICOM files generated in a NOVARAD PACS system. I'm using the following code from their GitHub page:
var image = new DicomImage(#"C:\myDicom.dcm");
image.RenderImage().AsClonedBitmap().Save(#"test.jpg");
Process.Start("test.jpg");
When I run this code I get the following error:
Dicom.Imaging.Codec.DicomCodecException: 'Decoding dataset with
transfer syntax: JPEG 2000 Image Compression (Lossless Only) is not
supported.'
I'm assuming I need to decompress from the JPEG 2000. Can this not be done with fo-dicom?
I was trying to play around with GDCM library but I couldn't find the C# wrapper, and noticed several comments saying they rolled it into fo-dicom.
Any suggestions?
In .NET Framework applications, the build architecture needs to be set to x86 or x64. Codec access on Any CPU architecture will not work, since there are no native codec libraries available for that architecture. For more information, see the fo-dicom wiki.
Related
I'm searching for a package able to extract one image at a given time of a video stored in a C# MemoryStream (.Net Framework 4.6.2) (in memory, not on disk). I have found a lot of wrapper for FFmpeg (I've tried Xabe.FFmpeg, MediaToolkit, NReco.VideoConverter, which all work fine) but I could not find any handling a file in memory. So i'd like to know if someone already did this before I start making my own wrapper, which does not look easy from my standpoint. Or if You know of an open source wrapper that I could use / modify. Thank you.
To add some precisions, I'm working on a midleware on which Videos are uploaded to then be stored by a third party (I don't know where the files are stored) and I'd like to extract a Thumnail from the video (the image at 10 seconds of a video). I stay at your disposal if you need any details.
I can't use any command line tool or the libraries that I tried because I can't use the disk, it has to be in memory.
We have legacy applications built using VB6. These applications are using lead-tools. Everything was perfectly working. We have another .NET process that optimizes the image (and do some water-marking) and save it in tiff format. Here is glimpse of .NET code,
using (var bitmap = new Bitmap(contractWidth, contractHeight))
{
using (var canvas = Graphics.FromImage(bitmap))
{
canvas.InterpolationMode = InterpolationMode.Default;
// Play with canvas
canvas.Save();
}
using (var stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Tiff);
return stream.ToArray();
}
}
When we save this in tiff format (say image.tif). But when we open this file on our VB6 project, it shows a blue screen. I tried to compare the image which is working and image which is not working. Here are screens,
Working:
Not Working:
Update: This fixed my issue Convert TIFF to 1bit
I know you found a solution to the problem by converting the input image to 1-bit, but I wanted to elaborate more on the cause of the original problem, which is LEADTOOLS not opening the 32-bit file correctly in the first place.
You haven't specified which version of LEADTOOLS you're using, but since it's a legacy VB6 application, it's probably a rather old version (somewhere between v10 and v17; the current version is 20).
In any case, even older versions of the SDK should have no problem opening 32-bit TIFF files, but your application might be missing one or both of the following requirements:
Different sub-types of TIFF files require different LEADTOOLS DLLs. This is explained in the help topic Files to be Included with Your Application.
Older versions of the SDK required a special license to support LZW compression, back in the days when there was an active patent on LZW. If you're using one of these versions AND your application does not have that license, it won't support LZW tiff or gif files.
Please note that even owners of older SDK versions get free support. So if you are the owner of the original SDK, feel free to email any questions to support#leadtools.com, along with your LEADTOOLS product serial number.
I am trying to download a LinkedIn profile image into a .NET application. The application attempts to open the image, and either resize, crop, then resave the image into a PNG image format. The application loads the image into a stream. Then it tries to instantiate a .NET Bitmap type from the stream. The Bitmap type cannot instantiate, there is always a "Parameter is not valid" error that is returned during instantiation.
It seems all LinkedIn profile images cannot be opened by the native .NET Bitmap() APIs. I.e. my profile image is https://media.licdn.com/mpr/mpr/wc_200_200/p/4/005/024/13e/11d2b5e.jpg. Images from other websites can be opened fine though. It seems possibly like the .NET graphics API does not natively support the LinkedIn jpeg format of profile images. In fact, saving the jpg locally and trying to open it using Windows native applications also fails.
How can I open and edit these images, either resizing, or cropping, etc.?
Error stack trace is below:
[ArgumentException: Parameter is not valid.]
System.Drawing.Bitmap..ctor(Stream stream) +411353
This is a WebP image. Using ImageMagick's identify utility, I get:
11d2b5e.jpg WEBP 200x200 200x200+0+0 8-bit sRGB 4.44KB 0.000u 0:00.00
You'll have to use a library to read this image. Here is one.
BTW, don't you just love GDI+ error messages? This is the one that pops up for... well, pretty much every sort of error you can imagine.
To add to Ed S.' advice and analysis, I researched the WebP image encoding type at https://developers.google.com/speed/webp/?csw=1. There is an official Windows WebP codec available by Google for download at https://developers.google.com/speed/webp/download. Google's download page also offers a WebP to PNG converter as a Windows command line utility. So it would be possible to PInvoke that, possibly as a plan B alternative to a pre-packed .NET library.
After downloading the Google WebP codec, all native Windows graphics applications, such as Windows Photo Viewer, and the Windows Explorer Shell, are able to open and render WebP format images. However, the codec does not integrate natively into the .NET bitmap API, so a third party library like Ed S.' suggestion, or the Google WebP decoder command line utility via PInvoke, would do the trick.
I assumed that LinkedIn was serving .jpg files as the profile images have a .jpg extension. I won't necessarily know the true file type while trying to process a .jpg. I guess my development path of least resistance will be to use the native .NET Bitmap API first, and if it fails, attempt other image libraries such as WebP for .NET. I imagine there are more robust solutions to the problem.
Does anyone know of a .Net library for saving or converting an image to the Mac PCT/PICT file format?
I can save a JPG to PCT/PICT format in Photoshop; and I see this LeadTools library for $995 dollars, but would like to spend less money than that for the simple convert. I don't need all the other features this costly library supports. http://www.leadtools.com/SDK/Raster/Formats/Raster-Format-MACPICT.htm
A client claims that need JPG images saved in PIC/PICT file format for their SASI yearbook Mac software.
Thanks!
Other than writing your own exporter, you may be able to use ImageMagick's QuickDraw PICT support.
I have seen their API used successfully from P/Invoke and COM; there is an example on CodeProject I just found that uses it from VB.NET.
If it helps, here is the file format for PICT. PICT v1.0 is substantially out of date (I believe it was deprecated sometime in the 90s). Inside Macintosh also has some details on the PICT v2.0 format, and I seem to remember hearing something about JPEG encoding in PICT.
Try this http://www.imagemagick.org/
It is a C++ lib, but they have .NET wrapper
ImageMagicNET
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.
Anybody got any ideas? I don't really want to pay for a library to do it unless I have to..
Seems like we can do it using FreeImage (which is free)
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);
I was looking for something similar a while back, with a view to implementing one if I could; The responses to my question imply that there is no documented method to do this for GDI+ which the Image class in .Net uses.
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 may be one out there already (ask your local friendly search engine?)
There is an option to use an addon such as DotImage which supports JPEG2000, although there may be more "effort" involved in loading images.
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.
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:
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);
// NOTE: memory needs to be explicitly freed (GC won't do this)
FreeImage.UnloadEx(ref dib);
I've used Leadtools to display JPEG 2000 images. They provide a .NET library including WPF and WinForms controls to display the images. However, there is a reasonably steep price tag.
You can use Jpeg2000.Net library if you need a fully managed solution without unsafe blocks. Disclaimer: I am working on this library, the library is commercial.
Here is the basic sample for decoding of JPEG 2000 image to 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);
}
Also, for a currently-up-to-date, open-source option you can use Emgu CV, which is a wrapper around OpenCV. Basic example code in C# looks like:
Mat image = CvInvoke.Imread(#"\Path\To\File.jp2");