Click here to Skip to main content
15,889,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to apply "Unsharp Mask" filter on an image. I am using C#.Net(FrameWork 4.0) and VS 2012. I tried this code, I am also getting the output image but only for 1-2 seconds, after that this error is coming. I tested everything but failed to solve.can anyone help me out of here.
Thank you in advance.

What I have tried:

public void unsharp()
{
Oimg = (Bitmap)file.Clone();//input image
for (int x = 0; x < Oimg.Width; x++)
{
for (int y = 0; y < Oimg.Height; y++)
{
Color pixel = Oimg.GetPixel(x, y);

int r = pixel.R;
int g = pixel.G;
int b = pixel.B;

int[] Oarray = new int[] { r, g, b };//input image array

int[] Rarray = new int[Oarray.Length];//resultant image array

newBitmap = (Bitmap)pictureBox1.Image;//filter applied image
for (int m = 0; m < newBitmap.Width; m++)
{
for (int n = 0; n < newBitmap.Height; n++)
{
Color p = newBitmap.GetPixel(m, n);//Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
int r1 = p.R;
int g1 = p.G;
int b1 = p.B;

int[] newarray = new int[] { r1, g1, b1 };//filter applied image array

for (int e = 0; e <= Oarray.Length - 1; e++)
Rarray[e] = Oarray[e] - newarray[e];

//convert array to bitmap
Bitmap Rbmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
int stride = pictureBox1.Width * 4;
unsafe
{
fixed (int* intPtr = &Rarray[Oarray.Length-1])
{
Rbmp = new Bitmap(pictureBox1.Width, pictureBox1.Height, stride, PixelFormat.Format32bppRgb, new IntPtr(intPtr));
}
}
newBitmap = (Bitmap)Rbmp.Clone();
pictureBox1.Image = newBitmap;
}
}
}
}
}
Posted
Updated 15-Oct-17 21:34pm
v2
Comments
Richard Deeming 17-Oct-17 14:37pm    
You might want to consider using .NET Image Library[^], which already implements a lot of image filters.

For example: C# unsharp mask filter[^]

Licensed under MIT, with some parts of the code covered by CPOL or Apache 2.0:
https://github.com/fschultz/NetImageLibrary/blob/master/License.txt[^]

1 solution

When you asked a related question: How to solve this " index was outside the bounds of the array." error ?[^] this morning, I told you to use the debugger.
It's the only tool that can tell you what is wrong: this depends on the data in your image and we do not have access to it.

So put a breakpoint on the first line of that method, and start working your way through it, looking at what is going on - it's the only way you are going to solve this!
 
Share this answer
 
Comments
Member 13341679 16-Oct-17 5:21am    
I tried as u said,still it is not solved.
OriginalGriff 16-Oct-17 5:50am    
The debugger doesn't solve it for you: it gives you the tool to look at your code while it is executing and see exactly what is going on. The contents of variables, which way conditionals branch, all of it.

So use the debugger and look at why it's failing. We can't do that for you!
Member 13341679 16-Oct-17 5:55am    
Color p = newBitmap.GetPixel(m, n);
This is the line where error is occurring, showing that attempting to read/write protected memory.
OriginalGriff 16-Oct-17 6:02am    
Right. So use the debugger to look at newBitmap, m, and n when it happens.
Unless you know what is in them, you can't begin to work out why it's a problem, or how to fix it...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900