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?
If I understand the code correctly, the loop
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.