[英]How to save image in Silverlight
嗨,我创建了一个Silverlight应用程序,该应用程序允许用户键入他们的姓名,选择日期并在其姓名上签名(签名栏)。 我希望将其添加到已经创建的网络表单中。 我在gridlight上的Silverlight中使用borderInk和inkP工具构建了签名条。 但是我不知道如何保存图像。 我想将其存储在已经创建的数据库中。 我还想将Silverlight应用程序附加到我创建的Web窗体中。 有什么帮助吗?
您应该将绘图表面元素(网格)渲染到位图并保存结果。
应该获取一个元素并返回jpeg图像字节的示例方法
private byte[] RenderToJpeg(FrameworkElement element)
{
using (var stream = new MemoryStream())
{
var bmp = new WriteableBitmap(element, null);
bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 90);
stream.Flush();
return stream.ToArray();
}
}
如果使用OpenFileDialog
保存图像,则可以为您提供帮助。
decimal _imagementSize = 0;
string _imageName = "";
string _imageType = "";
Binary _image;
OpenFileDialog dialog=new OpenFileDialog();
private void btnSaveImage_Click(object sender, RoutedEventArgs e)
{
dialog.Multiselect = false;
dialog.Filter = "All Files | *.*";
if (dialog.ShowDialog() == true)
{
bool fileExist = dialog.File.Exists;
if (fileExist)
{
UploadFile();
}
}
}
private void UploadFile()
{
double fileLength = 0;
var stream = dialog.File.OpenRead();
var bnr = new BinaryReader(stream);
byte[] buffer = new byte[stream.Length + 1];
buffer = bnr.ReadBytes((int)stream.Length);
fileLength = stream.Length;
_imageName = dialog.File.Name;
_imageType = dialog.File.Extension;
_imageSize = (decimal)(fileLength / 1024);
_image = new Binary() { Bytes = buffer };
}
如果您使用WCF service
保存图像,则只需发送_image
和WCF method
一样
[OperationContract]
public void SaveImage(System.Data.Linq.Binary _image)
{
//save image to DB or enything
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.