将图片转换为HTML的神奇方法

Peter Bromberg 提供一种方法将图片文件转换为HTML, 非常神奇。阅读原文。下面是他提供的源码

None.gif using  System;
None.gif
None.gif
using  System.Text;
None.gif
None.gif
using  System.IO;
None.gif
None.gif
using  System.Web;
None.gif
None.gif
using  System.Net;
None.gif
None.gif
using  System.Drawing ;
None.gif
None.gif
namespace  PAB.Web.Utils
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {    
InBlock.gif
InBlock.gif    
public class Image2Html
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
private Image2Html()
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif        
public static string ConvertImage( string imageUrl, int scale)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            WebClient wc 
= new  WebClient();
InBlock.gif
InBlock.gif            
byte[] img = wc.DownloadData(imageUrl);
InBlock.gif
InBlock.gif            
if(img.Length >100000return "<H1><font color=white>Sorry,Image too big for demo!</font></h1>";
InBlock.gif
InBlock.gif            MemoryStream imgStream 
= new MemoryStream(img);
InBlock.gif
InBlock.gif            Bitmap b 
= (Bitmap)Image.FromStream(imgStream);
InBlock.gif
InBlock.gif            MemoryStream ms 
= new MemoryStream();
InBlock.gif
InBlock.gif            StreamWriter SW 
= new StreamWriter(ms);
InBlock.gif
InBlock.gif            SW.WriteLine(
"<!--%<---Clip Here-->");
InBlock.gif
InBlock.gif            SW.WriteLine(
"<style>pre{letter-spacing:-4px;word-spacing:-4px;line-height:2px}</style>"); 
InBlock.gif
InBlock.gif            SW.WriteLine(
"<pre><b><font size='1pt'>");    
InBlock.gif
InBlock.gif            
for(int y=0;y<b.Height;y+=scale) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif
InBlock.gif                
for(int x=0;x<b.Width;x+=scale) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif
InBlock.gif                    SW.Write(
"<font color='#" + b.GetPixel(x,y).Name.Substring(2+ "'>"); 
InBlock.gif
InBlock.gif                    SW.Write( ((
byte)b.GetPixel(x,y).ToArgb())>>7 ); 
InBlock.gif
InBlock.gif                    SW.Write(
"</font>"); 
InBlock.gif
ExpandedSubBlockEnd.gif                }
 
InBlock.gif
InBlock.gif                SW.WriteLine(); 
InBlock.gif
ExpandedSubBlockEnd.gif            }
 
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            SW.WriteLine(
"</font></b></pre>"); 
InBlock.gif
InBlock.gif            SW.WriteLine(
"<!--%<---Clip Here-->");
InBlock.gif
InBlock.gif            SW.Close(); 
InBlock.gif
InBlock.gif            SW 
= null
InBlock.gif
InBlock.gif            
byte[] b2= ms.ToArray ();
InBlock.gif
InBlock.gif            
string s = System.Text.Encoding.ASCII.GetString(b2);
InBlock.gif
InBlock.gif            
return s;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/zwx/archive/2006/07/23/457658.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值