-1

这是我的第一个 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

4

2 回答 2

1

Silverlight applications run in a sandbox by default and do not have any direct access to the file system. For a Silverlight application to have access to the local file system it must be installed as a trusted applicaiton. A trusted Silverlight 5 application will have access to the entire hard drive, but a Silverlight 4 application will only have access to the MyDocuments, MyMusic, MyPictures, and MyVideos folders.

于 2012-11-13T12:57:22.153 回答
1

It's better to use File.WriteAllBytes(string path, byte[] data)

于 2015-05-14T12:26:49.937 回答