【问题标题】:Take a snapshot of using the rectangle in WP7拍摄在 WP7 中使用矩形的快照
【发布时间】:2012-04-17 07:25:30
【问题描述】:

我正在开发一个 WP7 应用程序,我想在其中录制视频,并在保存视频之前拍摄视频快照,以便将其用作缩略图。缩略图在使用前临时保存在隔离存储中。对于相机,我使用矩形来录制视频,然后我想在手机上显示图片。 问题是图片只显示黑屏。即使我尝试将图片保存在媒体库中,图片也显示为黑色。这个问题可能是什么原因造成的,我该如何解决?

我在下面插入了代码:

这是您用来捕捉视频的矩形。

 <Rectangle 
            x:Name="viewfinderRectangle"
            Width="640" 
            Height="480" 
            HorizontalAlignment="Left" 
            Canvas.Left="80"/>

这是拍照的代码:

try
            {
                String tempJPEG = FOSConstants.TEMP_VIDEO_THUMBNAIL_NAME;
                var myStore = IsolatedStorageFile.GetUserStoreForApplication();
                if (myStore.FileExists(tempJPEG))
                {
                    myStore.DeleteFile(tempJPEG);
                }
                IsolatedStorageFileStream myFileStream = myStore.CreateFile(tempJPEG);

                WriteableBitmap wb = new WriteableBitmap(viewfinderRectangle, null);
                wb.SaveJpeg(myFileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
                myFileStream.Close();

                myFileStream.Close();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error saving snapshot", MessageBoxButton.OK);
            }

下面是从隔离存储中读取缩略图的代码:

private BitmapImage GetIsolatedStorageFile(string isolatedStorageFileName)
{
        var bimg = new BitmapImage();
        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        {
           using (var stream = store.OpenFile(isolatedStorageFileName, FileMode.Open,                 FileAccess.Read))
            {  
                bimg.SetSource(stream);
            }
        }


return bimg;    
}

这是我想在 GUI 中显示缩略图的图像。

<Image Width="180" 
                       Height="180" 
                       Stretch="Fill"
                       Margin="24,0,0,0"
                       Source="{Binding Path=ImageSoruce, Mode=TwoWay}"
                       HorizontalAlignment="Left"/>

【问题讨论】:

    标签: c# .net windows-phone-7 bitmap writeablebitmap


    【解决方案1】:

    编辑:删除了关于 Invalidate() 的误导性内容,您不需要这样做。

    因此,您可以使用 GetPreviewBufferArgb32() 方法来获取相机当前提供的内容。这可以复制到您的可写位图中,如下所示。

    using (var myStore = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (myStore.FileExists(tempJPEG))
        {
            myStore.DeleteFile(tempJPEG);
        }
    
        IsolatedStorageFileStream file = myStore.CreateFile(tempJPEG);
        int[] buf = new int[(int)c.PreviewResolution.Width * (int)c.PreviewResolution.Height];
        c.GetPreviewBufferArgb32(buf);
    
        WriteableBitmap wb = new WriteableBitmap((int)c.PreviewResolution.Width, (int)c.PreviewResolution.Height);
        Array.Copy(buf, wb.Pixels, buf.Length);
        wb.SaveJpeg(file, (int)c.PreviewResolution.Width, (int)c.PreviewResolution.Height, 0, 100);
    }
    

    您的示例代码不起作用的原因是取景器画笔设置在 GPU 上(我想我记得为什么我认为它在那里完成,但我认为它是)。这意味着 silverlight 也无法访问原始视频,并且当您渲染 silverlight 元素时,它是空白的(就好像它没有背景一样)。

    【讨论】:

    • 抱歉 - 与 Invalidate() 相关的误导性内容。已更新答案以改用 GetPreviewBufferArgb32()。
    • 您的示例中的“c”是什么?如“c.GetPreviewBufferArgb32(buf);”它没有被声明或实例化
    • 我发现 c 是 PhotoCamera 的一种,但就我而言,我使用的是“CaptureSource _captureSource”。
    • MSDN 说您可以使用 CaptureImageAsync (msdn.microsoft.com/en-us/library/…) 捕获帧。抱歉没用过这个,我只用过PhotoCamera
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 2018-12-03
    相关资源
    最近更新 更多