Click here to Skip to main content
15,884,078 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
assume there is a folder "G:\Chargesheets\TestImage\thumbs\" with a bunch of thumbnails

I can display thos thumbnails just fine using the ReturnI subroutine. only I need to be able to move or delete the files from that folder, which I can not do while the program is running because of a file lock.

If I use the ReturnIMs subroutine I can delete and or move the files in that folder, but the listbox just shows a blank box for each image in the folder.

i think the problem is in this part of the xml Source="{Binding UriSource}"
i have tried changing UriSource to MemoryStream but that did not work. any ideas would be appreciated.


XML source
XML
<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="794.223" Width="1076.351">
    <Window.Resources>

        <Style  TargetType="{x:Type ListView}">
            <!-- Set the ItemTemplate of the ListBox to a DataTemplate 
           which explains how to display an object of type BitmapImage. -->
            <Setter  Property="ItemTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Border 
              BorderBrush="Black" 
              BorderThickness="4" 
              CornerRadius="5"
              Margin="6"
              >
                            <Image 
                Source="{Binding UriSource}" 
                Stretch="Fill"
                Width="144" Height="200" 
               />

                        </Border>

                    </DataTemplate>
                </Setter.Value>
            </Setter>


            <!-- Swap out the default items panel with a WrapPanel so that
           the images will be arranged with a different layout. -->
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <WrapPanel />
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>

            <!-- Set this attached property to 'Disabled' so that the 
           ScrollViewer in the ListBox will never show a horizontal 
           scrollbar, and the WrapPanel it contains will be constrained 
           to the width of the ScrollViewer's viewable surface. -->
            <Setter 
        Property="ScrollViewer.HorizontalScrollBarVisibility" 
        Value="Disabled" 
        />
            <Setter Property="SelectionMode" Value="Extended"/>
        </Style>
    </Window.Resources>
    <Grid Margin="0,92,0,22">
        <ListView x:Name="LBox" Margin="10,10,122,10" VerticalContentAlignment="Stretch" SelectionMode="Multiple"/>
        <Label x:Name="Working" Content="Working" HorizontalAlignment="Left" Margin="10,60,0,0" VerticalAlignment="Top" Visibility="Hidden" Background="#FFEE1616"/>

    </Grid>
</Window>


VB source

VB
Imports System.IO

Class MainWindow

    Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        Dim IPath As String = "G:\Chargesheets\TestImage\thumbs\"
        LBox.ItemsSource = LoadImages(IPath, 0)
    End Sub

    Public Function LoadImages(IPath As String, AddPercent As Decimal) As List(Of BitmapImage)
        Dim rImages As New List(Of BitmapImage)()
        Dim rImageDir As New DirectoryInfo(IPath)
        For Each rImageFile As FileInfo In rImageDir.GetFiles("*.png")
            rImages.Add(ReturnI(rImageFile.FullName))
        Next
        Return rImages
    End Function

    Private Function ReturnI(Fpath As String) As BitmapImage
        Dim BmpImage As BitmapImage
        ' Create a BitmapImage and sets its DecodePixelWidth and DecodePixelHeight
        'Using fs As New FileStream(FPath, FileMode.Open)
        BmpImage = New BitmapImage()
        BmpImage.BeginInit()
        BmpImage.CacheOption = BitmapCacheOption.OnLoad
        BmpImage.UriSource = New Uri(Fpath, UriKind.RelativeOrAbsolute)
        'BmpImage.StreamSource = fs
        'fs.Dispose()
        BmpImage.EndInit()
        Return BmpImage
    End Function

    Private Function ReturnIMs(Fpath As String) As BitmapImage
        Dim Buffer() As Byte
        Buffer = File.ReadAllBytes(Fpath)
        Dim bitmapImage = New BitmapImage()
        Using stream As Stream = New MemoryStream(DirectCast(Buffer, Byte()))
            bitmapImage.BeginInit()
            bitmapImage.CacheOption = BitmapCacheOption.OnLoad
            bitmapImage.StreamSource = stream
            bitmapImage.EndInit()
            bitmapImage.Freeze()
            Return bitmapImage
        End Using
    End Function
End Class
Posted
Comments
Sergey Alexandrovich Kryukov 9-Sep-14 12:51pm    
Why doing so? Why not having several different streams, one per image? I think you could do it by just reading all imaging, but wouldn't it defeat the purpose?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900