由网友(遗忘丶过去的痛)分享简介:我想创建一个大小160 * 160的位图,并将其与填充一种颜色,每平方分为四个正方形。如何才能做到这一点?I want to create a bitmap of size 160*160 and split it into four squares with each square filled with one...
我想创建一个大小160 * 160的位图,并将其与填充一种颜色,每平方分为四个正方形。如何才能做到这一点?
I want to create a bitmap of size 160*160 and split it into four squares with each square filled with one color. How can this be done?
推荐答案
以防万一有人需要一种方法,在更一般的方式解决这个具体问题,我写了一个扩展方法,以颜色和一个整数,指出有多少瓷砖它应该在x和y方向上分裂开
Just in case anyone needs a method solving this specific problem in a more general way, I wrote an extension method, taking colors and an integer that states how many tiles it should split off in x and y direction:
public static void FillImage(this Image img, int div, Color[] colors)
{
if (img == null) throw new ArgumentNullException();
if (div < 1) throw new ArgumentOutOfRangeException();
if (colors == null) throw new ArgumentNullException();
if (colors.Length < 1) throw new ArgumentException();
int xstep = img.Width / div;
int ystep = img.Height / div;
List<SolidBrush> brushes = new List<SolidBrush>();
foreach (Color color in colors)
brushes.Add(new SolidBrush(color));
using (Graphics g = Graphics.FromImage(img))
{
for (int x = 0; x < div; x++)
for (int y = 0; y < div; y++)
g.FillRectangle(brushes[(y * div + x) % colors.Length],
new Rectangle(x * xstep, y * ystep, xstep, ystep));
}
}
四广场,OP就想将与生产的:
The four squares, the OP wanted would be produced with:
new Bitmap(160, 160).FillImage(2, new Color[]
{
Color.Red,
Color.Blue,
Color.Green,
Color.Yellow
});
相关推荐
最新文章