CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    [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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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}
                });
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

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
  •  





Click Here to Expand Forum to Full Width

Featured