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

  • SEARCH
  • Home
  • 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

Ask A Question

Stats

  • Questions 96k
  • Answers 96k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your grep is probably removing ls's color codes because it… May 11, 2026 at 7:08 pm
  • Editorial Team
    Editorial Team added an answer If you want your HTML-email to look good in most… May 11, 2026 at 7:08 pm
  • Editorial Team
    Editorial Team added an answer I have the enviable situation of working for the guy… May 11, 2026 at 7:08 pm

Related Questions

I know it's a slightly rhetorical question, but I'd like to hear opinions that
Background I have an application written in native C++ over the course of several
I've incorporated Apple's Reachability sample into my own project so I know whether or
I want to make a List and add derived classes to this list. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.