Results 1 to 7 of 7

Thread: Help with System.Drawing.Region & System.Drawing.Bitmap

  1. #1

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Red face Help with System.Drawing.Region & System.Drawing.Bitmap

    What I am trying to do is make a routine that take a System.Drawing.Color and a System.Drawing.Bitmap in and returns a region excluding the passed in color. Sounds kinda easy. At least I thought so..... I have overflow in System.Drawing.dll......

    Code:
    public System.Drawing.Region ToRegion(System.Drawing.Color color, System.Drawing.Bitmap image)
    {
    	System.Drawing.Color c;
        	System.Drawing.Region r1 = null;
        	System.Drawing.Region r2 = null;
    
        	int[] colors = new int[] {System.Drawing.ColorTranslator.ToWin32(color),0}
        
    	for(int x = 0; x < image.Width; x++)
        	{
            	for(int y = 0; y < image.Height; y++)
            	{
                    	c = image.GetPixel(x, y);
                    	colors[1] = System.Drawing.ColorTranslator.ToWin32(c);
                    	if(color[0] != color[1])
                    	{
                            	if(r1 == null)
                            	{
                                    	r1 = new System.Drawing.Region(new System.Drawing.Rectangle(x, y, x + 1, y + 1));
                            	}
                            	else
                            	{
                                   		r2 = new System.Drawing.Region(new System.Drawing.Rectangle(x, y, x + 1, y + 1));
                                   		r1.Union(r2); //I'm not sure on this either
                            	}
                    	}
            	}
        	}
        	return r1;
    }
    As far as I can tel that code should work and it returns with no errors, BUT when you go say
    Code:
    frm.Region = ToRegion(System.Drawing.Color.Black, imgIGotFromFile);
    it runs, and runs, and runs, and System.OverFlowExeption in system.drawing.dll

    I also tried GrapicsPath.AddRectangle and the contructing a Region from the path but same overflow error.

    I don't want to use API if I can avoid it. Especially since it should work in the framework. I have vb6 code just like this using Region Api and SetWindowRgn that works......

    Help!!!!
    Magiaus

    If I helped give me some points.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Are you calling this in OnPaint or something that redraws UI ?

  3. #3

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    No right now I call it in a form that creates a test form.

    Code:
    Form frm = new Form();
    Region r = ToRegion(Color.Black, imageFromAFile);
    frm.Region = r;
    I get r fine but the frm.Region = r causes overflow and to be honest I can't find a way to validate r very well.....
    Magiaus

    If I helped give me some points.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I don't know and or at least I can't test this but you may try replacing these (x, y) in r1 with numbers . Otherwise , I've got no clue .

  5. #5

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    Me either but I'm thinking about it....
    Magiaus

    If I helped give me some points.

  6. #6

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    never just type code in on the page

    this code is actualy not all full of errors
    Code:
    namespace Ezekiel.Windows.Forms
    {
    	/// <summary>
    	/// Collection of usefull functions.
    	/// </summary>
    	public class Helpers
    	{
    		/// <summary>
    		/// Helper methods for System.Drawing.Regions.
    		/// </summary>
    		public class Region
    		{
    			/// <summary>
    			/// Gets a System.Drawing.Region equal to <paramref name="image"/> excluding <paramref name="excludeColor"/>.
    			/// </summary>
    			/// <param name="excludeColor">System.Drawing.Color to exclude from the generated System.Drawing.Region.</param>
    			/// <param name="image">System.Drawing.Bitmap the the generated System.Drawing.Region will equal ecluding <paramref name="excludeColor"/></param>
    			/// <returns>System.Drawing.Region equal to <paramref name="image"/> excluding <paramref name="excludeColor"/>.</returns>
    			public static System.Drawing.Region FromBitmap(System.Drawing.Color excludeColor, System.Drawing.Bitmap image)
    			{
    				System.Drawing.Color c;
    				System.Drawing.Region r1 = null;
    				System.Drawing.Region r2 = null;
    
    				int[] colors = new int[] {System.Drawing.ColorTranslator.ToWin32(excludeColor),0};
        
    				for(int x = 0; x < image.Width; x++)
    				{
    					for(int y = 0; y < image.Height; y++)
    					{
    						c = image.GetPixel(x, y);
    						colors[1] = System.Drawing.ColorTranslator.ToWin32(c);
    						if(colors[0] != colors[1])
    						{
    							if(r1 == null)
    							{
    								r1 = new System.Drawing.Region(new System.Drawing.Rectangle(x, y, x + 1, y + 1));
    							}
    							else
    							{
    								r2 = new System.Drawing.Region(new System.Drawing.Rectangle(x, y, x + 1, y + 1));
    								r1.Union(r2); //I'm not sure on this either
    							}
    						}
    					}
    				}
    				return r1;
    			}
    		}
    	}
    }
    Magiaus

    If I helped give me some points.

  7. #7

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Arrow help

    I hate not being able to make something work. I haven't slept since I started this because my brain won't shut up about why won't it work.....
    Magiaus

    If I helped give me some points.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width