The Sandbox Thread

10 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Invert Colors

Back to Forum: The Sandbox
  • User profile image
    Angus

    This program is one that inverts the colors of a picture. I was hoping that someone could tell me a way of making this program work faster, as currently it is somewhat lacking in speed.

    Thanks in advance

    Angus Higgins

  • User profile image
    Yargnits

    Instead of

                            int R = UserImage.GetPixel(WidthPixels, HeightPixels).R;
                            int G = UserImage.GetPixel(WidthPixels, HeightPixels).G;
                            int B = UserImage.GetPixel(WidthPixels, HeightPixels).B;
                            UserImage.SetPixel(WidthPixels, HeightPixels, Color.FromArgb(255 - R, 255 - G, 255 - B));

    couldn't you do
                            int invert = 0xFFFFFF;
                            UserImage.SetPixel(WidthPixels, HeightPixels, Color.FromArgb(UserImage.GetPixel(WidthPixels, HeightPixels).ToArgb ^ invert);

    (or some other more efficient way of XORing every pixel in the picture)?

  • User profile image
    John Galt

    Use a ColorMatrix. It will be almost instant.

  • User profile image
    Maurits

    John Galt wrote:
    Use a ColorMatrix. It will be almost instant.


    ColorMatrix example from Bob Powell
    Image img=Image.FromFile(<filename>);
    Bitmap copy=new Bitmap(img.Width,img.Height);
    ImageAttributes ia = new ImageAttributes();
    ColorMatrix cm=new ColorMatrix();
    cm.Matrix00=-1;
    cm.Matrix11=-1;
    cm.Matrix22=-1;
    ia.SetColorMatrix(cm);
    Graphics g=Graphics.FromImage(copy);
    g.DrawImage(img,new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);
    g.Dispose();
    img.Dispose();

  • User profile image
    Angus

    I have made a program that uses ColorMatrix class, but it uses System.Drawing.Graphics, so it draws an image onto a component, is there a way to use System.Drawing.Graphics to draw onto a blank bitmap, or just implement the ColorMatrix class without System.Drawing.Graphics?

    Thanks in advance

    Angus Higgins

  • User profile image
    Angus

    Thanks for the tips you guys, I have finally made it go at a speed that is acceptable, I have updated the .zip file so you can have a look for yourself, the mostI have had to wait was about 1 second, and that was when I inverted a gargantuan image of dimensions 5120 by 1024 pixels, if you want to see it, it is here.

    Thanks for all your help, I may extend the program to offer grayscale, and brightness changing.

    Angus Higgins

  • User profile image
    Manip

    Pretty interesting program I must say.

  • User profile image
    Angus

    I have made one that does change in contrast, I'll put it up in a little while.

    Angus Higgins

    Change Brightness


    [Save]

  • User profile image
    Loadsgood

    Can anyone convert his app into a .NET 1.1 app? I'm too lazy to install .NET 2.0 and I'd love to be able to use it.



    Make the Change Brightness program have its own thread.
    Loadsgood.

  • User profile image
    Angus

    Loadsgood wrote:
    Make the Change Brightness program have its own thread.
    Loadsgood.


    OK, I'll add a new thread for change brightness.

    Thanks

    Angus Higgins

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.