This forum contains outdated content and is available for reading only. Please contact technical support if you have any questions.

Convert to grayscale and transparency

Is it possible to have a greyscaling-function that greyscales all types of images regardless of format ( I guess pdf won't work ). And in addition preserve any transparancy for images that contain this information. It is also important to preserve metainfo/keywords in the image (if present).

I tried your sample-code:

        using (var reader = ImageReader.Create("../../../../_Input/Copenhagen_RGB.jpg"))
        using (var converter = new ColorConverter(PixelFormat.Format1bppIndexed))
        using (var writer = ImageWriter.Create("../../../../_Output/PF_ConvertToBlackAndWhiteMemoryFriendly.png"))
        {
            Pipeline.Run(reader + converter + writer);
        }


and changed the pixelformat to grayscale, but all i get is "UnsupportedPixelFormatException", and I get that also when using 1bppIndexed as in your sample...
Do you need to convert image to grayscale or black and white pixel format?

Please note only the grayscale pixel formats support transparency (we don't support alpha channel with B&W images).

To preserve metadata you need to copy EXIF, IPTC, XMP, and Adobe Resources.

using Aurigma.GraphicsMill;
using Aurigma.GraphicsMill.Codecs;
using Aurigma.GraphicsMill.Transforms;
using System;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			using (var reader = ImageReader.Create(@"C:\temp\dscf9639.jpg"))
			using (var converter = new ColorConverter(
				reader.PixelFormat.HasAlpha ? PixelFormat.Format16bppAgrayscale : PixelFormat.Format8bppGrayscale))
			using (var writer = new TiffWriter(@"C:\temp\out.tiff"))
			{
				writer.Exif = reader.Exif;
				writer.Iptc = reader.Iptc;
				writer.AdobeResources = reader.AdobeResources;
				writer.Xmp = reader.Xmp;

				Pipeline.Run(reader + converter + writer);
			}
		}
	}
}


Unfortunately I couldn't reproduce the "UnsupportedPixelFormatException" problem. Could you post an entire code sample?
Works perfectly. Don't know what I did wrong other than having an ImageWriter instead of TIFFWriter (I'm now using JpegWriter without errors).
BTW, I had to use use the TiffWriter class because only the TIFF format supports both grayscale with alpha channel (Format16bppAgrayscale) and all type of metadata (EXIF, IPTC and so on). For example JPEG doesn't support alpha channel and PNG doesn't support metadata.