繁体   English   中英

使用Silverlight将文件保存到磁盘中

[英]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:目录中?

Silverlight应用程序默认沙箱中运行,并且没有任何直接访问文件系统的权限。 为了使Silverlight应用程序能够访问本地文件系统,必须将其安装为受信任的应用程序 受信任的Silverlight 5应用程序将有权访问整个硬盘驱动器,但Silverlight 4应用程序将仅能访问MyDocuments,MyMusic,MyPictures和MyVideos文件夹。

最好使用File.WriteAllBytes(string path,byte [] data)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM