从之前恢复的坐标创建图像

时间:2014-02-16 05:05:23

标签: c# xaml windows-phone-8 nokia-imaging-sdk lumia-imaging-sdk

我尝试通过叠加另外两个来创建一个图像(一个是我放置另一个的背景)。 但是当我得到最终图像时,我意识到我使用之前检索到的坐标放置的那个移动了。

XAML:

<Grid x:Name="LayoutRoot">
  <Grid.RowDefinitions>
        <RowDefinition Height="Auto" MinHeight="83"/>
        <RowDefinition/>
    </Grid.RowDefinitions>


    <StackPanel Grid.Row="0" Margin="175,10,345,0">
        <Button Content="Ajouter cadre" Name="cadre" Background="{x:Null}" Margin="10,0,0,0" Height="73" HorizontalAlignment="Left" Width="188" Click="Button_Click_1"/>
    </StackPanel>


    <Image Name="testPicLabpreview" Grid.Row="1" Canvas.ZIndex="500" Width="128" Height="128" />

    <Image Name="PicLab"  HorizontalAlignment="Left" Width="708" Height="377" VerticalAlignment="Top" Margin="10,10,0,0" Grid.Row="1" />
    <Image Name="PicLabpreview" HorizontalAlignment="Left" Width="708" Height="377" VerticalAlignment="Top" Margin="10,10,0,0" Grid.Row="1" Stretch="None"  />

    <Button Content="Test effects" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Click="Button_Click" Height="72" Width="165"/>
    <Button Content="Finaliser" HorizontalAlignment="Left" Margin="586,10,0,0" VerticalAlignment="Top" Width="142" Click="Button_Finalise" Height="72"/>

</Grid>

C#:

private async Task createPicture(Stream storageFile_top, Stream storageFile_bg, double tx, double ty, double zoom, double rot)
    {
        using (var source_bg = new StreamImageSource(storageFile_bg))
        using (var filters_bg = new FilterEffect(source_bg))
        using (var render = new JpegRenderer(filters_bg))
        using (var source_top = new StreamImageSource(storageFile_top))
        using (var filters_top = new FilterEffect(source_top))
        {
            var topSize = await source_top.GetInfoAsync();
            var bgSize = await source_bg.GetInfoAsync();


            //top left point of the blend layer
            var topLeftLayer = new Windows.Foundation.Point(
                topSize.ImageSize.Width/2 - tx/zoom,
                //center on source_top and position top left point of the blend layer =>  trasnlation
                topSize.ImageSize.Height/2 - ty/zoom
                );

            var layerSize = new Windows.Foundation.Size(
                bgSize.ImageSize.Width/zoom, //appli factor on layer size => zoom
                bgSize.ImageSize.Height/zoom
                );

            var pivot = new Windows.Foundation.Point(
                tx/bgSize.ImageSize.Width,
                //rotation pivot point in relative coordinate => rotation around the center of the layer picture
                ty/bgSize.ImageSize.Height);




            //layer source
            filters_top.Filters = new IFilter[]
            {new ReframingFilter(new Windows.Foundation.Rect(topLeftLayer, layerSize), rot, pivot)};


            filters_bg.Filters = new IFilter[] {new BlendFilter(filters_top, BlendFunction.Normal)};
            var buffer = await render.RenderAsync();



            var bmp = new BitmapImage();
            bmp.SetSource(buffer.AsStream());
            PicLabpreview.Source = bmp;
        }
    }


private async void Button_Click_1(object sender, System.Windows.RoutedEventArgs e)
    {
        tx = Get_Position().X;
        ty = Get_Position().Y;

        var source = PhoneApplicationService.Current.State["source"];


        WriteableBitmap bmp = new WriteableBitmap((BitmapSource)PhoneApplicationService.Current.State["bmp"]);

        WriteableBitmap ImageApply = new WriteableBitmap(bmp);

        BitmapImage frame = (BitmapImage) testPicLabpreview.Source;

        WriteableBitmap ImageApplytest = new WriteableBitmap(frame);

        PicLab.Source = bmp;


        PicLabpreview.Source = ImageApply;


        testPicLabpreview.Source = ImageApplytest;

        var effect = new FilterEffect(source as IImageProvider);
        var renderer = new WriteableBitmapRenderer(effect, ImageApplytest);

        var fileStreamSource = new MemoryStream();
        bmp.SaveJpeg(fileStreamSource, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
        fileStreamSource.Seek(0, SeekOrigin.Begin);
        IBuffer bufferSrc = fileStreamSource.GetWindowsRuntimeBuffer();

        var fileStreamMask = new MemoryStream();
        ImageApplytest.SaveJpeg(fileStreamMask, 128, 128, 0, 100);
        fileStreamMask.Seek(0, SeekOrigin.Begin);
        IBuffer bufferMask = fileStreamMask.GetWindowsRuntimeBuffer();

        await createPicture(
                        fileStreamMask,
                        fileStreamSource,
                        tx,
                        ty,
                        1,
                        0);

        PicLab.Opacity = 0;
        PicLabpreview.Opacity = 100;
        testPicLabpreview.Opacity = 0;



    }


 private Point Get_Position()
    {
        GeneralTransform myTransform = testPicLabpreview.TransformToVisual(LayoutRoot);
        Point relativePosition = myTransform.Transform(new Point(0, 0));
        return relativePosition;
    }

这是我在“创建图片”之前的结果:

enter image description here

之后: enter image description here

是否有人知道它可能来自哪里?

0 个答案:

没有答案