本文整理汇总了C#中Canvas.ReduceResolution方法的典型用法代码示例。如果您正苦于以下问题:C# Canvas.ReduceResolution方法的具体用法?C# Canvas.ReduceResolution怎么用?C# Canvas.ReduceResolution使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Canvas
的用法示例。
在下文中一共展示了Canvas.ReduceResolution方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Test_Canvas_ReduceResolution
public void Test_Canvas_ReduceResolution()
{
Canvas canvas = new Canvas(new Resolution(8, 5));
#region Filling canvas
for (int y = 0; y < 4; y++)
for (int x = 0; x < 6; x++)
{
if((x == 4 && y == 0) ||
(x == 5 && y == 0) ||
(x == 5 && y == 1))
canvas.SetColor(x, y, Color.Green);
else if ((x == 0 && y == 2) ||
(x == 1 && y == 2) ||
(x == 2 && y == 2) ||
(x == 3 && y == 2) ||
(x == 2 && y == 3))
canvas.SetColor(x, y, Color.Blue);
else canvas.SetColor(x, y, Color.Red);
}
#endregion
Canvas expectedCanvas = new Canvas(new Resolution(3, 2));
#region Filling expected canvas
for(int y = 0; y < 2; y++)
for (int x = 0; x < 3; x++)
{
if (x == 2 && y == 0)
expectedCanvas.SetColor(x, y, Color.FromArgb(255, 63, 96, 0));
else if (x == 1 && y == 1)
expectedCanvas.SetColor(x, y, Color.FromArgb(255, 63, 0, 191));
else if (x == 0 && y == 1)
expectedCanvas.SetColor(x, y, Color.FromArgb(255, 127, 0, 127));
else
expectedCanvas.SetColor(x, y, Color.FromArgb(255, 255, 0, 0));
}
#endregion
Canvas actualCanvas = canvas.ReduceResolution(3, 2, 2);
Assert.IsTrue(actualCanvas.Width == expectedCanvas.Width && actualCanvas.Height == expectedCanvas.Height);
for (int y = 0; y < actualCanvas.Height; y++)
for (int x = 0; x < actualCanvas.Width; x++)
{
Color actualColor = actualCanvas.GetColor(x, y);
Color expectedColor = expectedCanvas.GetColor(x, y);
Assert.IsTrue(actualColor == expectedColor);
}
}
示例2: Generate
public Canvas Generate(Canvas canvas, Settings settings)
{
if (settings.Palette == null || settings.Palette.Count == 0)
throw new NullReferenceException("Check that Palette isn't null or it has any colors");
if (settings.CellsCount <= 0)
throw new WrongInitializedException("Square count has to be initialized and more than zero");
if (canvas.Width < settings.CellsCount)
throw new WrongResolutionException("Image's width must be higher or input less cells");
int cellWidth = canvas.Width / settings.CellsCount;
if (canvas.Height < cellWidth)
throw new WrongResolutionException("Image's height must be higher");
int newHeight = canvas.Height / cellWidth;
int newWidth = settings.CellsCount;
Canvas tempCanvas = canvas.ReduceResolution(newWidth, newHeight, cellWidth);
List<Color> colors = settings.Palette.GetAllColorsList();
//Parallel.For with Partition
Parallel.ForEach(Partitioner.Create(0, tempCanvas.Height), rangeHeight =>
{
for (int y = rangeHeight.Item1; y < rangeHeight.Item2; y++)
Parallel.ForEach(Partitioner.Create(0, tempCanvas.Width), rangeWidth =>
{
for (int x = rangeWidth.Item1; x < rangeWidth.Item2; x++)
{
Color oldColor = tempCanvas.GetColor(x, y);
Color colorAmoung = ChooseColorAmoung(oldColor, colors);
tempCanvas.SetColor(x, y, colorAmoung);
}
});
});
#region Just Parallel.For
/*
Parallel.For(0, tempCanvas.Height, y =>
{
Parallel.For(0, tempCanvas.Width, x =>
{
Color oldColor = tempCanvas.GetColor(x, y);
Color colorAmoung = ChooseColorAmoung(oldColor, colors);
tempCanvas.SetColor(x, y, colorAmoung);
});
});
*/
#endregion
#region Obsolete realization
/*for (int y = 0; y < tempCanvas.Height; y++)
for (int x = 0; x < tempCanvas.Width; x++)
{
Color oldColor = tempCanvas.GetColor(x, y);
Color colorAmoung = ChooseColorAmoung(oldColor, colors);
tempCanvas.SetColor(x, y, colorAmoung);
}
*/
#endregion
return tempCanvas;
}