本文整理匯總了C#中System.Drawing.Region.GetRegionScans方法的典型用法代碼示例。如果您正苦於以下問題:C# Region.GetRegionScans方法的具體用法?C# Region.GetRegionScans怎麽用?C# Region.GetRegionScans使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Drawing.Region
的用法示例。
在下文中一共展示了Region.GetRegionScans方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: DumpRegion
/* For debugging */
public static void DumpRegion (Region rgn)
{
Matrix matrix = new Matrix ();
RectangleF [] rects = rgn.GetRegionScans (matrix);
for (int i = 0; i < rects.Length; i++)
Console.WriteLine ( rects[i]);
}
示例2: Main
public static void Main(string[] args)
{
Region region = new Region ();
RectangleF[] rects = region.GetRegionScans (new Matrix ());
for (int i = 0; i < rects.Length; i++)
Console.WriteLine ("{0}", rects [i]);
}
示例3: GetRegionScans
/// <summary>
/// Retrieves an array of rectangles that approximates a region, and computes the
/// pixel area of it. This method is necessary to work around some bugs in .NET
/// and to increase performance for the way in which we typically use this data.
/// </summary>
/// <param name="region">The Region to retrieve data from.</param>
/// <param name="scans">An array of Rectangle to put the scans into.</param>
/// <param name="area">An integer to write the computed area of the region into.</param>
/// <remarks>
/// Note to implementors: Simple implementations may simple call region.GetRegionScans()
/// and process the data for the 'out' variables.</remarks>
public static void GetRegionScans(Region region, out Rectangle[] scans, out int area)
{
using (Matrix matrix = new Matrix ()) {
RectangleF [] s = region.GetRegionScans (matrix);
scans = new Rectangle [s.Length];
area = 0;
for (int i = 0; i < s.Length; i++){
scans [i].X = (int) s [i].X;
scans [i].Y = (int) s [i].Y;
scans [i].Width = (int) s [i].Width;
scans [i].Height = (int) s [i].Height;
area += scans[i].Width * scans[i].Height;
}
}
}
示例4: TrespassArea
// Returns a percentage of the ErrorArea circle trespassing on the zone.
// If anyone knows calculus better than me I'd welcome you to clean up this function =)
public float TrespassArea(Point3D pntLocation, float flErrorRadius) {
float flReturnPercentage = 0.0F;
float flErrorArea = (float)(flErrorRadius * flErrorRadius * Math.PI);
GraphicsPath gpLocationError = new GraphicsPath();
gpLocationError.AddEllipse(new RectangleF(pntLocation.X - flErrorRadius, pntLocation.Y - flErrorRadius, flErrorRadius * 2, flErrorRadius * 2));
gpLocationError.CloseAllFigures();
Region regZone = new Region(this.ZoneGraphicsPath);
regZone.Intersect(gpLocationError);
RectangleF[] a_recScans = regZone.GetRegionScans(new Matrix());
Rectangle recIntersection = new Rectangle(int.MaxValue, int.MaxValue, 0, 0);
int iPixelCount = 0;
if (a_recScans.Length > 0) {
for (int i = 0; i < a_recScans.Length; i++) {
recIntersection.X = a_recScans[i].X < recIntersection.X ? (int)a_recScans[i].X : recIntersection.X;
recIntersection.Y = a_recScans[i].Y < recIntersection.Y ? (int)a_recScans[i].Y : recIntersection.Y;
recIntersection.Width = a_recScans[i].Right > recIntersection.Right ? (int)a_recScans[i].Right - recIntersection.X : recIntersection.Width;
recIntersection.Height = a_recScans[i].Bottom > recIntersection.Bottom ? (int)a_recScans[i].Bottom - recIntersection.Y : recIntersection.Height;
}
//recIntersection = this.RecFtoRec(regZone.GetBounds(this.CreateGraphics()));
Point pntVisible = new Point(recIntersection.X, recIntersection.Y);
for (pntVisible.X = recIntersection.X; pntVisible.X <= recIntersection.Right; pntVisible.X++) {
for (pntVisible.Y = recIntersection.Y; pntVisible.Y <= recIntersection.Bottom; pntVisible.Y++) {
if (regZone.IsVisible(pntVisible) == true) {
iPixelCount++;
}
}
}
}
flReturnPercentage = (float)iPixelCount / flErrorArea;
// Accounts for low error when using this method. (98.4% should be 100%)
// but using regZone.GetRegionScans is slightly lossy.
if (flReturnPercentage > 0.0F) {
flReturnPercentage = (float)Math.Min(1.0F, flReturnPercentage + 0.02);
}
return flReturnPercentage;
}
示例5: OnShapeChange
private void OnShapeChange (object sender, EventArgs e)
{
GraphicsPath path = GetShape (shapeComboBox);
if (path != null) {
if (region != null) {
region.Dispose ();
}
region = new Region (path);
scans = region.GetRegionScans (Matrix);
infoLabel.Text = System.String.Format ("{0} rectangles to re-create the shape.", scans.Length);
StringBuilder sb = new StringBuilder ();
for (int i = 0; i < scans.Length; i++) {
sb.AppendFormat ("{0}: x {1}, y {2}, w {3}, h {4}{5}", i,
scans[i].X, scans[i].Y, scans[i].Width, scans[i].Height,
Environment.NewLine);
}
scansTextBox.Text = sb.ToString ();
}
UpdateUI ();
}
示例6: TestXor
public void TestXor()
{
Bitmap bmp = new Bitmap (600, 800);
Graphics dc = Graphics.FromImage (bmp);
Matrix matrix = new Matrix ();
RectangleF [] rects;
Rectangle rect1 = new Rectangle (380, 30, 60, 80);
Rectangle rect2 = new Rectangle (410, 40, 60, 80);
Region rgn1 = new Region (rect1);
Region rgn2 = new Region (rect2);
rgn1.Xor (rgn2);
rects = rgn1.GetRegionScans (matrix);
Assert.AreEqual (4, rects.Length);
Assert.AreEqual (380, rects[0].X);
Assert.AreEqual (30, rects[0].Y);
Assert.AreEqual (60, rects[0].Width);
Assert.AreEqual (10, rects[0].Height);
Assert.AreEqual (380, rects[1].X);
Assert.AreEqual (40, rects[1].Y);
Assert.AreEqual (30, rects[1].Width);
Assert.AreEqual (70, rects[1].Height);
Assert.AreEqual (440, rects[2].X);
Assert.AreEqual (40, rects[2].Y);
Assert.AreEqual (30, rects[2].Width);
Assert.AreEqual (70, rects[2].Height);
Assert.AreEqual (410, rects[3].X);
Assert.AreEqual (110, rects[3].Y);
Assert.AreEqual (60, rects[3].Width);
Assert.AreEqual (10, rects[3].Height);
}
示例7: TestIntersect
public void TestIntersect()
{
Bitmap bmp = new Bitmap (600, 800);
Graphics dc = Graphics.FromImage (bmp);
Matrix matrix = new Matrix ();
RectangleF [] rects;
RectangleF rect3, rect4;
Region rgn3, rgn4;
/* Two simple areas */
Rectangle rect1 = new Rectangle (260, 30, 60, 80);
Rectangle rect2 = new Rectangle (290, 40, 60, 80);
Region rgn1 = new Region (rect1);
Region rgn2 = new Region (rect2);
rgn1.Intersect (rgn2);
rects = rgn1.GetRegionScans (matrix);
Assert.AreEqual (1, rects.Length);
Assert.AreEqual (290, rects[0].X);
Assert.AreEqual (40, rects[0].Y);
Assert.AreEqual (30, rects[0].Width);
Assert.AreEqual (70, rects[0].Height);
/* No intersect */
rect1 = new Rectangle (20, 330, 40, 50);
rect2 = new Rectangle (50, 340, 40, 50);
rect3 = new Rectangle (70, 360, 30, 50);
rect4 = new Rectangle (80, 400, 30, 10);
rgn1 = new Region (rect1);
rgn2 = new Region (rect2);
rgn3 = new Region (rect3);
rgn4 = new Region (rect4);
rgn1.Intersect (rgn2);
rgn1.Intersect (rgn3);
rgn1.Intersect (rgn4);
rects = rgn1.GetRegionScans (matrix);
Assert.AreEqual (0, rects.Length);
}
示例8: SmallXor2
public void SmallXor2 ()
{
Region region = new Region (sp2);
region.Xor (sp1);
CompareSmallRegion (region, sxor, 7, 7);
RectangleF[] scans = region.GetRegionScans (matrix);
Assert.AreEqual (4, scans.Length, "GetRegionScans");
CheckRectF ("[0]", 0, 0, 3, 2, scans[0]);
CheckRectF ("[1]", 0, 2, 2, 1, scans[1]);
CheckRectF ("[2]", 3, 2, 2, 1, scans[2]);
CheckRectF ("[3]", 2, 3, 3, 2, scans[3]);
}
示例9: SmallComplement2
public void SmallComplement2 ()
{
Region region = new Region (sp2);
region.Complement (sp1);
CompareSmallRegion (region, sexclude1, 7, 7);
RectangleF[] scans = region.GetRegionScans (matrix);
Assert.AreEqual (2, scans.Length, "GetRegionScans");
CheckRectF ("[0]", 0, 0, 3, 2, scans[0]);
CheckRectF ("[1]", 0, 2, 2, 1, scans[1]);
}
示例10: SmallUnion_Self1
public void SmallUnion_Self1 ()
{
Region region = new Region (sp1);
region.Union (sp1);
CompareSmallRegion (region, self1, 7, 7);
RectangleF[] scans = region.GetRegionScans (matrix);
Assert.AreEqual (1, scans.Length, "GetRegionScans");
CheckRectF ("[0]", 0, 0, 3, 3, scans[0]);
}
示例11: Colorize
/// <summary>Implements a b&w + hue-based transformation for an image.</summary>
/// <param name="original">The original image.</param>
/// <param name="selectedPixels">The location in the original image of the selected pixels for hue
/// comparison.</param>
/// <param name="epsilon">Allowed hue variation from selected pixels.</param>
/// <param name="paths">GraphicPath instances demarcating regions containing possible pixels to be
/// left in color.</param>
/// <param name="parallel">Whether to run in parallel.</param>
/// <returns>The new Bitmap.</returns>
public Bitmap Colorize(Bitmap original, List<Point> selectedPixels, int epsilon, List<GraphicsPath> paths, bool parallel)
{
// Create a new bitmap with the same size as the original
int width = original.Width, height = original.Height;
Bitmap colorizedImage = new Bitmap(width, height);
// Optimization: For every GraphicsPath, get a bounding rectangle. This allows for quickly
// ruling out pixels that are definitely not containing within the selected region.
Rectangle [] pathsBounds = null;
if (paths != null && paths.Count > 0)
{
pathsBounds = new Rectangle[paths.Count];
for(int i=0; i<pathsBounds.Length; i++)
{
pathsBounds[i] = Rectangle.Ceiling(paths[i].GetBounds());
}
}
// Optimization: Hit-testing against GraphicPaths is relatively slow. Hit testing
// against rectangles is very fast. As such, appromixate the area of the GraphicsPath
// with rectangles which can be hit tested against instead of the paths. Not quite
// as accurate, but much faster.
List<RectangleF[]> compositions = null;
if (paths != null && paths.Count > 0)
{
compositions = new List<RectangleF[]>(paths.Count);
using (Matrix m = new Matrix())
{
for(int i=0; i<paths.Count; i++)
{
using (Region r = new Region(paths[i])) compositions.Add(r.GetRegionScans(m));
}
}
}
// Use FastBitmap instances to provide unsafe/faster access to the pixels
// in the original and in the new images
using (FastBitmap fastColorizedImage = new FastBitmap(colorizedImage))
using (FastBitmap fastOriginalImage = new FastBitmap(original))
{
// Extract the selected hues from the selected pixels
List<float> selectedHues = new List<float>(selectedPixels.Count);
foreach (Point p in selectedPixels)
{
selectedHues.Add(fastOriginalImage.GetColor(p.X, p.Y).GetHue());
}
// For progress update purposes, figure out how many pixels there
// are in total, and how many constitute 1% so that we can raise
// events after every additional 1% has been completed.
long totalPixels = height * width;
long pixelsPerProgressUpdate = totalPixels / 100;
if (pixelsPerProgressUpdate == 0) pixelsPerProgressUpdate = 1;
long pixelsProcessed = 0;
// Pixels close to the selected hue but not close enough may be
// left partially desaturated. The saturation window determines
// what pixels fall into that range.
const int maxSaturationWindow = 10;
int saturationWindow = Math.Min(maxSaturationWindow, epsilon);
// Separated out the body of the loop just to make it easier
// to switch between sequential and parallel for demo purposes
Action<int> processRow = y =>
{
for (int x = 0; x < width; x++)
{
// Get the color/hue of th epixel
Color c = fastOriginalImage.GetColor(x, y);
float pixelHue = c.GetHue();
// Use hit-testing to determine if the pixel is in the selected region.
bool pixelInSelectedRegion = false;
// First, if there are no paths, by definition it is in the selected
// region, since the whole image is then selected
if (paths == null || paths.Count == 0) pixelInSelectedRegion = true;
else
{
// For each path, first see if the pixel is within the bounding
// rectangle; if it's not, it's not in the selected region.
Point p = new Point(x, y);
for (int i = 0; i < pathsBounds.Length && !pixelInSelectedRegion; i++)
{
if (pathsBounds[i].Contains(p))
{
// The pixel is within a bounding rectangle, so now
// see if it's within the composition rectangles
// approximating the region.
foreach (RectangleF bound in compositions[i])
{
//.........這裏部分代碼省略.........
示例12: TestUnionGroup2
public void TestUnionGroup2 ()
{
RectangleF[] rects;
Region r1 = new Region ();
Rectangle rect2 = Rectangle.Empty;
Rectangle rect1 = Rectangle.Empty;
Rectangle rect3 = Rectangle.Empty;
Rectangle rect4 = Rectangle.Empty;
{ // TEST1: Not intersecting rects. Union just adds them
rect1 = new Rectangle (20, 20, 20, 20);
rect2 = new Rectangle (20, 80, 20, 10);
rect3 = new Rectangle (60, 60, 30, 10);
r1 = new Region (rect1);
r1.Union (rect2);
r1.Union (rect3);
rects = r1.GetRegionScans (new Matrix ());
Assert.AreEqual (3, rects.Length, "TUG1Test1");
AssertEqualRectangles (new RectangleF (20, 20, 20, 20), rects[0], "TUG1Test2");
AssertEqualRectangles (new RectangleF (60, 60, 30, 10), rects[1], "TUG1Test3");
AssertEqualRectangles (new RectangleF (20, 80, 20, 10), rects[2], "TUG1Test4");
}
{ // TEST2: Intersecting from the right
/*
* -----------
* | |
* | |-------- |
* | | |
* | |-------- |
* | |
* ----------|
*
*/
rect1 = new Rectangle (10, 10, 100, 100);
rect2 = new Rectangle (40, 60, 100, 20);
r1 = new Region (rect1);
r1.Union (rect2);
rects = r1.GetRegionScans (new Matrix ());
Assert.AreEqual (3, rects.Length, "TUG2Test1");
AssertEqualRectangles (new RectangleF (10, 10, 100, 50), rects[0], "TUG2Test2");
AssertEqualRectangles (new RectangleF (10, 60, 130, 20), rects[1], "TUG2Test3");
AssertEqualRectangles (new RectangleF (10, 80, 100, 30), rects[2], "TUG2Test4");
}
{ // TEST3: Intersecting from the right
/*
* -----------
* | |
* |-------- | |
* | | |
* |-------- | |
* | |
* ----------|
*
*/
rect1 = new Rectangle (70, 10, 100, 100);
rect2 = new Rectangle (40, 60, 100, 20);
r1 = new Region (rect1);
r1.Union (rect2);
rects = r1.GetRegionScans (new Matrix ());
Assert.AreEqual (3, rects.Length, "TUG3Test1");
AssertEqualRectangles (new RectangleF (70, 10, 100, 50), rects[0], "TUG3Test2");
AssertEqualRectangles (new RectangleF (40, 60, 130, 20), rects[1], "TUG3Test3");
AssertEqualRectangles (new RectangleF (70, 80, 100, 30), rects[2], "TUG3Test4");
}
{ // TEST4: Intersecting from the top
/*
* -----
* | |
* -----------
* | | | |
* | ----- |
* | |
* | |
* ----------|
*
*/
rect1 = new Rectangle (40, 100, 100, 100);
rect2 = new Rectangle (70, 80, 50, 40);
r1 = new Region (rect1);
r1.Union (rect2);
rects = r1.GetRegionScans (new Matrix ());
Assert.AreEqual (2, rects.Length, "TUG4Test1");
AssertEqualRectangles (new RectangleF (70, 80, 50, 20), rects[0], "TUG4Test2");
AssertEqualRectangles (new RectangleF (40, 100, 100, 100), rects[1], "TUG4Test3");
}
{ // TEST5: Intersecting from the bottom
//.........這裏部分代碼省略.........
示例13: TestUnionGroup1
public void TestUnionGroup1 ()
{
Bitmap bmp = new Bitmap (600, 800);
Graphics dc = Graphics.FromImage (bmp);
Matrix matrix = new Matrix ();
Rectangle rect1, rect2, rect3, rect4;
Region rgn1, rgn2, rgn3, rgn4;
RectangleF [] rects;
rect1 = new Rectangle (500, 30, 60, 80);
rect2 = new Rectangle (520, 40, 60, 80);
rgn1 = new Region(rect1);
rgn2 = new Region(rect2);
rgn1.Union(rgn2);
rects = rgn1.GetRegionScans (matrix);
Assert.AreEqual (3, rects.Length);
Assert.AreEqual (500, rects[0].X);
Assert.AreEqual (30, rects[0].Y);
Assert.AreEqual (60, rects[0].Width);
Assert.AreEqual (10, rects[0].Height);
Assert.AreEqual (500, rects[1].X);
Assert.AreEqual (40, rects[1].Y);
Assert.AreEqual (80, rects[1].Width);
Assert.AreEqual (70, rects[1].Height);
Assert.AreEqual (520, rects[2].X);
Assert.AreEqual (110, rects[2].Y);
Assert.AreEqual (60, rects[2].Width);
Assert.AreEqual (10, rects[2].Height);
rect1 = new Rectangle (20, 180, 40, 50);
rect2 = new Rectangle (50, 190, 40, 50);
rect3 = new Rectangle (70, 210, 30, 50);
rgn1 = new Region (rect1);
rgn2 = new Region (rect2);
rgn3 = new Region (rect3);
rgn1.Union (rgn2);
rgn1.Union (rgn3);
rects = rgn1.GetRegionScans (matrix);
Assert.AreEqual (5, rects.Length);
Assert.AreEqual (20, rects[0].X);
Assert.AreEqual (180, rects[0].Y);
Assert.AreEqual (40, rects[0].Width);
Assert.AreEqual (10, rects[0].Height);
Assert.AreEqual (20, rects[1].X);
Assert.AreEqual (190, rects[1].Y);
Assert.AreEqual (70, rects[1].Width);
Assert.AreEqual (20, rects[1].Height);
Assert.AreEqual (20, rects[2].X);
Assert.AreEqual (210, rects[2].Y);
Assert.AreEqual (80, rects[2].Width);
Assert.AreEqual (20, rects[2].Height);
Assert.AreEqual (50, rects[3].X);
Assert.AreEqual (230, rects[3].Y);
Assert.AreEqual (50, rects[3].Width);
Assert.AreEqual (10, rects[3].Height);
Assert.AreEqual (70, rects[4].X);
Assert.AreEqual (240, rects[4].Y);
Assert.AreEqual (30, rects[4].Width);
Assert.AreEqual (20, rects[4].Height);
rect1 = new Rectangle (20, 330, 40, 50);
rect2 = new Rectangle (50, 340, 40, 50);
rect3 = new Rectangle (70, 360, 30, 50);
rect4 = new Rectangle (80, 400, 30, 10);
rgn1 = new Region (rect1);
rgn2 = new Region (rect2);
rgn3 = new Region (rect3);
rgn4 = new Region (rect4);
rgn1.Union (rgn2);
rgn1.Union (rgn3);
rgn1.Union (rgn4);
rects = rgn1.GetRegionScans (matrix);
Assert.AreEqual (6, rects.Length);
Assert.AreEqual (20, rects[0].X);
Assert.AreEqual (330, rects[0].Y);
Assert.AreEqual (40, rects[0].Width);
Assert.AreEqual (10, rects[0].Height);
Assert.AreEqual (20, rects[1].X);
Assert.AreEqual (340, rects[1].Y);
Assert.AreEqual (70, rects[1].Width);
Assert.AreEqual (20, rects[1].Height);
Assert.AreEqual (20, rects[2].X);
Assert.AreEqual (360, rects[2].Y);
Assert.AreEqual (80, rects[2].Width);
Assert.AreEqual (20, rects[2].Height);
//.........這裏部分代碼省略.........
示例14: Complement_383878
public void Complement_383878 ()
{
using (Region clipRegion = new Region ()) {
clipRegion.MakeInfinite ();
Rectangle smaller = new Rectangle (5, 5, -10, -10);
Rectangle bigger = new Rectangle (-5, -5, 12, 12);
clipRegion.Intersect (smaller);
clipRegion.Complement (bigger);
Assert.IsFalse (clipRegion.IsEmpty (graphic), "IsEmpty");
Assert.IsFalse (clipRegion.IsInfinite (graphic), "IsInfinite");
RectangleF [] rects = clipRegion.GetRegionScans (new Matrix ());
Assert.AreEqual (2, rects.Length, "Length");
Assert.AreEqual (new RectangleF (5, -5, 2, 10), rects [0], "0");
Assert.AreEqual (new RectangleF (-5, 5, 12, 2), rects [1], "1");
}
}
示例15: Rectangle_GetRegionScans
public void Rectangle_GetRegionScans ()
{
Matrix matrix = new Matrix ();
GraphicsPath gp = new GraphicsPath ();
gp.AddRectangle (new Rectangle (10, 10, 10, 10));
Region region = new Region (gp);
Assert.AreEqual (1, region.GetRegionScans (matrix).Length, "1");
gp.AddRectangle (new Rectangle (20, 20, 20, 20));
region = new Region (gp);
Assert.AreEqual (2, region.GetRegionScans (matrix).Length, "2");
}