从ImageBrush创建writeableBitmap

时间:2012-04-27 03:50:14

标签: c# windows-phone-7 xaml

我正试图拍摄快照'在点击事件上显示视频错误,但由于某种原因我无法正确显示此快照。

double height = Application.Current.Host.Content.ActualHeight;

//Capture the screen           
private void freeze_Click(object sender, EventArgs e)
{
        // Takes the preview buffer pixel array from camera and returns
        // an array of pixels representing the camera's preview buffer
        int[] previewBuffer = GetPreviewBuffer();

        //Process the preview image
        int[] imageData = PhotofunDataContext.SelectedEffect.Effect.ProcessImage(previewBuffer);

        //Copy to WriteableBitmap
        //previewWriteableBitmap = new WriteableBitmap((int)camera.PreviewResolution.Width, (int)camera.PreviewResolution.Height);
        WriteableBitmap freezeWriteableBitmap = new WriteableBitmap((int)camera.PreviewResolution.Width, (int)camera.PreviewResolution.Height);

        imageData.CopyTo(freezeWriteableBitmap.Pixels, 0);

        freezeWriteableBitmap.Invalidate();  

        //Display that on the screen
        FreezeImage.Source = freezeWriteableBitmap;

        //height of device
        FreezeImage.Width = height;
        //width to maintain 4:3 aspect ratio
        FreezeImage.Height = height * (.75);

        FreezeImage.Opacity = 1;

        }

}

//Get the preview buffer
private int[] GetPreviewBuffer()
{
        int[] pixelData = new int[(int)(camera.PreviewResolution.Width * camera.PreviewResolution.Height)];
        //int[] pixelData = new int[(int)width * (int)height];
        camera.GetPreviewBufferArgb32(pixelData);
        return pixelData;
}

以上代码用于拍摄视频投影的快照,并将其作为冻结帧显示在当前活动的视频投影上。

<Rectangle x:Name="videoRectangle" Width="1" Height="1" HorizontalAlignment="Center">
            <Rectangle.Fill>
                <VideoBrush x:Name="viewfinderBrush" >
                    <VideoBrush.RelativeTransform>
                        <CompositeTransform x:Name="viewfinderTransform" Rotation="90"
                                            CenterX="0.5" CenterY="0.5"/>
                    </VideoBrush.RelativeTransform>
                </VideoBrush>                    
            </Rectangle.Fill>
        </Rectangle>

        <Image x:Name="FreezeImage" Opacity="0" HorizontalAlignment="Center" Stretch="Fill" Canvas.ZIndex="900">
            <Image.Projection>
                <PlaneProjection RotationZ="-90"/>
            </Image.Projection>
        </Image>        

FreezeImage现在覆盖了活动的视频图像,但图像尺寸与手机尺寸不匹配。我想在纵向模式下将FreezeImage(无需拉伸)放到整个手机屏幕上。

我尝试设置FreezeImage.Width = height;FreezeImage.Height = height * (.75);,以便在将图像调整到手机屏幕时保持相机的自然4:3宽高比,但这不会覆盖预览缓冲区和FreezeWriteableBitmap尺寸(640x480)。

我还尝试对预览缓冲区和FreezeWriteableBitmap的宽度(= 1064)和高度(= 800)维度进行硬编码,但是根本不显示图像(就像writeableBitmap不会接受除相机之外的任何其他值) #39;尺寸)。

在使用相机初始化之后,我发现无论我将分辨率设置为什么,writeableBitmaps总是默认为640x480,这是我认为问题必须在哪里因为看起来这个宽高比是实际显示的在电话屏幕上。如何修复此实现,以便生成的freezeImage高度与手机屏幕高度匹配,并调整宽度,以便不执行拉伸?

注意:设置FreezeImage.Width = height;FreezeImage.Height = height * (.75);时的高度值是通过将double height = Application.Current.Host.Content.ActualHeight;设置为除了当前标准480x800之外的其他尺寸的未来WP7设备而找到的。

0 个答案:

没有答案