如何在我的应用程序中保存网格背景

时间:2014-10-02 07:14:14

标签: windows-phone-7 windows-phone-8 windows-phone windows-phone-8.1

通过选择图像任务选择图像后如何切割网格背景

EX:

 private void photoChooserTask_Completed(object sender, PhotoResult e)
 {
     if (e.TaskResult == TaskResult.OK)
     {
         bmp = new System.Windows.Media.Imaging.BitmapImage();
         bmp.SetSource(e.ChosenPhoto);
         ImageBrush img = new ImageBrush();
         img.ImageSource = bmp;
         LayoutRoot.Background = img;

         //Save grid Background 
    }
}
请帮助我 谢谢你:))

注意:“保存”表示下次打开应用程序时  是'网格背景'选择相同的背景

2 个答案:

答案 0 :(得分:1)

试试这个:

XAML

<Grid x:Name="LayoutRoot">
    <Grid.Background>
        <ImageBrush x:Name="imgsrc"></ImageBrush>
    </Grid.Background>
</Grid>

CS:

    if (e.TaskResult == TaskResult.OK)
    {
        BitmapImage Bitmap = new BitmapImage();
        Bitmap.SetSource(e.ChosenPhoto);
        imgsrc.ImageSource = Bitmap;
    }

对于保存图像,您需要使用isolatedstorage。您需要将图像保存在Isolatedstorage中,并且无论您是否选择了任何图像,都需要使用Isolatedsettings变量来保存图像的状态。

如果是,那么从那里获取图像,否则不需要采取任何行动,这里有一个很好的例子,你可以参考保存和从Isolatedstorage Isolated Storage - Read and Save Images

中重建图像

答案 1 :(得分:1)

试试这个..在示例中,我使用了一个按钮来激活PhotoChooserTask。 XAML是这样的。

<强> XAML

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Button Name="btnSet" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Content="Set Image" Click="btnSet_Click" />
    </Grid>
</Grid>

单击按钮的事件。

<强> C#

private void btnSet_Click(object sender, RoutedEventArgs e)
{
    PhotoChooserTask photoTask = new PhotoChooserTask();
    photoTask.Completed += photoTask_Completed;
    photoTask.PixelHeight = 1280;
    photoTask.PixelWidth = 768;
    photoTask.Show();
}

photoTask_Completed事件处理程序中,您可以将图像保存到IsolatedStorage

void photoTask_Completed(object sender, PhotoResult e)
{
    if (e.TaskResult == TaskResult.OK)
    {
        using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (isoStore.FileExists(fileName))
            {
                isoStore.DeleteFile(fileName);
            }
            using (IsolatedStorageFileStream targetStream = isoStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
            {
                byte[] readBuffer = new byte[4096];
                int bytesRead = -1;

                while ((bytesRead = e.ChosenPhoto.Read(readBuffer, 0, readBuffer.Length)) > 0)
                {
                    targetStream.Write(readBuffer, 0, bytesRead);
                }
            }
        }
    }
}

然后在OnNavigatedTo事件中,您可以从IsolatedStorage加载图片并将其设置为背景。

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    byte[] data;

    using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (isoStore.FileExists(fileName))
        {
            using(IsolatedStorageFileStream stream = isoStore.OpenFile(fileName, FileMode.Open, FileAccess.Read))
            {
                data = new byte[stream.Length];
                stream.Read(data, 0, data.Length);
            }

            MemoryStream ms = new MemoryStream(data);
            bmp = new BitmapImage();
            bmp.SetSource(ms);
            ImageBrush img = new ImageBrush();
            img.ImageSource = bmp;
            LayoutRoot.Background = img;
        }
    }
}

变量fileName包含保存到IsolatedStorage的图像的名称。每次从库中选择新图像时,图像都会被覆盖。希望这会有所帮助。