【问题标题】:Image from isolated storage来自独立存储的图像
【发布时间】:2012-03-28 17:48:30
【问题描述】:

我正在尝试绑定保存在独立存储中的图像并将其显示在与相机相同的屏幕上,但我似乎无法显示图像。我不知道是不是因为我没有将它保存在手机的相机胶卷上,但我没有保存它们,因为我要拍摄多张照片并将它们像胶片一样显示在相机取景器的底部。谁能帮帮我?

我正在使用本教程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;

        }
    }

这是我的 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>

【问题讨论】:

  • 如此接近! :) 你能给出 PhotoItem 的定义吗(因为这可能对你来说很糟糕)
  • 嗨,Shahar,PhotoItem 位于类中 C# 代码的底部
  • 嘘。 PhotoItem 是一个字符串——对不起。现在回答。

标签: c# silverlight windows-phone-7


【解决方案1】:

好的 - 问题是没有隐式转换器知道如何获取 IsoStorage URI 并加载它。

一个简单的解决方案是向您的 PhotoImage 类添加另一个属性并绑定到它 - 这是一个快速而肮脏的方法:

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

请注意,这不是一个很好的解决方案 - 我只是向您展示总体思路。实现自己的时要考虑的事项:

  1. 流未被丢弃。将 Stream 设置为 image 时,将其包装在 using 中。
  2. 根据您要执行的操作,您可能希望使用image.CreateOptions 来提高应用的响应速度(但随后您需要弄清楚如何处理需要保持打开状态的流)
  3. 最后,将要加载的图像将以全分辨率显示。您可能需要查看 PictureDecoder.DecodeJpeg() 以加载图像的缩略图(或查看相机对象提供的缩略图)

【讨论】:

  • 感谢您的帮助,所以您的意思是将我的班级更改为 public class PhotoImage { public ImageSource PhotoItem { get; set; } public PhotoImage(ImageSource pItem) { this.PhotoItem = pItem; } } 我应该将 ImageSource 添加到哪里?
  • 否 - 保留您的 PhotoImage 类 - 只需添加我写的属性(而不是文件名,但放入 PhotoItem - 我会在回复中更改)
  • 我不知道我应该把这个属性放在哪里,我应该添加一个像public string PhotoItem{ get; set; }这样的属性,然后再添加一个public ImageSource SourceItem{ get; set; },我应该把这段代码放在哪里?
  • 作为 ImageSource SourceItem 的正文(我编辑了我的回复以使其更清晰)
  • 非常感谢你真的帮了大忙:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多