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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:25:23+00:00 2026-05-31T23:25:23+00:00

I have a method in my C# WinForms program, which checks if X column

  • 0

I have a method in my C# WinForms program, which checks if X column of an image has black pixels or not.

    static Boolean GetColumnState(Bitmap bmp, int x, int col)
    {
        BitmapData pixelData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb);
        Boolean state = false;
        unsafe
        {
            int* pData = (int*)pixelData.Scan0.ToPointer();
            pData += x;
            for (int i = 0; i < bmp.Height; ++i)
            {
                pData += bmp.Width;
                if (Color.FromArgb(*pData) == Color.FromArgb(255, col, col, col)) // error here
                { state = true; break; }
            }
        }
        bmp.UnlockBits(pixelData);
        return state;
    }

Unfortunatelly I’m getting

“Attempted to read or write protected memory” error

at “(Color.FromArgb(*pData) == Color.FromArgb(255, col, col, col)) “ line

Here is how i define “col” from different method:

           if (GetColumnState(b1, 0, 255) == false)
            { col = 255; }
           else {col = 0;}
           // more code + loops
           GetColumnState(b1, i, col)

Strange thing is: I’m getting errors only if pixel color is defined as 255 (i.e. black)..

How do you explain this? Please note, that I’m writing an OCR program, for that reason I load multiple dictionaries with different key values. I crop images a lot during OCR.
My lucky guess is that threads are messing up one another.

Now, I’ve found a way to fix this problem, but the price is + ~150-200ms to total script execution time (which is unnecessary price, I think).

Normally I load dictionaries like this:

        Dictionary<string, Bitmap> lookup = new Dictionary<string, Bitmap>();
        Bitmap l0 = new Bitmap(@"C:\xxx\0.bmp", true);
            //+15 more
        lookup.Add("0", l0);
            //+15 more

        Dictionary<string, Bitmap> lookup2 = new Dictionary<string, Bitmap>();
        Bitmap nAa = new Bitmap(@"C:\yyy\Aa.bmp", true);
            //+15 more
        lookup2.Add("A", nAa);
            //+15 more

To fix this problem I have to create “voids” for each dictionary and load them in different threads, like this:

void loadNumbers1()
{
        lookup4 = new Dictionary<string, Bitmap>();
        Bitmap sd = new Bitmap(@"C:\xxxxx\a.bmp", true);
            //+15 more
        lookup4.Add("0", s0);
            //+15 more
}

void loadNumbers2()// 4, 5, 6,
{
        //repeat
}

Now we launch threads:

        Thread tNum2= new Thread(new ThreadStart(loadNumbers2));
        tNum2.Start();

        Thread tNum3= new Thread(new ThreadStart(loadNumbers3));
        tNum3.Start();

The last step (without this step program works faster, but error occurs more often):

tNum3.Join();

That’s it, now i don’t have any problems, but execution time is longer.. Any ideas on how to solve this problem in an easier way, without using multiple threads? In my case, without join() I’m getting 5-100ms Dictionary load time, With join() – up to 300ms. Without threads – up to 180 (in case if error doesn’t occur).

Sorry for long post

EDIT: (permanent fix)

static unsafe Boolean GetColumnState(Bitmap bmp, int x, int col)
        {
            BitmapData pixelData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb);
            Boolean state = false;

            unsafe
            {
                byte[] buffer = new byte[pixelData.Height * pixelData.Stride];
                Marshal.Copy(pixelData.Scan0, buffer, 0, buffer.Length);

                for (int i = 0; i < pixelData.Height - 1; ++i)
                {
                    byte red = buffer[i * pixelData.Stride + 4 * x + 2];
                    if (red == col)
                    { state = true; break; }
                }
            }
            bmp.UnlockBits(pixelData);
            return state;
        }

I don’t understand what was so wrong about pointers, but bytes work great. @tia thanks for pointing out to my problem.

Does anybody know why multithreading slows down dictionary load time instead of speeding it up?

  • 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-31T23:25:24+00:00Added an answer on May 31, 2026 at 11:25 pm

    If I understand the code correctly, the loop

    for (int i = 0; i < bmp.Height; ++i)
    {
        pData += bmp.Width;
        if (Color.FromArgb(*pData) == Color.FromArgb(255, col, col, col)) // error here
        { state = true; break; }
    }
    

    is not correct. The pointer should be incremented at the end of iteration, or else you will skip the first scan line and overflow reading the bitmap buffer.

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

Sidebar

Related Questions

I have a winforms, and it connecting wit webservice. Webservice has method which create
I have a winforms image list which contains say like 200 images 256x256. I
I have a winforms program which have an Exit button which fires an event
I have a program which displays an image to the windows form, and places
I have a method which takes params object[] such as: void Foo(params object[] items)
I have a method in .NET (C#) which returns string[][] . When using RegAsm
I have a method, which returns void. It sets some values in an array,
I have a Library ( DLL ) that has a method with the following:
In my WinForms application I have a timer which 'ticks' every second. In the
I have a Winforms App (.NET 3.X) That runs a method in a class

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.