繁体   English   中英

如何设置UWP C#StorageFile将SoftwareBitmap存储到特定路径

[英]how to set up UWP C# StorageFile to store a SoftwareBitmap to a specific path

我正在尝试将SoftwareBitmap保存到文件。

但是当我这样做时,出现错误“参数不正确”:StorageFile.GetFileFromPathAsync()

这是错误.....

在此处输入图片说明

pathname =“ diag ProcessFrame .JPG”

WEBCAM USB框架的事件处理程序代码…………

        private void FrameReader_FrameArrived(MediaFrameReader sender, MediaFrameArrivedEventArgs args)
    {
        // Get frame image from camera:
        using (var frame = sender.TryAcquireLatestFrame())    //  ==>  MediaFrameReference 
        {
            // Got image?
            if (frame != null)
            {
                var renderer = _frameRenderers[frame.SourceKind];

                        renderer.ProcessFrame(frame);
            }
        }
    }






    // Process latest frame received from USB webcam.
    public void ProcessFrame( MediaFrameReference frame)
    {
        // Convert frame to a SoftwareBitmap of a valid format to display in an Image control:
            //      SoftwareBitmap Class
            //          https://docs.microsoft.com/en-us/uwp/api/windows.graphics.imaging.softwarebitmap?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DEN-US%26k%3Dk(Windows.Graphics.Imaging.SoftwareBitmap)%3Bk(TargetFrameworkMoniker-.NETCore%2CVersion%3Dv5.0)%3Bk(DevLang-csharp)%26rd%3Dtrue
            var softwareBitmap = FrameRenderer.ConvertToDisplayableImage(frame?.VideoMediaFrame);



        if (softwareBitmap != null)
        {
            // Swap the processed frame to _backBuffer and trigger UI thread to render it
            softwareBitmap = Interlocked.Exchange(ref _backBuffer, softwareBitmap);          // does bytes = h X w ?

            // UI thread always reset _backBuffer before using it.  Unused bitmap should be disposed.
            softwareBitmap?.Dispose();

            ////////////////////////   DISPLAY FRAME LOCALLY   /////////////////////////

            // Changes to xaml ImageElement must happen in UI thread through Dispatcher
            var task = _imageElement.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                async () =>
                {
                    // Keep draining frames from the backbuffer until the backbuffer is empty.
                    SoftwareBitmap latest_frame_SoftwareBitmap;
                    while (( latest_frame_SoftwareBitmap = Interlocked.Exchange(ref _backBuffer, null)) != null)
                    {
                                                         // Diag to save frame to a file:
                                                                 //VideoMediaFrame inputFrame    =     frame.VideoMediaFrame;
                                                                 //SoftwareBitmap  frame_SoftwareBitmap = inputFrame.SoftwareBitmap;

                                                                 Common.SaveSoftwareBitmapToFile( latest_frame_SoftwareBitmap, "diag ProcessFrame .JPG");

                    . . .

        }





    // Saves encoded (ie. compressed) SoftwareBitmap image pixel data to a specific file pathname.
    public static async void SaveSoftwareBitmapToFile(SoftwareBitmap softwareBitmap, String pathname)
    {

        StorageFile outputFile = await StorageFile.GetFileFromPathAsync( pathname );
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  ERROR OCCURS HERE



        using (IRandomAccessStream stream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
        {
            // Create an encoder with the desired format
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);

            // Set the software bitmap
            encoder.SetSoftwareBitmap(softwareBitmap);

            // Set additional encoding parameters, if needed
            //encoder.BitmapTransform.ScaledWidth = 320;
            //encoder.BitmapTransform.ScaledHeight = 240;
            //encoder.BitmapTransform.Rotation = Windows.Graphics.Imaging.BitmapRotation.Clockwise90Degrees;
            //encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant;
            encoder.IsThumbnailGenerated = true;

            try
            {
                await encoder.FlushAsync();
            }
            catch (Exception err)
            {
                const int WINCODEC_ERR_UNSUPPORTEDOPERATION = unchecked((int)0x88982F81);
                switch (err.HResult)
                {
                    case WINCODEC_ERR_UNSUPPORTEDOPERATION: 
                        // If the encoder does not support writing a thumbnail, then try again
                        // but disable thumbnail generation.
                        encoder.IsThumbnailGenerated = false;
                        break;
                    default:
                        throw;
                }
            }

            if (encoder.IsThumbnailGenerated == false)
            {
                await encoder.FlushAsync();
            }


        }
    }

StorageFile的文档中:

ArgumentException

该路径不能是相对路径或Uri。 检查路径值。

您需要指定文件的绝对路径。

不相关的说明:我会为您的文件尝试一些更好的命名约定。 名称和扩展名之间的多余空间是非常规的(并且可能会导致某些文件系统出现问题,例如在基于unix / linux的OS上的svn)。 从上下文来看,像"C:/path/to/diagProcessFrame.jpg"这样的文件名看起来确实不错。

暂无
暂无

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

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