[英]Save file in disk with Silverlight
这是我的第一个silverlight应用程序,我需要将文件保存在C:
目录中。 我的Silverlight应用程序将与网络摄像头建立连接,然后创建快照,然后将其保存在C:
目录中。
看看我做了什么
protected void photoButton_Click(object sender, RoutedEventArgs e)
{
this.src.CaptureImageCompleted += (s, a) =>
{
this.lastSnapshot = a.Result;
this.snapshot.Visibility = Visibility.Visible;
this.snapshot.Source = this.lastSnapshot;
this.src.Stop();
if (this.lastSnapshot != null)
{
var pngStream = this.GetPngStream(lastSnapshot);
byte[] binaryData = new Byte[pngStream.Length];
long bytesRead = pngStream.Read(binaryData, 0, (int)pngStream.Length);
WriteBytesToFile("imagem.png", binaryData);
}
};
src.CaptureImageAsync();
}
static public void WriteBytesToFile(string fileName, byte[] content)
{
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
try
{
w.Write(content);
}
finally
{
fs.Close();
w.Close();
}
}
protected Stream GetPngStream(WriteableBitmap bmp)
{
// Use Joe Stegman's PNG Encoder
// http://bit.ly/77mDsv
EditableImage imageData = new EditableImage(bmp.PixelWidth, bmp.PixelHeight);
for (int y = 0; y < bmp.PixelHeight; ++y)
{
for (int x = 0; x < bmp.PixelWidth; ++x)
{
int pixel = bmp.Pixels[bmp.PixelWidth * y + x];
imageData.SetPixel(x, y,
(byte)((pixel >> 16) & 0xFF),
(byte)((pixel >> 8) & 0xFF),
(byte)(pixel & 0xFF),
(byte)((pixel >> 24) & 0xFF)
);
}
}
return imageData.GetStream();
}
在我的WriteBytesToFile
,出现错误File operation not permitted. Access to path is denied.
File operation not permitted. Access to path is denied.
。 如何将快照保存在名为imagem.png
C:
目录中?
最好使用File.WriteAllBytes(string path,byte [] data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.