0

如何塞弗电网背景选择通过选择图像任务图像后如何在我的应用程序保存网格背景

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 
    } 
} 

帮我请 谢谢:)

注:“保存'意味着当应用程序下次打开时 是'网格后端'选择相同的背景

回答

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> 

该按钮Click事件。

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图像的名称。每次从库中选择新图像时,图像都会被覆盖。希望这可以帮助。

+0

谢谢yooooooooooooooooooooooou very veryyyyyyyyyyyyyyy much^_^ – mohammad 2014-10-02 10:10:04

+0

不客气:D – 2014-10-02 10:36:58

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中,并且无论您是否选择了任何图像,都需要有用于保存图像状态的独立设置变量。

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

+0

谢谢:)^_^ – mohammad 2014-10-02 10:10:31

相关问题