• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

【C#】马赛克的艺术

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

  一到周末,就变得好伤感,似乎每个周末,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

 

转载于:https://www.cnblogs.com/bomo/archive/2013/03/22/2976402.html


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#开发: 准备工作-C# 新建工程 - 杨奉武发布时间:2022-07-10
下一篇:
[转]C# Invalidate()方法 用处发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap