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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:35:59+00:00 2026-05-13T10:35:59+00:00

How can I XOR images with BMP extension using C# built in methods or

  • 0

How can I XOR images with BMP extension using C# built in methods or any other image processing technique?

I’m now doing this, but I want much more efficient method.

using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace IProcessing
{
/// <summary>
/// Summary description for LogicalOperator.
/// </summary>
public class LogicalOperator
{
    Bitmap bmpimg;
    public LogicalOperator()
    {
        //
        // TODO: Add constructor logic here
        //

    }

    public Bitmap XORing( Bitmap bmp )
    {
        BitmapData bmpData = bmpimg.LockBits( new Rectangle( 0 , 0 , bmpimg.Width , bmpimg.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb );
        BitmapData bmpData2 = bmp.LockBits( new Rectangle( 0 , 0 , bmp.Width , bmp.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb );

        int width = bmpData.Width;
        int height = bmpData.Height;

        if( bmpData2.Width > width )
            width = bmpData2.Width;
        if( bmpData2.Height > height )
            height = bmpData2.Height;

        bmpimg.UnlockBits( bmpData );
        bmp.UnlockBits( bmpData2 );

        Bitmap bit1 = new Bitmap( bmpimg , width , height );
        Bitmap bit2 = new Bitmap( bmp , width , height );

        Bitmap bmpresult = new Bitmap( width , height );

        BitmapData data1 = bit1.LockBits( new Rectangle( 0 , 0 , bit1.Width , bit1.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb );
        BitmapData data2 = bit2.LockBits( new Rectangle( 0 , 0 , bit2.Width , bit2.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb );
        BitmapData data3 = bmpresult.LockBits( new Rectangle( 0 , 0 , bmpresult.Width , bmpresult.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb );

        unsafe
        {
            int remain1 = data1.Stride - data1.Width * 3;
            int remain2 = data2.Stride - data2.Width * 3;
            int remain3 = data3.Stride - data3.Width * 3;

            byte* ptr1 = ( byte* )data1.Scan0;
            byte* ptr2 = ( byte* )data2.Scan0;
            byte* ptr3 = ( byte* )data3.Scan0;

            for( int i = 0 ; i < height ; i ++ )
            {
                for( int j = 0 ; j < width * 3 ; j ++ )
                {
                    ptr3[ 0 ] = ( byte ) ( XOR_Operator( ptr1[ 0 ] , ptr2[ 0 ] ) );
                    ptr1 ++;
                    ptr2 ++;
                    ptr3 ++;
                }

                ptr1 += remain1;
                ptr2 += remain2;
                ptr3 += remain3;
            }


        }

        bit1.UnlockBits( data1 );
        bit2.UnlockBits( data2 );
        bmpresult.UnlockBits( data3 );

        return bmpresult;
    }       

    public byte XOR_Operator( byte a , byte b )
    {

        byte A = ( byte )( 255 - a );
        byte B = ( byte )( 255 - b );

        return ( byte )( ( a & B ) | ( A & b ) );
    }

    public Bitmap XNORing( Bitmap bmp )
    {
 // NANDing of 2 images is done by ANDing 2 images then negating the resultant

        Bitmap orimg = XORing( bmp );

        BitmapData data = orimg.LockBits( new Rectangle( 0 , 0 , orimg.Width , orimg.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb );

        unsafe
        {
            int remain = data.Stride - data.Width * 3;

            byte* ptr = ( byte* )data.Scan0;

            for( int i = 0 ; i < data.Height ; i ++ )
            {
                for( int j = 0 ; j < data.Width * 3 ; j ++ )
                {

                    ptr[ 0 ] = invert( ptr[ 0 ] );
                    ptr ++;
                }

                ptr += remain;
            }

        }

        orimg.UnlockBits( data );

        return orimg;

    }

    public byte invert( byte b )
    {
        return ( byte )( 255 - b );
    }

    public void setImage( Bitmap bmp )
    {
        bmpimg = ( Bitmap )bmp.Clone( );  
    }

    public Bitmap getImage( )
    {
        return ( Bitmap )bmpimg.Clone( );
    }
}
}
  • 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-13T10:35:59+00:00Added an answer on May 13, 2026 at 10:35 am

    The big speedup will be from using C/C++ for this inner loop.

    Other alternatives would be:

    1. In the unmanaged code, if you draw with GDI rather than GDI+, you can use SetROP2() with R2_XORPEN. This may be faster than your current loop, although your current loop doesn’t look too scary.

    2. If your bitmap is just for showing on the screen, you could display it normally and then draw over it with ControlPaint.DrawReversibleLine()

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

Sidebar

Related Questions

How can the XOR operation (on two 32 bit ints) be implemented using only
Can someone explain to me how XOR swapping of two variables with no temp
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
How can I find local IP addresses (i.e. 192.168.x.x or 10.0.x.x) in Python platform
Can somebody point me to a resource that explains how to go about having
Can you cast a List<int> to List<string> somehow? I know I could loop through
can you recommend some good ASP.NET tutorials or a good book? Should I jump
Can a LINQ enabled app run on a machine that only has the .NET
Can anyone tell me how I can display a status message like 12 seconds
Can you tell me what is the difference between abstraction and information hiding in

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.