1.Silverlight只能显示网站项目目录的ClIEntBin目录下的图片,也可以在ClIEntBin目录下建子文件夹进行显示
,例如ClIEntBin/images目录
2.Silverlight上传图片一种方法是使用WebClIEnt,服务端使用一般处理程序.ashx
参考代码:
*******************************Silverlight客户端代码*****************************************************
using System;
using System.Collections.Generic;
using System.linq;
using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;
using System.windows.Navigation;
using System.windows.Media.Imaging;
using System.IO;
namespace Silverligh图片相册
{
public partial class AddProduct : Page
{
public string mStrImagefilename = "";
public fileInfo mObjImagefileInfo = null;
private wcfMain.IwcfMainClIEnt clIEnt = null;
OpenfileDialog dlg = null;
public AddProduct()
{
InitializeComponent();
clIEnt = ServerManager.GetPox();
clIEnt.AddProductCompleted += new EventHandler<wcfMain.AddProductCompletedEventArgs>(clIEnt_AddProductCompleted);
}
voID clIEnt_AddProductCompleted(object sender,wcfMain.AddProductCompletedEventArgs e)
{
if (e.Error == null)
{
Dictionary<string,string> dicGet = e.Result;
if (dicGet["Result"] == "OK")
{
MessageBox.Show("添加产品成功!");
mStrImagefilename = "";
mObjImagefileInfo = null;
txtProductname.Text = "";
imgSource.source = new BitmAPImage(
new Uri("",UriKind.relative));
}
else
{
MessageBox.Show("添加产品失败!" + dicGet["Result"]);
}
}
else
{
MessageBox.Show(e.Error.InnerException.ToString());
}
}
// 当用户导航到此页面时执行。
protected overrIDe voID OnNavigatedTo(NavigationEventArgs e)
{
}
private voID btnExit_Click(object sender,RoutedEventArgs e)
{
SessionManager.Session["Login"] = "N";
this.Content = new ShowPicMain();
}
private voID btnSelimage_Click(object sender,RoutedEventArgs e)
{
//这里在客户端显示图片,并显示在Image控件中
dlg = new OpenfileDialog();
dlg.Multiselect = false;
dlg.Filter = "JPG 图片 (*.jpg)|*.jpg|PNG 图片 (*.png)|*.png";
bool? result = dlg.ShowDialog();
if (result != null && result == true)
{
mStrImagefilename = dlg.file.name;
mObjImagefileInfo = dlg.file;
BitmAPImage image = new BitmAPImage();
image.SetSource(dlg.file.OpenRead());
imgSource.source = image;
dlg.file.OpenRead().Close();
}
}
#region 关键代码,使用WebClIEnt调用一般处理程序,上传图片
private voID uploadImage(string filename,Stream data)
{
Uri uri = new Uri(string.Format("/UploadImageHandler.ashx?filename={0}",filename),UriKind.relative);
WebClIEnt clIEnt = new WebClIEnt();
clIEnt.OpenWriteCompleted += delegate(object s,OpenWriteCompletedEventArgs e)
{
uploadData(data,e.Result);
e.Result.Close();
data.Close();
};
clIEnt.OpenWriteAsync(uri);
}
#endregion
private voID uploadData(Stream input,Stream output)
{
//这里要注意,传入的输入流不能是关闭的,否则不能成功上传图片
byte[] buffer = new byte[4096];
int bytes;
while ((bytes = input.Read(buffer,buffer.Length)) != 0)
{
output.Write(buffer,bytes);
}
}
private voID btnAddProduct_Click(object sender,RoutedEventArgs e)
{
if (txtProductname.Text!="")
{
clIEnt.AddProductAsync(txtProductname.Text,"/images/" + mStrImagefilename);
}
else
{
MessageBox.Show("产品名称和图片不能为空!");
}
}
private voID btnUpload_Click(object sender,RoutedEventArgs e)
{
if (mStrImagefilename != "" && mObjImagefileInfo != null)
{
uploadImage(mStrImagefilename,mObjImagefileInfo.OpenRead());
MessageBox.Show(string.Format("上传成功!{0}",
mStrImagefilename));
}
}
private voID btnReturn_Click(object sender,RoutedEventArgs e)
{
this.Content = new ShowPicMain();
}
}
}
*******************************一般处理程序服务端代码*****************************************************
using System;
using System.Collections.Generic;
using System.linq;
using System.Web;
using System.IO;
namespace Silverligh图片相册.Web
{
/// <summary>
/// UploadImageHandler 的摘要说明
/// </summary>
public class UploadImageHandler : IhttpHandler
{
public voID ProcessRequest(httpContext context)
{
//关键代码,把图片存储在网站的ClIEntBin/images目录下
context.Response.ContentType = "text/plain";
string filename = context.Request.queryString["filename"].ToString();
string strSavefilename = context.Server.
MapPath("~/ClIEntBin/images/" + filename);
if (file.Exists(strSavefilename))
{
file.Delete(strSavefilename);
}
using(fileStream fs = file.Create(strSavefilename))
{
SaveImage(context.Request.inputStream,fs);
}
}
private voID SaveImage(Stream stream,fileStream fs)
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = stream.Read(buffer,buffer.Length)) != 0)
{
fs.Write(buffer,bytesRead);
}
}
public bool IsReusable { get { return false; } } } }
总结以上是内存溢出为你收集整理的Silverlight上传图片到网站目录并显示全部内容,希望文章能够帮你解决Silverlight上传图片到网站目录并显示所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)