silverlight中 设置 头像(添加图片)
参考文章: http://www.cnblogs.com/wsdj-ITtech/archive/2009/08/26/1554056.html
1. ChainManager.xaml 前台
<Grid Grid.Row="0" Grid.Column="3" Grid.RowSpan="3" Margin="0,0,0,0" HorizontalAlignment="Left">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<Border BorderThickness="1" Width="110" Height="110" Margin="5" BorderBrush="#404040">
<Image x:Name="imgHead" Margin="5" Stretch="Fill"/>
</Border>
<telerik:RadButton Name="btnPhoto" Grid.Column="1" Content="选择" Width="40" Height="25" Click="btnPhoto_Click" TabIndex="3"/>
</Grid>
2. XAMLChainManager.cs 后台
设定全局变量
System.IO.FileInfo fileinfo = null; //获取选定图片信息
/// <summary>
/// 获取图片文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPhoto_Click(object sender, RoutedEventArgs e)
{
try
{
/*
* OpenWriteCompleted - 在打开用于上传的流完成时(包括取消操作及有错误发生时)所触发的事件
* WriteStreamClosed - 在写入数据流的异步操作完成时(包括取消操作及有错误发生时)所触发的事件
* UploadProgressChanged - 上传数据过程中所触发的事件。如果调用 OpenWriteAsync() 则不会触发此事件
* Headers - 与请求相关的的标头的 key/value 对**
* OpenWriteAsync(Uri address, string method, Object userToken) - 打开流以使用指定的方法向指定的 URI 写入数据
* Uri address - 接收上传数据的 URI
* string method - 所使用的 HTTP 方法(POST 或 GET)
* Object userToken - 需要上传的数据流
*/
// 获取图片文件
OpenFileDialog openFileDialog = new OpenFileDialog()
{
Filter = "JPEG Images (*.jpg,*.jpeg)|*.jpg;*.jpeg|PNG Images (*.png)|*.png|All images|*.png;*.jpg;*.jpeg",
Multiselect = false
};
if (openFileDialog.ShowDialog() == true)//.DialogResult.OK)
{
fileinfo = openFileDialog.File;
if (fileinfo != null)
{
// 图片显示在页面空间里
RadBitmap vwatermarkBitmap = new RadBitmap(fileinfo.OpenRead());
this.imgHead.Source = vwatermarkBitmap.Bitmap;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 图片上传到服务器上
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSave_Click(object sender, RoutedEventArgs e)
{
try
{
if (fileinfo != null)
{
#region 把图片上传到服务器上
WebClient webclient = new WebClient();
Uri WebUri = new Uri(Application.Current.Host.Source, "../"); //获取网站根目录
Uri upTargetUri = new Uri(String.Format(WebUri + "WebClientUpLoadStreamHandler.ashx?fileName={0}", fileinfo.Name.ToString()), UriKind.Absolute); //指定上传地址
webclient.OpenWriteCompleted += new OpenWriteCompletedEventHandler(Webclient_OpenWriteCompleted);
webclient.Headers["Content-Type"] = "multipart/form-data";
webclient.OpenWriteAsync(upTargetUri, "POST", fileinfo.OpenRead());
webclient.WriteStreamClosed += delegate(object sender1, WriteStreamClosedEventArgs f)
{
if (f.Error != null)
{
System.Windows.Browser.HtmlPage.Window.Alert(f.Error.Message.ToString());
}
};
#endregion
}
}
catch (Exception)
{
RadWindow.Alert("保存失败,请稍后重试!");
}
}
/// <summary>
/// 将图片数据流发送到服务器上
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Webclient_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
{
Stream clientStream = e.UserState as Stream; // 需要上传的流(客户端流)
Stream serverStream = e.Result; // 目标地址的流(服务端流)
byte[] buffer = new byte[4096];
int readcount = 0;
// clientStream.Read - 将需要上传的流读取到指定的字节数组中
while ((readcount = clientStream.Read(buffer, 0, buffer.Length)) > 0)
{
// serverStream.Write - 将指定的字节数组写入到目标地址的流
serverStream.Write(buffer, 0, readcount);
}
serverStream.Close();
clientStream.Close();
}
在数据存的是文件名称 (代码这里省略):
chain.ImgName = fileinfo == null ? "" : fileinfo.Name.ToString(); //获取所选文件的名字;
从数据库中读到文件名称后,显示在页面上:
imgHead.Source = new BitmapImage(new Uri("/Pics/" + chain.ImgName, UriKind.Relative));
3. 在服务端添加文件(WebClientUpLoadStreamHandler.ashx),如图

代码实现如下:
/// <summary>
/// WebClientUpLoadStreamHandler 的摘要说明
/// </summary>
public class WebClientUpLoadStreamHandler : IHttpHandler
{
public bool IsReusable
{
get
{
return false;
}
}
public void ProcessRequest(HttpContext context)
{
//获取上传的数据流
string fileNameStr = context.Request.QueryString["fileName"];
Stream sr = context.Request.InputStream;
try
{
string filename = "";
filename = fileNameStr;
byte[] buffer = new byte[4096];
int bytesRead = 0;
//将当前数据流写入服务器端文件夹ClientBin下
string targetPath = context.Server.MapPath("/ClientBin/Pics/" + filename);
using (FileStream fs = File.Create(targetPath, 4096))
{
while ((bytesRead = sr.Read(buffer, 0, buffer.Length)) > 0)
{
//向文件中写信息
fs.Write(buffer, 0, bytesRead);
}
}
context.Response.ContentType = "text/plain";
context.Response.Write("上传成功");
}
catch (Exception e)
{
context.Response.ContentType = "text/plain";
context.Response.Write("上传失败, 错误信息:" + e.Message);
}
finally
{
sr.Dispose();
}
}
}
4.并且在服务端建立文件夹 Pics,储存上传到服务器的图片,如图:

silverlight中 设置 头像(添加图片)的更多相关文章
- Silverlight中文本框添加回车事件后,换行无法清除的解决方法
在开发Silverlight的项目中,为了更好的用户体验,我们常要给一些控件添加一些快捷键.然而,在Silverlight中当用户回车提交后,光标停留在文本框的第二行怎么也清除不掉,经过一段时间研究, ...
- Gtk 窗口,控件,设置(添加图片等)
1.关于窗口 // 创建顶层窗体,后面有POPUP的 GtkWidget *main_window; main_window = gtk_window_new (GTK_WINDOW_TOPLEV ...
- IOS 在IOS6中设置navigationBar背景图片 会有一条 黑色阴影 --- 解决方案
//给navigationBar设置背景图片 if ([self.navigationController.navigationBar respondsToSelector:@selector(set ...
- GitHub 中 readme 如何添加图片
一.Readme 是什么 readme文件一般是放在github 每个repo的根目录下,用来解释.说明本repo的主要内容和相关信息.而且在repo主页进去的时候会被自动加载.一般采用md标记的文本 ...
- 在tableView中设置cell的图片和文字
// 设置UITableViewCellEditingStyle的 accessoryType UITableViewCellAccessoryNone, // d ...
- markdown中设置、调整图片尺寸
使用百分比描述尺寸 <img src="https://img2018.cnblogs.com/blog/1122471/201902/1122471-2019022218575673 ...
- 一步一步学Silverlight 2系列(20):如何在Silverlight中与HTML DOM交互(下)
述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- 关于在Silverlight中添加图片的问题
在Silverlight中添加图片,目前支持的Image格式有jpg和png两种,如何在目录中添加,有些什么技巧呢? <StackPanel Background="White&quo ...
- Silverlight 中图片路径的设置
在Silverlight中图片的设置方法有几种 如上图在一个工程中有个images文件夹,buttons.xaml页面中的Image控件要引用一张图片 第一种方法 xaml: <Image x: ...
随机推荐
- 微信小程序onlaunch异步,首页onLoad先执行?
按照原理是小程序初始化时会先触发APP里的onLaunch事件,之后再执行页面Page里的onLoad事件.但实际请求时在onLaunch事件中请求获取数据,等待返回值的时候Page里的onLoad事 ...
- JavaScript前端面试题总结
1.em和rem 像素(px):用于元素的边框或定位. em/rem:用于做响应式页面,em相对于父元素,rem相对于根元素. rem 单位翻译为像素值是由 html 元素的字体大小决定的. 此字体大 ...
- 229. Majority Element II求众数II
网址:https://leetcode.com/problems/majority-element-ii/ 参考:https://blog.csdn.net/u014248127/article/de ...
- canvas和SVG
Canvas的介绍 1.1.创建canvas元素 canvas的定义:它是HTML5中新增一个HTML5标签与操作canvas的javascript API,它可以实现在网页中完成动态的2D与3D图像 ...
- nginx 代理 https 后,应用变成 http
需求:nginx 代理 https,后面的 tomcat 处理 http 请求,sso 的客户端,重定向时需要带上 target,而这个 target 默认是 tomcat 的 http,现在需要把这 ...
- 电梯问题——致敬ACM
The Fair Nut and Elevator time limit per test 1 second memory limit per test 256 megabytes inp ...
- MapReduce(二) MR的高级特性-序列化、排序、分区、合并
一.序列化 (*) 核心接口:Writable接口.如果有一个类实现了Writable接口,就可以作为Map/Reduce的key和value. 举例: 读取员工数据,生成员工对象,直接存储 ...
- vim常用指令
命令历史 以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中输入以下命令即可 vim 直接启动vim vim filename 打开vim ...
- mybatis支持jdk8等localdate类型
大家知道,在实体Entity里面,可以使用java.sql.Date.java.sql.Timestamp.java.util.Date来映射到数据库的date.timestamp.datetime等 ...
- 零基础IDEA整合SpringBoot + Mybatis项目,及常见问题详细解答
开发环境介绍:IDEA + maven + springboot2.1.4 1.用IDEA搭建SpringBoot项目:File - New - Project - Spring Initializr ...