I was reading about image compression, but found that the encoder for c# is lossy, is there any way to make a lossless function compressor in c#? I could only find the lossy option ( https://learn.microsoft.com/en-us/dotnet/api/system.drawing.imaging.encoderparameters?view=netframework-4.8 )
Please have a look at the Fo-Dicom library at GitHub. It is an open-source library for the use in medical imaging. They have implemented all the lossless JPEG algorithms like Jpeg Lossless, Jpeg-LS and Jpeg2000:
https://github.com/fo-dicom/fo-dicom/tree/4562544b8742c78b7b6dd49f1e47bc7952690ee5
Fo-Dicom in itself uses the Health-Efferent library (also open source), which is written in C++, but C# wrappers are implemented:
https://github.com/Efferent-Health/fo-dicom.Codecs
Use PNG to have lossless image compression in .NET.
Because PNG is by design (and also standardized by W3C and IETF) is lossless.
For more information, visit official documentation on encoding and decoding PNG using .NET:
https://learn.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/how-to-encode-and-decode-a-png-image
And for reference, this is the official PNG standard: https://www.rfc-editor.org/rfc/rfc2083
Update 1:
I added clarification of supported lossless image compression in .NET.
JPEG compression is by design lossy in most cases ("acceptably so") but a lossless standard does exist; you might find that some of the c# snippets on this site get you towards your goal:
https://www.graphicsmill.com/docs/gm/applying-lossless-jpeg-transforms.htm
Related
I'm writing a service for a project that's going to handle our image processing. One such process is supposed to strip all metadata from the byte[] provided and return the same image as a byte[].
The method I'm currently working on involves always converting the image to a Bitmap, then converting it back to the original format and returning the data from a MemoryStream.
I haven't been able to test it yet but something tells me I'm going to experience some quality loss.
How can I remove all metadata from any image with a common format?
(bmp, gif, png, jpg, icon, tiff)
Not sure how I can narrow that down any further. Would be nice if I got some feedback regarding the downvotes.
For the lossless formats (except JPEG), your idea of loading it as a bitmap and re-saving is fine. Not sure if .NET natively supports TIFFs (I doubt it does).
For JPEGs, as you suggested there may be quality loss if you're re-compressing the file after decompressing it. For that, you might try the ExifLibrary and see if that has anything. If not, there are command line tools (like ImageMagick) that can strip metadata. (If you use ImageMagick, you're all set, since it supports all of your required formats. The command you want is convert -strip.)
For TIFFs, .NET has built-in TiffBitmapDecoder and ...Encoder classes you might be able to use; see here.
In short, using an external tool like ImageMagick is definitely the easiest solution. If you can't use an external tool, you're almost certainly going to need to special-case the formats that .NET doesn't support natively (and the lossy JPEG).
EDIT: I just read that ImageMagick doesn't do lossless stripping with JPEGs, sorry. I guess using the library I linked above, or some other JPEG library, is the best I can think of.
Is there a class in C# library which does LZW compression on TIFF images. I know there is a compression scheme inviolving LZW being present, but using that doesnt decrease the file size whatsoever. Is there any thing that Im assuming wrong? Please correct me if I am.
Because LZW is loseless compression, you can compress TIFF images or any other kind of data using the same way. In C# you can use the SharpLZW library.
--EDIT (1)--
If you want to produce a TIFF file with embedded LZW compression respecting the TIFF specification look at section 13 of the specification.
--EDIT (2)--
There was a patent but it is now expired.
This is a question about TIFF and compression. I have hundreds of LZW compressed tiff images. I wonder, is it possible to convert those to CCITT T.6? Is there some API? Please help.
LZW compression can be used to compress almost any image. CCITT T.6 can compress only bilevel (black and white) images.
If your images are bilevel ones and they are compressed with LZW compression then you can recompress them using tiffcp utility (comes with LibTiff.Net library, free, source code available).
If your images are full-color ones then you will have to convert them to bilevel first. One of my answers contains sample code for such conversion.
How to compress and uncompress JPEG using lossless method?
It's specified in the JPEG standard but few encoders support it. Use a lossless image format like PNG.
The Intel IPP contains a codec that supports the lossless version of the JPEG standard. Certain medical devices use this format and probably nobody else in the world.
Using the normal JPEG you can set the quality to 100% (or 1.0), but I don't think you'll ever get completely lossless compression because this is lossy by definition.
There is Lossless JPEG but it's a completely different algorithm, but I can't find any evidence of how well it's supported.
If you have any choice in the matter use png instead.
How do I display tiff files on a Silverlight application? I can display any image format except tiff, can anyone help me? Thanks.
I was successful displaying TIFFs in Silverlight. It's easy to port the free LibTiff.NET library to Silverlight, just 3-4 minor tweaks required.
The library itself is quite legacy-like and raw to use and one still needs to have some knowledge about the inner workings of the TIFF format in order to be able to extract the image data the way one needs it.
But it's doable and the bits and pieces can then be chiseled into a WriteableBitmap.
Why don't you try TiffLight? It is a Silverlight control that allows native display of Tiff files in Silverlight.
A Tiff file is a multi-page format so rendering it is not as simple as a png, gif or bmp.
You have of course already found this via a web search but it'll cost you.
Silverlight 2.0 doesn't support tiff images according to this.
However, in the article I believe it explains a way to convert the tiff image to a jpeg or a png (which is supported by Silverlight). However, you'll have to do this processing on the server-side.
I would use an HttpHandler that converts the Tiff using the TiffBitmapDecoder and PngBitmapEncoder classes.
Alternatively, if you can decode the Tiff images in Silverlight, you can display them using a WriteableBitmap.