這是我的第一個silverlight應用程序,我需要將文件保存在我的C:
目錄中。我的Silverlight應用程序將與我的攝像頭建立連接,然後我將拍攝一張快照,然後將其保存在我的C:
目錄中。使用Silverlight將文件保存在磁盤中
看我做了什麼
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
?
錯誤很明顯。您需要升級的特權才能寫入C:目錄,因此選擇一個不需要這些特權的目錄,或者撤消該程序的特權。 –
我試過D:但沒有訪問也。在我的網絡應用程序中,我在磁盤上做了很多寫操作,所以我不知道爲什麼這種方式不起作用。 –