PDA

Click to See Complete Forum and Search --> : Help with System.Drawing.Region & System.Drawing.Bitmap


Magiaus
Feb 29th, 2004, 01:07 AM
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......


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 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!!!!

Pirate
Feb 29th, 2004, 05:59 AM
Are you calling this in OnPaint or something that redraws UI ?

Magiaus
Feb 29th, 2004, 01:05 PM
No right now I call it in a form that creates a test form.


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.....

Pirate
Feb 29th, 2004, 02:37 PM
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 .

Magiaus
Feb 29th, 2004, 05:09 PM
Me either but I'm thinking about it....

Magiaus
Feb 29th, 2004, 05:21 PM
this code is actualy not all full of errors

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
Mar 1st, 2004, 11:06 AM
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.....