WPF 图片浏览器
使用WPF可以写出很不错的图片浏览器
window1.xmal
<Window x:Class="MediaShow.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="682" Width="806"> <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Dictionary1.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> <Grid > <Grid.RowDefinitions> <RowDefinition Height="37"></RowDefinition> <RowDefinition Height="577*"></RowDefinition> <RowDefinition Height="43"></RowDefinition> </Grid.RowDefinitions> <DockPanel Height="37" Grid.RowSpan="1" VerticalAlignment="Top"> <Menu Grid.Row="0" Template="{StaticResource mycontroltemplate}" DockPanel.Dock="Top" FontSize="12" FontWeight="bold"> <MenuItem Header="文件" > <MenuItem Header="打开" Click="OpenFileClick" /> <MenuItem Header="关闭" Click="CloseFileClick" /> <MenuItem Header="退出" Click="OnQuitClick"/> </MenuItem> <MenuItem Header="操作"> <MenuItem Header="图片旋转90" Click="OnRotate90Click" /> <MenuItem Header="图片旋转-90" Click="btnRotatecounterclockwise90_Click" /> </MenuItem> <MenuItem Header="帮助"> <MenuItem Header="关于" Click="OnAboutMe" /> </MenuItem> <Menu.ItemContainerStyle> <Style TargetType="{x:Type MenuItem}"> <Setter Property="Template" Value="{StaticResource mymenuitemtemplate}"/> </Style> </Menu.ItemContainerStyle> </Menu> <Popup></Popup> </DockPanel> <ScrollViewer Margin="0,0,0,0" Name="scrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Grid.Row="1" Padding="4,4,4,4"> <ScrollViewer.Background> <ImageBrush ImageSource="/MediaShow;component/blue.jpg"/> </ScrollViewer.Background> <Grid ShowGridLines="False" ClipToBounds="False"> <Image Name="img" Stretch="Fill" Visibility="Visible"> </Image> <Border HorizontalAlignment="Stretch" Name="border1" VerticalAlignment="Stretch" BorderThickness="1" CornerRadius="1,1,1,1" BorderBrush="LightBlue"> <Border.BitmapEffect> <OuterGlowBitmapEffect GlowSize="1" GlowColor="LightBlue" Noise="0" /> </Border.BitmapEffect> </Border> </Grid> </ScrollViewer> <Grid Grid.Row="2" Margin="0.5,0,0.5,1"> <Grid.BitmapEffect> <OuterGlowBitmapEffect GlowColor="DarkViolet" /> </Grid.BitmapEffect> <Grid.BitmapEffectInput> <BitmapEffectInput /> </Grid.BitmapEffectInput> <Grid.Background> <ImageBrush ImageSource="/MediaShow;component/blue.png" ></ImageBrush> </Grid.Background> <StackPanel Height="43" Name="stackPanel1" VerticalAlignment="Center" Orientation="Horizontal" ClipToBounds="True" HorizontalAlignment="Center" > <Button Height="23" Name="btnFitSwitch" Width="75" Margin="10" Click="btnFitSwitch_Click" Background="Green" Foreground="White" FontSize="14" FontWeight="bold">FitSwitch</Button> <Button Height="23" Name="btnPrevious" Width="75" HorizontalContentAlignment="Center" Margin="10" Click="btnPrevious_Click" Background="Green" Foreground="White" FontSize="14" FontWeight="bold">上一个</Button> <Button Height="23" Name="btnPlaySwitch" Width="75" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10" Background="Green" Foreground="White" FontSize="14" FontWeight="bold">停止</Button> <Button Height="23" Name="btnNext" Width="75" HorizontalAlignment="Right" Margin="10" Click="btnNext_Click" Background="Green" Foreground="White" FontSize="14" FontWeight="bold">下一个</Button> <Button Height="23" Name="btnRotatecounterclockwise90" Width="75" Margin="10" Click="btnRotatecounterclockwise90_Click" Background="Green" Foreground="White" FontSize="14" FontWeight="bold">左旋90</Button> <Button Height="23" Name="btnRotateclockwise90" Width="75" Margin="10" Click="btnRotateclockwise90_Click" Background="Green" Foreground="White" FontSize="14" FontWeight="bold">右旋90</Button> </StackPanel> </Grid> </Grid> </Window>
window1.xmal.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Microsoft.Win32; using System.IO; using System.Collections; namespace MediaShow { /// <summary> /// Window1.xaml 的交互逻辑 /// </summary> public partial class Window1 : Window { string fileName; string filePath; BitmapImage bitmapImage; DirectoryInfo directory; ArrayList filesArray ; int fileIndex; bool fitToScreen = true; double rotateAngle = 0; public Window1() { InitializeComponent(); this.border1.Visibility = Visibility.Hidden; this.SizeChanged += new SizeChangedEventHandler(Window1_SizeChanged); } void Window1_SizeChanged(object sender, SizeChangedEventArgs e) { if (fileName != null) { showPicture(fileName); } } private void OpenFileClick(object sender, RoutedEventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "图像文件(*.jpg;*.jpeg;*.png;*.gif)|*.jpg;*.jpeg;*.png;*.gif|视频文件(*.wmv;*.mp4)|*.wmv;*.mp4|All Files(*.*)|*.*"; if ((bool)dlg.ShowDialog(this)) { fileName = dlg.FileName; showPicture(fileName); filePath = fileName.Substring(0, fileName.LastIndexOf('//')); directory = new DirectoryInfo(filePath); filesArray = null; filesArray = new ArrayList(); if (directory.GetFiles("*.jpg") != null) { foreach (FileInfo fileinfo in directory.GetFiles("*.jpg")) { filesArray.Add(fileinfo); } } if (directory.GetFiles("*.jpeg") != null) { foreach (FileInfo fileinfo in directory.GetFiles("*.jpeg")) { filesArray.Add(fileinfo); } } if (directory.GetFiles("*.png") != null) { foreach (FileInfo fileinfo in directory.GetFiles("*.png")) { filesArray.Add(fileinfo); } } if (directory.GetFiles("*.gif") != null) { foreach (FileInfo fileinfo in directory.GetFiles("*.gif")) { filesArray.Add(fileinfo); } } int i = 0; foreach (FileInfo file in filesArray) { if (file.Name == fileName) { fileIndex=i; break; } i++; } } this.border1.Visibility = Visibility.Visible; } private void showPicture(string fileName) { bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.UriSource = new Uri(fileName); if (rotateAngle==0) { bitmapImage.Rotation = Rotation.Rotate0; } else if (rotateAngle == 90) { bitmapImage.Rotation = Rotation.Rotate90; } else if (rotateAngle == 180) { bitmapImage.Rotation = Rotation.Rotate180; } else if (rotateAngle == 270) { bitmapImage.Rotation = Rotation.Rotate270; } bitmapImage.EndInit(); if (!fitToScreen) { this.img.Height = bitmapImage.Height; this.img.Width = bitmapImage.Width; this.border1.Height = this.img.Height; this.border1.Width = this.img.Width; this.img.Source = bitmapImage; return; } else { double scale = bitmapImage.Height / bitmapImage.Width; if (scale >= 1) //以竖直方向为标准 { this.img.Height = scrollViewer.ViewportHeight - 5; this.img.Width = this.img.Height / scale; } else { this.img.Width = scrollViewer.ViewportWidth - 5; this.img.Height = this.img.Width * scale; } this.border1.Height = this.img.Height; this.border1.Width = this.img.Width; this.img.Source = bitmapImage; return; } } private void CloseFileClick(object sender, RoutedEventArgs e) { //imgsource = null; bitmapImage = null; this.img.Width =this.scrollViewer.Width; this.img.Height = this.scrollViewer.Height; this.img.Source = null; this.border1.Visibility = Visibility.Hidden; } private void btnNext_Click(object sender, RoutedEventArgs e) { this.fileName = filePath + "//" + ((FileInfo)(filesArray[(fileIndex++) % (filesArray.Count)])).Name; showPicture(this.fileName); } private void btnPrevious_Click(object sender, RoutedEventArgs e) { if (fileIndex==0) { fileIndex = filesArray.Count - 1; } else { fileIndex--; } this.fileName = filePath + "//" + ((FileInfo)(filesArray[(fileIndex) % (filesArray.Count)])).Name; showPicture(this.fileName); } private void btnFitSwitch_Click(object sender, RoutedEventArgs e) { fitToScreen =! fitToScreen; showPicture(fileName); } private void OnRotate90Click(object sender, RoutedEventArgs e) { rotateAngle += 90; rotateAngle = rotateAngle % 360; doRotate(); } private void doRotate() { //TransformedBitmap tb = new TransformedBitmap(); //tb.BeginInit(); //tb.Source = bitmapImage; //RotateTransform transform; //if (rotateAngle==0) //{ // transform = new RotateTransform(0); // tb.Transform = transform; //} //else if (rotateAngle == 90) //{ // transform = new RotateTransform(90); // tb.Transform = transform; //} //else if (rotateAngle == 180) //{ // transform = new RotateTransform(180); // tb.Transform = transform; //} //else if (rotateAngle == 270) //{ // transform = new RotateTransform(270); // tb.Transform = transform; //} //tb.EndInit(); //this.img.Height = tb.Height; //this.img.Width = tb.Width; //this.img.Source = tb; showPicture(fileName); } private void btnRotatecounterclockwise90_Click(object sender, RoutedEventArgs e) { rotateAngle -= 90; rotateAngle += 360; rotateAngle = rotateAngle % 360; doRotate(); } private void btnRotateclockwise90_Click(object sender, RoutedEventArgs e) { rotateAngle += 90; rotateAngle = rotateAngle % 360; doRotate(); } private void OnQuitClick(object sender, RoutedEventArgs e) { Close(); } private void OnAboutMe(object sender, RoutedEventArgs e) { AboutMe aboutDialog = new AboutMe(); aboutDialog.ShowDialog(); } } }
app.xmal
<Application x:Class="MediaShow.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="Window1.xaml"> <Application.Resources> <!--定义按钮样式--> <Style TargetType="Button"> <Setter Property="Foreground" Value="Black"/> <!--修改模板属性--> <Setter Property="Template"> <Setter.Value> <!--控件模板--> <ControlTemplate TargetType="Button"> <!--背景色--> <Border x:Name="back" Opacity="0.8" CornerRadius="3"> <Border.BitmapEffect> <OuterGlowBitmapEffect Opacity="0.7" GlowSize="0" GlowColor="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Button.Background).(SolidColorBrush.Color)}" /> </Border.BitmapEffect> <Border.Background> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1.5"> <GradientBrush.GradientStops> <GradientStopCollection> <GradientStop Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Button.Background).(SolidColorBrush.Color)}" Offset="0"/> <GradientStop Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Button.Background).(SolidColorBrush.Color)}" Offset="0.4"/> <GradientStop Color="#FFF" Offset="1"/> </GradientStopCollection> </GradientBrush.GradientStops> </LinearGradientBrush> </Border.Background> <!--前景色及边框--> <Border x:Name="fore" BorderThickness="1" CornerRadius="3" BorderBrush="#5555"> <Border.Background> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientBrush.GradientStops> <GradientStopCollection> <GradientStop Color="#6FFF" Offset="0.5"/> <GradientStop Color="#1111" Offset="0.51"/> </GradientStopCollection> </GradientBrush.GradientStops> </LinearGradientBrush> </Border.Background> <!--按钮内容--> <ContentPresenter x:Name="content" HorizontalAlignment="Center" VerticalAlignment="Center" Content="{TemplateBinding Content}"> <ContentPresenter.BitmapEffect> <DropShadowBitmapEffect Color="#000" Direction="-90" ShadowDepth="2" Softness="0.1" Opacity="0.3" /> </ContentPresenter.BitmapEffect> </ContentPresenter> </Border> </Border> <!--触发器--> <ControlTemplate.Triggers> <!--鼠标移入移出--> <Trigger Property="IsMouseOver" Value="True"> <Trigger.EnterActions> <BeginStoryboard> <Storyboard> <DoubleAnimation To="6" Duration="0:0:0.2" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" /> <ColorAnimation To="#AFFF" BeginTime="0:0:0.2" Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" /> <ColorAnimation To="#3FFF" BeginTime="0:0:0.2" Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" /> </Storyboard> </BeginStoryboard> </Trigger.EnterActions> <Trigger.ExitActions> <BeginStoryboard> <Storyboard> <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" /> <ColorAnimation Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" /> <ColorAnimation Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" /> </Storyboard> </BeginStoryboard> </Trigger.ExitActions> </Trigger> <!--按钮按下弹起--> <Trigger Property="IsPressed" Value="True"> <Trigger.EnterActions> <BeginStoryboard> <Storyboard> <DoubleAnimation To="3" Duration="0:0:0.1" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" /> <ColorAnimation To="#3AAA" Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" /> <ColorAnimation To="#2111" Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" /> </Storyboard> </BeginStoryboard> </Trigger.EnterActions> <Trigger.ExitActions> <BeginStoryboard> <Storyboard> <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" /> <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" /> <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" /> </Storyboard> </BeginStoryboard> </Trigger.ExitActions> </Trigger> <!--按钮失效--> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Foreground" Value="#B444"/> <Trigger.EnterActions> <BeginStoryboard> <Storyboard> <DoubleAnimation To="0" Duration="0:0:0.3" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" /> <DoubleAnimation To="1" Duration="0:0:0.1" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Opacity)" /> <DoubleAnimation To="-135" Duration="0:0:0.1" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Direction)" /> <ColorAnimation To="#FFF" Duration="0:0:0.3" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Color)" /> <ColorAnimation To="#D555" Duration="0:0:0.3" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" /> <ColorAnimation To="#CEEE" Duration="0:0:0.3" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" /> <ColorAnimation To="#CDDD" Duration="0:0:0.3" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" /> </Storyboard> </BeginStoryboard> </Trigger.EnterActions> <Trigger.ExitActions> <BeginStoryboard> <Storyboard> <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" /> <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Opacity)" /> <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Direction)" /> <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Color)" /> <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" /> <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" /> <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" /> </Storyboard> </BeginStoryboard> </Trigger.ExitActions> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Application.Resources> </Application>
附加说明:按钮样式和菜单样式都是其它地方找的;
附上截图:
当然你还可以下载源代码:
http://rorger.download.csdn.net/
(需要付出一分哦,评价后归还分数)