一到周末,就变得好伤感,似乎每个周末,P林去家教,发哥回家,张导到隔壁打机,宿舍留我一个人空荡荡,然后苦逼苦逼的写代码。越写越疼。。。
前几天看到几张图片,感觉挺好看,是把图片转换为马赛克的效果,并在马赛克上打上边框,加上马赛克后变成另一种味道,其实,有码也不错
那个叫自己的才是我最好的朋友
手绘图片,有点十字绣的感觉
冬天走了
最近在做图像处理,就顺便把程序给实现出来
原理很简单
1、设置马赛克大小,边框宽度,边框颜色
2、根据马赛克大小,边框宽度,给图片重设大小,并计算出新图片的大小
3、在新图片中画出颜色和边框
代码如下(这里使用之前发过的指针法操作图片PointerBitmap类 http://www.cnblogs.com/bomo/archive/2013/02/26/2934055.html):
定义三个变量:马赛克大小,边框宽度,边框颜色
int borderwidth = 1; int mosaicwidth = 3; Color bordercolor = Color.FromArgb(211, 172, 158);
重设大小
public Bitmap Resize(Bitmap source, Size size) { int widthskip = source.Width / size.Width; int heightskip = source.Height / size.Height; Bitmap bmp = new Bitmap(size.Width, size.Height); PointerBitmap pbmp = new PointerBitmap(source); PointerBitmap newpbmp = new PointerBitmap(bmp); pbmp.LockBits(); newpbmp.LockBits(); for (int height = 0; height < newpbmp.Height; height++) { for (int width = 0; width < newpbmp.Width; width++) { int x = width > 0 ? 1 + (width - 1) * widthskip : 0; int y = height > 0 ? 1 + (height - 1) * heightskip : 0; Color c = pbmp.GetPixel(x, y); newpbmp.SetPixel(width, height, c); } } pbmp.UnlockBits(); newpbmp.UnlockBits(); return bmp; }
生产马赛克图像
//生成马赛克图像 public Bitmap CreateMosaicImage(Bitmap source) { //计算新图像需要的尺寸 int widthcount = source.Width / mosaicwidth; int heightcount = source.Height / mosaicwidth; int newwidth = widthcount * mosaicwidth + (widthcount + 1) * borderwidth; int newheight = heightcount * mosaicwidth + (heightcount + 1) * borderwidth; Bitmap bmp = new Bitmap(newwidth, newheight); source = Resize(source, new Size(widthcount, heightcount)); PointerBitmap sourcepbmp = new PointerBitmap(source); PointerBitmap newpbmp = new PointerBitmap(bmp); sourcepbmp.LockBits(); newpbmp.LockBits(); //绘制背景 for (int height = 0; height < newpbmp.Height; height++) { for (int width = 0; width < newpbmp.Width; width++) { newpbmp.SetPixel(width, height, bordercolor); } } for (int height = 0; height < sourcepbmp.Height; height++) { for (int width = 0; width < sourcepbmp.Width; width++) { int x = borderwidth * (width + 1) + mosaicwidth * width; int y = borderwidth * (height + 1) + mosaicwidth * height; Color c = sourcepbmp.GetPixel(width, height); for (int k = 0; k < mosaicwidth; k++, y++, x-=mosaicwidth) { for (int l = 0; l < mosaicwidth; l++, x++) { newpbmp.SetPixel(x, y, c); } } } } sourcepbmp.UnlockBits(); newpbmp.UnlockBits(); return bmp; }
博客园突然传不了文件,程序传到百度盘
http://pan.baidu.com/share/link?shareid=454661&uk=1008393043
请发表评论