使用bitmapsource的Tiff图像,比JPEG加载慢

时间:2015-02-01 21:06:23

标签: wpf windows-8.1 jpeg .net-4.5 tiff

我在UI中有以下代码

 <ListBox x:Name="scannedImageslist" ItemContainerStyle="{StaticResource ListBoxCustom}" Background="Gray"  SelectionMode="Multiple" Grid.Column="1" Grid.Row="1" ItemsSource="{Binding }"  ScrollViewer.PanningMode="Both" ScrollViewer.HorizontalScrollBarVisibility="Disabled" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">

                                    <!--<ListBox.ItemContainerStyle>
                                        <Style TargetType="ListBoxItem">
                                            <Style.Triggers>
                                                <Trigger Property="IsSelected" Value="true" >
                                                    <Setter Property="FontWeight" Value="Bold" />
                                                    <Setter Property="Background" Value="Orange" />
                                                    <Setter Property="Foreground" Value="Black" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </ListBox.ItemContainerStyle>-->
                                    <!--<ListBox.Resources>
                                        <gif:ImageConverter x:Key="UriToBitmapConverter" />
                                    </ListBox.Resources>-->
                                    <!--<ListBox.Background>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="Black"/>
                                        <GradientStop Color="#FF1E2A2F" Offset="1"/>
                                    </LinearGradientBrush>
                                </ListBox.Background>-->
                                    <ListBox.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <WrapPanel Orientation="Horizontal" IsItemsHost="True" >
                                            </WrapPanel>
                                        </ItemsPanelTemplate>
                                    </ListBox.ItemsPanel>
                                    <ListBox.ItemTemplate>
                                        <DataTemplate>
                                            <Grid>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition/>
                                                    <ColumnDefinition/>
                                                </Grid.ColumnDefinitions>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition/>
                                                    <RowDefinition/>
                                                </Grid.RowDefinitions>
                                                <!--<Border BorderBrush="#FFFF9800" BorderThickness="3"  Width="120" Height="120" Padding="10" Margin="15" CornerRadius="10">-->
                                                <!--Bind Image Path in Image Control-->
                                                <!--<CheckBox Grid.Column="0" Grid.Row="0" VerticalAlignment="Top"  HorizontalAlignment="Left" ToolTip="{Binding FullPath}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"></CheckBox>-->
                                                <Image Grid.Column="1" Grid.Row="0" Stylus.IsPressAndHoldEnabled="True"  Stretch="None" HorizontalAlignment="Center" ToolTip="{Binding FullPath}"  MouseRightButtonDown="Image_MouseRightButtonDown" >
                                                    <Image.Source>
                                                    <BitmapImage UriSource="{Binding FullPath}" DecodePixelWidth="200" CreateOptions="IgnoreImageCache"  RenderOptions.BitmapScalingMode="HighQuality" VirtualizingPanel.VirtualizationMode="Recycling" CacheOption="OnLoad"> </BitmapImage>
                                                   </Image.Source>
                                                    <Image.LayoutTransform>
                                                        <TransformGroup>
                                                            <ScaleTransform CenterX="0" CenterY="0" ScaleX="{Binding ElementName=sliderzoom,Path=Value}" ScaleY="{Binding ElementName=sliderzoom,Path=Value}" />
                                                        </TransformGroup>
                                                    </Image.LayoutTransform>
                                                </Image>
                                                <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding ImageName}" TextAlignment="Center" VerticalAlignment="Bottom"  Typography.SlashedZero="True"  Typography.NumeralStyle="OldStyle" TextWrapping="Wrap"  HorizontalAlignment="Center"></TextBlock>
                                            </Grid>
                                        </DataTemplate>
                                    </ListBox.ItemTemplate>
                                </ListBox>

绑定类和属性是

 public class ImageItem : INotifyPropertyChanged
    {
        #region Properties
        public string FullPath { get; set; }
        public string ImageName { get; set; }
        public ImageItem(string fullPath)
        {
            FullPath = fullPath;
            //SetImage(fullPath);
            //Image = new BitmapImage(new Uri(@fullPath));
            ImageName = System.IO.Path.GetFileNameWithoutExtension(fullPath);
        }
  #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Private Helpers
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }

使用上面的代码,Jpegs的加载速度比Tiff图像快10倍。这里的Tiff处理有什么问题,在性能方面有什么正确的方法? 更新: 我使用了一个转换器,它就像Jpegs加载Tiff一样快,但问题不是解码,内存被吃掉了。

public class UriConverter : IValueConverter
{
   public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
   {

       if (Path.GetExtension(value.ToString()).ToUpper() == ".TIF")
       {

           Stream tiff = new System.IO.FileStream(value.ToString(), FileMode.Open, FileAccess.Read, FileShare.Read);
           TiffBitmapDecoder tiffdec = new TiffBitmapDecoder(tiff, BitmapCreateOptions.DelayCreation, BitmapCacheOption.Default);

返回tiffdec.Frames [0];                //返回tiffdec.Frames [0] .Thumbnail;

           //GdPictureImaging obj = new GdPictureImaging();
           //int m_CurrentImage = obj.CreateGdPictureImageFromFile(value.ToString());
           //int thumnail= obj.CreateThumbnail(m_CurrentImage, 96, 96);

           //TiffBitmapEncoder encoder = new TiffBitmapEncoder();
           //MemoryStream memoryStream = new MemoryStream();
           //BitmapImage bImg = new BitmapImage();
           //encoder.Frames.Add(BitmapFrame.Create(tiffibitmapsource));
           //encoder.Save(memoryStream);
           //bImg.BeginInit();
           //bImg.DecodePixelWidth = 100;
           //bImg.CacheOption = BitmapCacheOption.OnLoad;
           //bImg.StreamSource = new MemoryStream(memoryStream.ToArray());
           //bImg.EndInit();
           //memoryStream.Close();

           //return bImg;

       }
       else
       {
           BitmapImage bi = new BitmapImage();
           bi.BeginInit();
           bi.DecodePixelWidth = 100;
           bi.CacheOption = BitmapCacheOption.OnLoad;
           bi.UriSource = new Uri(value.ToString());
           bi.EndInit();
           return bi;
       }

   }
   public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
   {
       throw new NotImplementedException();
   }
}

}

如何将Bitmapsource转换为BitmapImage,然后解码像素! TIFF没有缩略图,有没有办法创建和存储,然后只显示缩略图

更新2

根据MSDN

TIF是否可以像JPEG一样工作,而无需转换只是为了加载......

  

JPEG和便携式网络图形(PNG)编解码器本机解码   图像到指定的大小;其他编解码器解码图像   原始尺寸并将图像缩放到所需的大小。

0 个答案:

没有答案