-
November 12th, 2009, 12:35 PM
#1
[RESOLVED] ColorMatrix failure on Windows 7
I have this code that transforms a bitmap to its negative:
Code:
public Bitmap Transform(Bitmap source)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(source.Width, source.Height);
//get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
// create the negative color matrix
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.Matrix00 = colorMatrix.Matrix11 = colorMatrix.Matrix22 = -1f;
colorMatrix.Matrix33 = colorMatrix.Matrix44 = 1f;
// create some image attributes
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(source, new Rectangle(0, 0, source.Width, source.Height),
0, 0, source.Width, source.Height, GraphicsUnit.Pixel, attributes);
//dispose the Graphics object
g.Dispose();
return newBitmap;
}
This works great on Windows XP. However, when I'm compiling and running the same on Windows 7 (regardless I do it from Visual Studio 2008 or Visual Studio 2010 Beta 2) I only get a black image. All the pixels are black (255, 0, 0, 0).
The only conclusion I can draw is that there is a failure in GDI+, because it cannot have anything to do with the framework (as I said, regardless I build with 3.5 or 4.0, it's the same).
Any ideas what is the problem? Thank you.
-
November 12th, 2009, 12:58 PM
#2
Re: ColorMatrix failure on Windows 7
I found the problem: the color matrix was incorrect. This is how it should be:
Code:
ColorMatrix 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[] {1, 1, 1, 0, 1}
});
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|