简体   繁体   English

图像来自孤立的存储

[英]Image from isolated storage

I am trying to bind an image that is saved in isolated storage and display it on the same screen as the camera, but I can't seem to get the image to display. 我试图绑定保存在独立存储中的图像并将其显示在与相机相同的屏幕上,但我似乎无法显示图像。 I don't know if it because I am not saving it on the phones camera roll, but I am not saving them because I am gonna take multiple pictures and display them like a film strip across the bottom of the view finder for the camera. 我不知道是不是因为我没有将它保存在手机相机胶卷上,但是我没有保存它们,因为我要拍多张照片,并将它们像电影胶片一样显示在相机的取景器底部。 Can anyone help me please? 有人可以帮我吗?

I am using this tutorial Here 我在这里使用这个教程

public partial class Page1 : PhoneApplicationPage
{
private static ObservableCollection<PhotoImage> photoList = new ObservableCollection<PhotoImage>();//For the class and list
private int savedCounter = 0;



public Page1()
{
        InitializeComponent();
}


private void ShutterButton_Click(object sender, RoutedEventArgs e)
    {
        if (cam != null)
        {
            try
            {
                // Start image capture.
                cam.CaptureImage();

            }
            catch (Exception ex)
            {
                this.Dispatcher.BeginInvoke(delegate()
                {
                    txtDebug.Text = ex.Message;
                });
            }
        }
    }

    void cam_CaptureCompleted(object sender, CameraOperationCompletedEventArgs e)
    {
        // Increments the savedCounter variable used for generating JPEG file names.
        savedCounter++;
    }

    void cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e)
    {
        string fileName = "MyImage" + savedCounter + ".jpg";

        try
        {   

            // Save picture to the library camera roll.
            //library.SavePictureToCameraRoll(fileName, e.ImageStream);//dont want to save it to the camera roll


            // Set the position of the stream back to start
            e.ImageStream.Seek(0, SeekOrigin.Begin);

            // Save picture as JPEG to isolated storage.
            using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
                {
                    // Initialize the buffer for 4KB disk pages.
                    byte[] readBuffer = new byte[4096];
                    int bytesRead = -1;

                    // Copy the image to isolated storage. 
                    while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
                    {
                        targetStream.Write(readBuffer, 0, bytesRead);
                    }

                }

            }

            Deployment.Current.Dispatcher.BeginInvoke(delegate()
            {

                photoList.Add(new PhotoImage(fileName));//here is where I set with the file name
                listBoxSearch.ItemsSource = photoList; //here is the binding 
            });


        }
        finally
        {
            // Close image stream
            e.ImageStream.Close();
        }

    }
    public class PhotoImage
    {

        public string PhotoItem { get; set; }

        public PhotoImage(string pItem)
        {
            this.PhotoItem = pItem;

        }
    }

here is my XAML code 这是我的XAML代码

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="640" />
        <ColumnDefinition Width="160" />
    </Grid.ColumnDefinitions>

    <Canvas x:Name="viewfinderCanvas" Width="640" HorizontalAlignment="Left" Margin="0,0,0,143">

        <!--Camera viewfinder -->
        <Canvas.Background>
            <VideoBrush x:Name="viewfinderBrush" />
        </Canvas.Background>
        <TextBlock Height="40" Name="txtDebug" Width="626" FontSize="24" FontWeight="ExtraBold" Canvas.Left="14" Canvas.Top="297" />
    </Canvas>

    <!--Button StackPanel to the right of viewfinder>-->
    <StackPanel Grid.Column="1" >
        <Button x:Name="ShutterButton" Content="SH" Click="ShutterButton_Click" FontSize="26" FontWeight="ExtraBold" Height="75" />
    </StackPanel>
    <Grid>
        <ListBox Foreground="RoyalBlue" Height="131"  Name="listBoxSearch"  Width="438"  TabIndex="10" Margin="96,343,106,6">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Height="Auto" >
                        <Image Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0" Source="{Binding PhotoItem }" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </Grid>

</Grid>

Okay - the issue is that does not have an implicit converter that knows how to take an IsoStorage URI and load it. 好的 - 问题是没有隐式转换器知道如何获取IsoStorage URI并加载它。

One easy solution is to add another property to your PhotoImage class and bind to it instead - here's a quick and dirty: 一个简单的解决方案是将另一个属性添加到您的PhotoImage类并绑定到它 - 这是一个快速和肮脏的:

public ImageSource SourceItem
{
  get
  {
    BitmapImage image = new BitmapImage();
    image.SetSource(isStore.OpenFile(PhotoItem, FileMode.Open));  
    return image;
  }
}

Note that this is not a great solution - I am just showing you the general idea. 请注意,这不是一个很好的解决方案 - 我只是向您展示一般的想法。 Things to think about when implementing your own: 实施自己的时候要考虑的事情:

  1. The stream is not being discarded. 流不会被丢弃。 Wrap the Stream in a using when setting it into image . Stream设置为image时将Stream包裹在使用中。
  2. Depending on what you are trying to do, you may want to use image.CreateOptions to make the app more responsive (but then you need to figure out how to handle the stream needing to be kept opened) 根据您要执行的操作,您可能希望使用image.CreateOptions来使应用程序更具响应性(但是您需要弄清楚如何处理需要保持打开的流)
  3. Finally, the image that will be loaded will be at full resolution. 最后,将加载的图像将处于全分辨率。 You may want to look into PictureDecoder.DecodeJpeg() to load a thumbnail of the image instead (or look at the thumbnail provided by the camera object) 您可能需要查看PictureDecoder.DecodeJpeg()来加载图像的缩略图(或查看相机对象提供的缩略图)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM