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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:42:08+00:00 2026-06-01T17:42:08+00:00

I want to get the difference between two images I use this source code:

  • 0

I want to get the difference between two images

I use this source code:

 public Bitmap GetDifference(Bitmap bm1, Bitmap bm2)
        {

            if (bm1.Size != bm2.Size)
                throw new ArgumentException("exception");


            var resultImage = new Bitmap(bm1.Width, bm1.Height);

            using (Graphics g = Graphics.FromImage(resultImage))
                g.Clear(Color.Transparent);


            for (int w = 0; w < bm1.Width; w++)
            {
                for (int h = 0; h < bm1.Height; h++)
                {
                    var bm2Color = bm2.GetPixel(w, h);
                    if (IsColorsDifferent(bm1.GetPixel(w, h), bm2Color))
                    {
                        resultImage.SetPixel(w, h, bm2Color);                    
                    }                  
                }
            }

            return resultImage;
        }


        bool IsColorsDifferent(Color c1, Color c2)
        {
            return c1 != c2;
        }

This source works good for me, but I have following issues:

For exaple if I choose image with a resolution 480×320 my resultImage has the same resolution (and this is corect, because I set in when created new Bitmap), but my Images have transparent color and I want to get result images with a solid color only.

Let us say – if the solid result images (solid color pixel) in 480×320 have 100×100 field I should to get just this solid Bitmap with a resolution 100×100.

To put it simply I need to get only rectangle that bordered with a solid color and cutt of alpha chanel.

Thanks!

  • 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-06-01T17:42:09+00:00Added an answer on June 1, 2026 at 5:42 pm

    Please see the class that I wrote below:

    class ProcessingImage
        {         
            public Bitmap GetDifference(Bitmap bm1, Bitmap bm2)
            {            
                if (bm1.Size != bm2.Size)
                    throw new ArgumentException("Images must have one size");
    
                var resultImage = new Bitmap(bm1.Width, bm1.Height);
    
                using (Graphics g = Graphics.FromImage(resultImage))
                    g.Clear(Color.Transparent);
    
                int min_height = int.MaxValue;
                int min_width = int.MaxValue;
                int max_height = -1;
                int max_width = -1;
    
                for (int w = 0; w < bm1.Width; w++)
                    for (int h = 0; h < bm1.Height; h++)
                    {
                        var bm2Color = bm2.GetPixel(w, h);
                        if (IsColorsDifferent(bm1.GetPixel(w, h), bm2Color))
                        {
                            resultImage.SetPixel(w, h, bm2Color);
                            if (h < min_height) min_height = h;
                            if (w < min_width) min_width = w;
                            if (h > max_height) max_height = h;
                            if (w > max_width) max_width = w;
                        }                    
                    }
    
                max_height = max_height + 1;    //Needed for get valid max height point, otherwise we lost one pixel.
                max_width = max_width + 1;      //Needed for get valid max width point, otherwise we lost one pixel.
    
    
                // Calculate original size for image without alpha chanel.
    
                int size_h = max_height - min_height;
                int size_w = max_width - min_width;
    
                var resizeImage = new Bitmap(size_w, size_h); // Creaete bitmap with new size.
    
                for (int w = 0; w < resultImage.Width; w++)
                    for (int h = 0; h < resultImage.Height; h++)
                    {
                        var color = resultImage.GetPixel(w, h);
                        if (color.A != 0)
                        {
                            // Move each pixels at a distance calculate before.
                            int x = w - min_width;
                            int y = h - min_height;
                            resizeImage.SetPixel(x, y, color);
                        }
                    }           
    
                return resizeImage;
            }
    
            bool IsColorsDifferent(Color c1, Color c2)
            {
                return c1 != c2;
            }
        }
    

    I think, this class can be modified to optimize, but now it works good. If you need return image with original size and without alpha channel – this is one of many solutions how to do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get the difference between two Java Date objects. I've used Joda-Time
I want a Simple Javascript function to get the difference between two numbers in
I want to get the time difference in hundredth second between two touches float
I want to get the difference between two times. - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];
I want to get the difference between two sets of ints in c#. Given
I want to get the number of years between two dates. I can get
I want to calculate the difference between two dates. Currently, I am doing: Calendar
How can I get all differences, not just one? I want to use the
What I want to do is get the data from two different tables (table1
I have two objects of Zend_Date class and I want to calculate the difference

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.