Bug 19515 - SetColorMatrix gives incorrect result
Summary: SetColorMatrix gives incorrect result
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Drawing ()
Version: 3.2.x
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-05-04 08:42 UTC by Erik Schierboom
Modified: 2014-05-04 08:42 UTC (History)
1 user (show)

Is this bug a regression?: ---
Last known good build:

The input file for the Gist to test with (24.94 KB, image/jpeg)
2014-05-04 08:42 UTC, Erik Schierboom

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 19515 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Erik Schierboom 2014-05-04 08:42:48 UTC
Created attachment 6727 [details]
The input file for the Gist to test with

I believe I have found a bug in the System.Drawing.Imaging namespace. The bug occurs when trying to apply an image transformation using a ColorMatrix instance. After applying the transformation, the resulting image is incorrect. When running the same code on the Microsoft .NET framework, the images turn out correctly.

The code to reproduce this problem can be found below (the input.jpg image has been added as an attachment):

var brightnessFactor = -0.05f;

var image = Image.FromFile("input.jpg");

var colorMatrix = new ColorMatrix(
								new float[][]
												new float[] { 1, 0, 0, 0, 0 }, 
												new float[] { 0, 1, 0, 0, 0 },
												new float[] { 0, 0, 1, 0, 0 },
												new float[] { 0, 0, 0, 1, 0 },
												new float[] { brightnessFactor, brightnessFactor, brightnessFactor, 0, 1 }

using (var graphics = Graphics.FromImage(image))
	using (var imageAttributes = new ImageAttributes())

		graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);


// Saving the images results in incorrect images being output on Mono, for any chosen image format
image.Save("output.jpg", ImageFormat.Jpeg);
image.Save("output.png", ImageFormat.Png);
image.Save("output.bmp", ImageFormat.Bmp);

You can also find the code here in a Gist: https://gist.github.com/ErikSchierboom/99cbec38a537d6e65c32

That this code does not execute on Mono is best shown through the following images: 

* The original input.jpg image: http://i.imgur.com/TCY7kl7.jpg
* The output.jpg image when running on the Microsoft .NET Framework: http://i.imgur.com/AFvIHqR.jpg
* The output.jpg image when running on Mono: http://i.imgur.com/czowWrE.jpg

I am running the latest stable release of Mono and 3.4.0 on Unix.