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 7552497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:46:19+00:00 2026-05-30T10:46:19+00:00

How could I generate a System.Drawing.Image that contains the differences between the pixels of

  • 0

How could I generate a System.Drawing.Image that contains the differences between the pixels of two other images?

Something similar to GitHub does, but written in C#

The algorithm that GiHub uses is implemented in javascript. There is a context-blender project that replicates Photoshop blend modes.

Do you know if is translated to C# or a similar algorithm that has the same quality level? I need to manage also transparent images (with alpha channel).

  • 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. Editorial Team
    Editorial Team
    2026-05-30T10:46:20+00:00Added an answer on May 30, 2026 at 10:46 am

    Here’s a quick and dirty implementation:

    void Main()
    {
        var a = (Bitmap)Image.FromFile("image1.png");
        var b = (Bitmap)Image.FromFile("image2.png");
        var diff = PixelDiff(a, b);
    }
    
    unsafe Bitmap PixelDiff(Bitmap a, Bitmap b)
    {
        Bitmap output = new Bitmap(a.Width, a.Height, PixelFormat.Format32bppArgb);
        Rectangle rect = new Rectangle(Point.Empty, a.Size);
        using (var aData = a.LockBitsDisposable(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb))
        using (var bData = b.LockBitsDisposable(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb))
        using (var outputData = output.LockBitsDisposable(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb))
        {
            byte* aPtr = (byte*)aData.Scan0;
            byte* bPtr = (byte*)bData.Scan0;
            byte* outputPtr = (byte*)outputData.Scan0;
            int len = aData.Stride * aData.Height;
            for (int i = 0; i < len; i++)
            {
                // For alpha use the average of both images (otherwise pixels with the same alpha won't be visible)
                if ((i + 1) % 4 == 0)
                    *outputPtr = (byte)((*aPtr  + *bPtr) / 2);
                else
                    *outputPtr = (byte)~(*aPtr ^ *bPtr);
    
                outputPtr++;
                aPtr++;
                bPtr++;
            }
        }
        return output;
    }
    
    static class Extensions
    {
        public static DisposableImageData LockBitsDisposable(this Bitmap bitmap, Rectangle rect, ImageLockMode flags, PixelFormat format)
        {
            return new DisposableImageData(bitmap, rect, flags, format);
        }
    
        public class DisposableImageData : IDisposable
        {
            private readonly Bitmap _bitmap;
            private readonly BitmapData _data;
    
            internal DisposableImageData(Bitmap bitmap, Rectangle rect, ImageLockMode flags, PixelFormat format)
            {
                bitmap.CheckArgumentNull("bitmap");
                _bitmap = bitmap;
                _data = bitmap.LockBits(rect, flags, format);
            }
    
            public void Dispose()
            {
                _bitmap.UnlockBits(_data);
            }
    
            public IntPtr Scan0
            {
                get { return _data.Scan0; }
            }
    
            public int Stride
            {
                get { return _data.Stride;}
            }
    
            public int Width
            {
                get { return _data.Width;}
            }
    
            public int Height
            {
                get { return _data.Height;}
            }
    
            public PixelFormat PixelFormat
            {
                get { return _data.PixelFormat;}
            }
    
            public int Reserved
            {
                get { return _data.Reserved;}
            }
        }   
    }
    

    Notes:

    • this implementation assumes that both images have the same size, which might not be the case… taking different sizes into account is possible of course, just a little harder.
    • the LockBitsDisposable method is just a convenience, if you prefer you can use the standard LockBits method (but don’t forget to unlock the bits when you’re done)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some ASP.NET MVC actions that generate images dynamically (although it could equally
Im trying to consume a Java web-service, that uses a certificate. I could generate
Enterprise Architect has a way to generate the documentation in HTML/RTF/etc. that you could
I'm looking for a good address-parser that could parser any free texts and generate
I once knew a website where the user could generate the little W3C valid
I'm looking for a NHibernate Mapping generator which could generate a mapping file (hbm)
I wonder if you could automatically generate header header class functions automatically for a
I'm getting an error: GenerationException: url_for could not generate URL. Called with args: ()
Could someone describe the algorithm to generate the needed tables (action, goto) from a
Could someone please point me toward a cleaner method to generate a random enum

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.