Hi,

I'm a beginner in c# and I'm now work in a project with image editing.
i already figure how to do this manually but my professor wants it automatically.
and i tried to call the class type after i load the image but it doesn't work.

here is my codes for the removing it manually.

#region Methods
public void Extract(Point position, int fuzziness)
{
if (_image != null)
{
SaveUndo();
Color clrBaseColor = _image.GetPixel(position.X, position.Y);
if (clrBaseColor.A != 0)
{

BitmapData bitmapData = _image.LockBits(new Rectangle(0, 0, _image.Width, _image.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
int stride = bitmapData.Stride;
IntPtr ptrScan0 = bitmapData.Scan0;
unsafe
{
int count = 0;
int limit = (fuzziness + count) * 20;
byte* pixel = (byte*)(void*)ptrScan0;
int nOffset = stride - _image.Width * 4;
byte alpha;
byte red;
byte green;
byte blue;
for (int y = 0; y < _image.Height; ++y)
{
for (int x = 0; x < _image.Width; ++x)
{
blue = pixel[0];
green = pixel[1];
red = pixel[2];
alpha = pixel[3];
//if (limit >= Math.Sqrt (Math .Pow ((red - clrBaseColor.R),2)+ Math .Pow  ((green- clrBaseColor.G),2) +Math .Pow ((blue-clrBaseColor.B),2)))
if (Math.Abs(red - clrBaseColor.R) < limit && Math.Abs(green - clrBaseColor.G) < limit  && Math.Abs(blue - clrBaseColor.B) < limit )
// {
//if (limit >= Math.Abs(Math.Sqrt(Math.Pow((red - clrBaseColor.R), 2) + Math.Pow((green - clrBaseColor.G), (2 + 1 / 2)) + Math.Pow((blue - clrBaseColor.B), 2))))
{
pixel[0] = 0;
pixel[1] = 0;
pixel[2] = 0;
pixel[3] = 0;
}
pixel += 4;
}
pixel += nOffset;
//}
}
count = count - 1;
}
_image.UnlockBits(bitmapData);
}
}
}

thanks

Edited 3 Years Ago by happygeek: fixed formatting

OK, so you have working code. What stops it working automatically? Do you get an error? What values are you passing your method when you called it after the image load? What did debugging show you?

OK, so you have working code. What stops it working automatically? Do you get an error? What values are you passing your method when you called it after the image load? What did debugging show you?

yes i have but its not automatic
i still need to load the picture to a form and the codes were executed by pressing a button.

i tried to make a series of codes same as the one's i used for the manual.
i just set a specific x, y to get the color value of the pixel .
but still it does work.

the debuger did not flag any errors so i don't know were were i got wrong

Well faking pressing a button is usually easy enough, and sending a point to your method looks reasonable - so, the question is, what code did you have that didnt work?

sorry for the late reply i was busy in my studies in school had no time to check on this.

well i tried to set again the specific coordinates where i will get the color that i will set as a comparison for determine the color to be deleted.

but when i do this i encountered an error :
StackOverflowException
this happens when i directly call the method for removing the green part of the picture.

#region Methods


private void ShowPhotograph()
{
//if (_preview == null)
//{
//    _preview = new Bitmap(this.picPhotograph.Width, this.picPhotograph.Height);
//    using (Graphics paintGraphics = Graphics.FromImage(_preview))
//    {
//        paintGraphics.Clear(Color.Transparent);
//        paintGraphics.DrawImage(this.Photograph.Image, 0, 0, this.picPhotograph.Width, this.picPhotograph.Height);
//    }
//}
//this.picPhotograph.Image = _preview;


this.picPhotograph.Image = this.Photograph.Image;
this.picPhotograph.Cursor = Cursors.Default;


ShowPhotograph(); ------> here were the error occurse.
btnUndo.Enabled = this.Photograph.Undoable;
this.picPhotograph.Refresh();
}


private Rectangle GetOperateRect(Point position, int toolSize)
{
Rectangle rctRusule = new Rectangle(position.X - (int)Math.Floor(toolSize / 2.0), position.Y - (int)Math.Floor(toolSize / 2.0), toolSize, toolSize);


rctRusule.X = (rctRusule.X < 0) ? 0 : rctRusule.X;
rctRusule.Y = (rctRusule.Y < 0) ? 0 : rctRusule.Y;
if (rctRusule.Right >= _preview.Width - 1)
{
rctRusule.Width -= rctRusule.Right - _preview.Width + 1;
}
if (rctRusule.Bottom >= _preview.Height - 1)
{
rctRusule.Height -= rctRusule.Bottom - _preview.Height + 1;
}


return rctRusule;
}
#endregion
}
}

i found some syntax in msdn but i'm not sure on how to use these.
is there any way that i can prevent the overflowing.

thanks

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.