这是我的第一个 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.
中。如何C:
使用名称将快照保存在我的目录中imagem.png
?