Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 159379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:50:55+00:00 2026-05-11T10:50:55+00:00

That is the solution to make a negative from a image in C# Windows

  • 0

That is the solution to make a negative from a image in C# Windows Forms without any dlls and in a effective, fast way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:50:56+00:00Added an answer on May 11, 2026 at 10:50 am

    The best way to do this is directly accessing the pixels with bitmap data.

    Just to add some timing details:

    Performing Negate on an 8 Megapixel Image (on a 2.4 Ghz Core 2 Duo):

    • SetPixel (~22 seconds) – 220 Times slower
    • Color Matrix, Matajon’s method below (~750 milliseconds) – 7 times slower
    • Directly accesing the bitmap data (~100 milliseconds) – Fastest

    So, if you can’t have unsafe code, then Color Matrix is much better than SetPixel.

        public static void Negate(Bitmap image)     {         const int RED_PIXEL = 2;         const int GREEN_PIXEL = 1;         const int BLUE_PIXEL = 0;           BitmapData bmData = currentImage.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, image.PixelFormat);          try         {             int stride = bmData.Stride;             int bytesPerPixel = (currentImage.PixelFormat == PixelFormat.Format24bppRgb ? 3 : 4);              unsafe             {                 byte* pixel = (byte*)(void*)bmData.Scan0;                 int yMax = image.Height;                 int xMax = image.Width;                  for (int y = 0; y < yMax; y++)                 {                     int yPos = y * stride;                     for (int x = areaSize.X; x < xMax; x++)                     {                         int pos = yPos + (x * bytesPerPixel);                          pixel[pos + RED_PIXEL] = (byte)(255 - pixel[pos + RED_PIXEL]);                         pixel[pos + GREEN_PIXEL] = (byte)(255 - pixel[pos + GREEN_PIXEL]);                         pixel[pos + BLUE_PIXEL] = (byte)(255 - pixel[pos + BLUE_PIXEL]);                                                                         }                  }             }         }         finally         {             image.UnlockBits(bmData);         }      } 

    If you are interested, here is the code for Color Matrix:

        public static void Negate(Bitmap image)     {              Bitmap clone = (Bitmap) image.Clone();              using (Graphics g = Graphics.FromImage(image))             {                  // negation ColorMatrix                 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[] {0, 0, 0, 0, 1}                         });                  ImageAttributes attributes = new ImageAttributes();                  attributes.SetColorMatrix(colorMatrix);                  g.DrawImage(clone, new Rectangle(0, 0, clone.Width, clone.Height),                             0, 0, clone.Width, clone.Height, GraphicsUnit.Pixel, attributes);            }     } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a solution that contains both a Windows Forms application and a Console
I have a fairly small solution that includes a WPF windows application. It builds
I have a solution that is being referenced by a MSBuild project. In the
I have a solution that I am trying to build in Visual Studio which
I got a solution that includes 3 different projects, a Class Library, a Web
I have a solution that I work on with another person and the solution
We have a reporting solution that works like this. User orders the report to
I'm developing a solution that allows people to upload a DOCX file as a
I have a new solution that is an ASP.NET MVC 2.0 application. I have
Can someone recommend a hosted solution that answers the following requirements (I have seen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.