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