Skip to content

Instantly share code, notes, and snippets.

@ErikSchierboom
Created May 4, 2014 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ErikSchierboom/99cbec38a537d6e65c32 to your computer and use it in GitHub Desktop.
Save ErikSchierboom/99cbec38a537d6e65c32 to your computer and use it in GitHub Desktop.
namespace MonoColorMatrix
{
using System.Drawing;
using System.Drawing.Imaging;
public class Program
{
public static void Main()
{
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())
{
imageAttributes.SetColorMatrix(colorMatrix);
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment