Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,229 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,295 people online right now. Registration is fast and FREE... Join Now!




plz help in image editing c#

 
Reply to this topicStart new topic

plz help in image editing c#, ways on removing a background in a picture

mode17
12 Oct, 2008 - 11:33 PM
Post #1

New D.I.C Head
*

Joined: 12 Oct, 2008
Posts: 2

Hi There,

I'm new in c# and I'm working in a project that edits images.
well i want to remove all the background of a picture , same as the green screen effect on movies.
I kinda make it work but its only deleting only 1 shade color green(background) at a time,
so if i have a 3 shade of green i need to delete it 3 time.
is there a way to do this one time even if i have different shades of green.

here are my codes.
[
CODE


    #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)

                {
                  
                    int count = 4;
                    int limit = (fuzziness + count ) * 10;
                    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
                    {
                        byte* pixel = (byte*)(void*)ptrScan0;
                        int nOffset = stride - _image.Width * 4;
                        byte alpha;
                        byte red;
                        byte green;
                        byte blue;
                        //int R, G, B, R2, G2, B2;
                        //long c, c2;
                      
                      
                        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];

                               // long c = _image.GetPixel(position.X, position.Y);
                               // int R= c
                                
                                
                              
                                if (limit >= Math.Sqrt(Math.Pow((red - clrBaseColor.R), (2 + 1 / 2)) + Math.Pow((green - clrBaseColor.G), (2+ 1 / 2)) + Math.Pow((blue - clrBaseColor.B), (2 + 1 / 2))))
                                {
                                    pixel[0] = 0;
                                    pixel[1] = 0;
                                    pixel[2] = 0;
                                    pixel[3] = 0;
                                  
                                    

                                }
                                pixel += 4;
                            }
                            pixel += nOffset;
                            
                        }
                        count = count - 2;
                    }

                    _image.UnlockBits(bitmapData);
                }
            }
        }

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 03:13AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month