Bitmap source = (Bitmap)Bitmap.FromFile(@"lena_std.tif");
Bitmap dest = new Bitmap(source.Width * 3, source.Height * 3, source.PixelFormat);
for (int y = 0; y < source.Height; y++)
{
for (int x = 0; x < source.Width; x++)
{
Color clr = source.GetPixel(x, y);
dest.SetPixel(x, y, clr);
dest.SetPixel(x, y * 2, clr);
dest.SetPixel(x * 2, y, clr);
dest.SetPixel(x * 2, y * 2, clr);
dest.SetPixel(x * 3, y, clr);
dest.SetPixel(x, y * 3, clr);
dest.SetPixel(x * 3, y * 3, clr);
}
}
pictureBox1.Image = dest;
我希望获得Lena的马赛克,如下所示:
但是,反而获得了失真的图像:
出了什么问题?
答案 0 :(得分:1)
您应该设置x + offset而不是x * 2。另外,您希望获得9张图像=>您应该设置9项
for (int y = 0; y < source.Height; y++)
{
for (int x = 0; x < source.Width; x++)
{
Color clr = source.GetPixel(x, y);
dest.SetPixel(x, y, clr);
dest.SetPixel(x, y + source.Height, clr);
dest.SetPixel(x + source.Width, y, clr);
dest.SetPixel(x + source.Width, y + source.Height, clr);
dest.SetPixel(x + (source.Width*2), y, clr);
dest.SetPixel(x, y + (source.Height*2), clr);
dest.SetPixel(x + (source.Width*2), y + (source.Height*2), clr);
dest.SetPixel(x + (source.Width*2), y + source.Height, clr);
dest.SetPixel(x + source.Width, y + (source.Height*2), clr);
}
}
这对您有用吗?
答案 1 :(得分:1)
您得到的正是您想要的:)例如,对于x和y为0,您将像素设置为(0,0)七次(而不是九次)。您需要使用图像的宽度和高度进行补偿。
将SetPixel语句替换为3x3马赛克的行,
for (j=0; j<3; j++)
for (i=0; i<3; i++)
dest.SetPixel(i*source.Width+x, j*source.Height+y, clr);
总共:
for (y = 0; y < source.Height; y++)
for (x = 0; x < source.Width; x++)
{
Color clr = source.GetPixel(x, y);
for (j=0; j<3; j++)
for (i=0; i<3; i++)
dest.SetPixel(i*source.Width+x, j*source.Height+y, clr);
}
答案 2 :(得分:0)
您可以使用TextureBrush绘制图块:
Image image = (Bitmap)Bitmap.FromFile(@"lena_std.tif");
TextureBrush tBrush = new TextureBrush(image);
e.Graphics.DrawRectangle(blackPen, new Rectangle(0, 0, 200, 200));