博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Cool!!将图片转换为HTML图片
阅读量:7190 次
发布时间:2019-06-29

本文共 1471 字,大约阅读时间需要 4 分钟。

嘿嘿,就是将图片转换为HTML代码(DIV点阵),也就是将图片的每个象素点都用DIV来实现,这样一张HTML图片就出来了:) 
为了避免IE暂停响应,转换的图片不要太大.要不然转换出来也不敢看!
比如我们将验证码图片输出为HTML代码,这样要破解的话就让对方去还原这副图片吧:) 
大家看看下面这张图(嘿嘿,不是图片来的,注意别用鼠标拖动选择,要不然我怕你的浏览器会暂停响应!)的效果:
代码很少很简单,就只有两个函数,如下: 
        
public
 
static
 
void
 CovertImageToHtml(
string
 imageFile, 
string
 fileName)
        
{
            
using (Bitmap image = new Bitmap(imageFile))
            
{
                CovertImageToHtml(image, fileName); 
            }
        }
        
public
 
static
 
void
 CovertImageToHtml(Bitmap image, 
string
 fileName)
        
{
            
using (StreamWriter writer = new StreamWriter(fileName, false, Encoding.Default, 1024))
            
{
                
//定义CSS样式
                writer.WriteLine("<style>");
                writer.WriteLine(
"#htmlpic{
{width:{0}px;height:{1}px;}}
", image.Width, image.Height);
                writer.WriteLine(
"#htmlpic div{float:left;height:1px;overflow:hidden;}");
                writer.WriteLine(
"</style>");
                
//输出图片数据
                writer.WriteLine("<div id=\"htmlpic\">");
                
for (int h = 0; h < image.Height; h++)
                
{
                    Color preColor 
= image.GetPixel(0, h);     //获取第一点的颜色值
                    int count = 1;
                    
for (int w = 1; w < image.Width; w++)
                    
{
                        Color nowColor 
= image.GetPixel(w, h);
                        
if (preColor == nowColor)
                        
{
                            count
++;
                        }
                        
else
                        
{
                            writer.WriteLine(
"<div style=\"background:{
0}
;width:{
1}
px\"></div>", ColorTranslator.ToHtml(preColor), count);
                            count 
= 1;
                            preColor 
= nowColor;
                        }
                    }
                    
//写入最后的数据
                    writer.WriteLine("<div style=\"background:{
0}
;width:{
1}
px\"></div>", ColorTranslator.ToHtml(preColor), count);
                    writer.WriteLine();
                }
                writer.WriteLine(
"</div>");
            }
        }
本文转自Kingthy博客园博客,原文链接:http://www.cnblogs.com/kingthy/archive/2008/05/09/1189664.html
,如需转载请自行联系原作者
你可能感兴趣的文章
Unity4.5版本DLL库名字问题
查看>>
打造一个集Java,C/C++,Python开发与一体的eclipse neon.3-32bit
查看>>
C#进阶之路(六):表达式进行类的赋值
查看>>
SQL夯实基础(八):联接运算符算法归类
查看>>
如何使用JMeter来实现更大批量的并发的解决方案(即如何设置controller和Agent)
查看>>
第七天
查看>>
poj2823
查看>>
Linux 必会
查看>>
HTML骨架结构
查看>>
【哈希表】CodeVs1230元素查找
查看>>
自定义上传图片样式并实现上传立即展示该图片
查看>>
通达OA 自定义菜单
查看>>
Excutors 线程池
查看>>
BootStrap的table技术小结:数据填充、分页、列宽可拖动
查看>>
用HTML做的简单的个人简历
查看>>
MySQL高级 之 order by、group by 优化
查看>>
POJ3320:Jessica's Reading Problem(尺取法)
查看>>
Mysql学习——安装登录
查看>>
React-nwb的使用
查看>>
【人生思考】2015读书清单
查看>>