c# - 位图马赛克
问题描述
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;
我希望获得如下的莉娜马赛克:
但是,得到了一个扭曲的图像:
什么地方出了错?
解决方案
您得到的正是您所要求的:) 例如,对于 x 和 y 为 0,您将像素设置为 (0,0) 七次(而不是九次)。您需要偏移图像的宽度和高度。
对于 3x3 马赛克,将 SetPixel 语句替换为
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);
}
推荐阅读
- django - DJango - 将模型中的 Json 字段与其他字段一起导出为 CSV
- c - Ruby C API 宏,例如 INT2NUM() 返回/存储与预期不同的数字
- iot - 节点红色无法监听 http://127.0.0.1:1880
- python - DjangoCMS add childplugin button
- c - 合并排序程序不对超过 5 个元素的数组进行排序
- java - 尝试使用不同的远程数据源。失败 - jndi 解决异常
- postgresql - About the implementation details of PostgreSQL sequence
- php - 按 id 对数组进行分组并检查
- c# - 使用配置文件指定连接字符串来定义单元测试中的数据源
- c++ - 将 void* 强制转换为具有多重继承的类