WPF - 簡單的圖檔瀏覽程式

posted @ 2008/6/23 09:38 | 閱讀數 : 1928 | 我要推薦 | 5 Comments | 訂閱

 

這篇當作練習題,把之前的稍為回憶一下,來作個簡單的圖檔瀏覽程式,完成之後是像下面這樣

介面的部分就改用XAML Code來作了,下面我們來看看相關的程式碼吧

首先是介面相關的XAML Code

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Window1"
    Title="Window1" Height="346" Width="469" Name="Window1">
    <DockPanel >
        <DockPanel.Background>
            <SolidColorBrush Color="Black" />
        </DockPanel.Background>
        <WrapPanel DockPanel.Dock="Top">
            <TextBox Name="txtSource" Width="200" />    
            <Button Name="btnSelect" Width="50" Content="..." />
        </WrapPanel>
        <Rectangle DockPanel.Dock="Top" Fill="White" Height="2" />
        <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" Name="spSmallImg"></StackPanel>
        <Rectangle DockPanel.Dock="Bottom" Fill="White" Height="2" />
        <Image  DockPanel.Dock="Bottom" Name="LargerImg"  Margin="2,2,2,2"/>
    </DockPanel>
</Window>

再來我們看看後置的程式碼

Imports System.Windows
Imports System.IO
Imports System.Reflection
 
Class Window1
 
    Dim smallImg() As Image
 
    Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        RefreshImage()
    End Sub
 
    Private Sub wpSmallImg_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles spSmallImg.MouseDown
        If e.OriginalSource.GetType.Name = "Image" Then
            LargerImg.Source = CType(e.OriginalSource, Image).Source
        End If
    End Sub
 
    Private Sub btnSelect_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSelect.Click
        Dim dlgOpen As New Windows.Forms.FolderBrowserDialog
        If dlgOpen.ShowDialog = Forms.DialogResult.OK Then
            txtSource.Text = dlgOpen.SelectedPath
            RefreshImage()
        End If
    End Sub
 
    Private Sub RefreshImage()
        Dim strArr() As String
        If txtSource.Text = "" Then
            strArr = IO.Directory.GetFiles("Images", "*.bmp")
        Else
            strArr = IO.Directory.GetFiles(txtSource.Text, "*.jpg")
        End If
        Dim tmpBmp As BitmapImage
        Dim tmpUri As Uri
        ReDim smallImg(strArr.Length - 1)
 
        spSmallImg.Children.RemoveRange(0, spSmallImg.Children.Count)
 
        For i As Integer = 0 To strArr.Length - 1
            If txtSource.Text = "" Then
                tmpUri = New Uri("pack://application:,,,/" & strArr(i))
            Else
                tmpUri = New Uri(strArr(i))
            End If
            tmpBmp = New BitmapImage
            tmpBmp.BeginInit()
            tmpBmp.UriSource = tmpUri
            tmpBmp.EndInit()
 
            smallImg(i) = New Image
            smallImg(i).Stretch = Stretch.Uniform
            smallImg(i).Width = 48
            smallImg(i).Height = 48
            smallImg(i).Source = tmpBmp
            spSmallImg.Children.Add(smallImg(i))
        Next
    End Sub
End Class

bauann@dotblog

  • 文章分類 : WPF

回覆
lou - 2008/6/28 下午 05:11   回覆
# re: WPF - 簡單的圖檔瀏覽程式
請問!! 未定義Windows.Forms.FolderBrowserDialog 怎麼解決ㄋ!

Dim dlgOpen As New Windows.Forms.FolderBrowserDialog
If dlgOpen.ShowDialog = Forms.DialogResult.OK Then
bauann - 2008/6/28 下午 07:53   回覆
# re: WPF - 簡單的圖檔瀏覽程式

Hi, 抱歉,忘了提,要把 System.Windows.Form 的命名空間加進來才會有 FolderBrowserDialog 可以用

bauann - 2008/6/28 下午 08:11   回覆
# re: WPF - 簡單的圖檔瀏覽程式

另外上面程式碼圖片過多時無法出現捲軸,我們可以加個ScrollViewer來解決這個問題,完整的XAML像下面這樣

<Window    
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
    x:Class="Window1"    Title="Window1" Height="346" Width="469" Name="Window1">
    <DockPanel >
        <DockPanel.Background>
            <SolidColorBrush Color="Black" />
        </DockPanel.Background>
        <WrapPanel DockPanel.Dock="Top">
            <TextBox Name="txtSource" Width="200" />
            <Button Name="btnSelect" Width="50" Content="..." />
        </WrapPanel>
        <Rectangle DockPanel.Dock="Top" Fill="White" Height="2" />
        <ScrollViewer DockPanel.Dock="Bottom" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" >
            <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" Name="spSmallImg"></StackPanel>   
        </ScrollViewer>
        <Rectangle DockPanel.Dock="Bottom" Fill="White" Height="2" />
        <Image  DockPanel.Dock="Bottom" Name="LargerImg"  Margin="2,2,2,2"/>
    </DockPanel>
</Window>
MIss Tong - 2009/11/11 下午 05:16   回覆
# re: WPF - 簡單的圖檔瀏覽程式
不好意思
我想請問一下

Dim dlgOpen As New Windows.Forms.FolderBrowserDialog
If dlgOpen.ShowDialog = Forms.DialogResult.OK Then

的問題..

你可以再清楚一點講解解決的方法嗎?

<要把 System.Windows.Form 的命名空間加進來才會有 FolderBrowserDialog 可以用> 我不太明白意思是什麼..

可以說一下步驟嗎?
謝謝!!
bauann - 2009/11/12 上午 09:21   回覆
# re: WPF - 簡單的圖檔瀏覽程式

to MIss Tong :
Hi, 主要就是在專案名稱點選滑鼠右鍵,之後選擇加入參考,之後找到System.WIndows.Form這個參考;把這個參考加進來之後才有FolderVrowser可以使用

發表回覆






登入後使用進階評論

Please add 6 and 6 and type the answer here: