Silverlight中用Stream作为图片源进行加载

几经周折..发现自己真的很笨! 有很多人想用Silverlight做个验证码之类的东西,但是考虑到Silverlight没有画图的功能就都直接用ASP.net给实现了. 今天想了想..有个笨办法~ 搞一个WCF服务,里面写

 

[OperationContract]
public object[] AttackDef()
{
    object[] c = new object[2];
    Bitmap B = new Bitmap(20, 20);
    Graphics G = Graphics.FromImage(B);
    G.Clear(Color.Black);
    G.Dispose();
    MemoryStream S = new MemoryStream();
    B.Save(S, System.Drawing.Imaging.ImageFormat.Png);
    B.Dispose();
    c[0] = S.ToArray();
    c[1] = "一些数据";
    return c;
}
 
当然,什么都没做,只是为了测试和构建一个可行的方案~
 
在SL程序里面:
(SLC是WCF的Proxy对象)
 
先调用
 
SLC.AttackDefCompleted += new EventHandler<AttackDefCompletedEventArgs>(SLC_AttackDefCompleted); 
SLC.AttackDefAsync();
   
然后有
void SLC_AttackDefCompleted(object sender, AttackDefCompletedEventArgs e)
 {
     
     BitmapImage B = new BitmapImage();
     MemoryStream M = new MemoryStream();
     M.Write((byte[])e.Result[0], 0, ((byte[])e.Result[0]).Length);
     B.SetSource(M);
     你的Image控件.Source = B;
 }
 
于是。。图片就被解码,加码,解码。。出来了。
中间有很多非常倒霉的问题,比如说图片格式,如果设定成BMP就会Crash。它告诉你:
毁灭性错误。
。。我也毁灭了。。。。
 
很简单的东西,只是框架和可行性的实验,要是需要使用,请更改WCF服务中的逻辑,String是为了传回核对数据用的。。WCF服务不太适用远程直接核对。。太不安全了。
 
高手们直接忽略我的记事。。这。。。。见笑
posted @ 2009-01-20 21:47  struggle-luan  阅读(1550)  评论(7编辑  收藏  举报