WPF 照片墙的实现
主要参照了DevExpress的PhotoGallery实例的实现。
效果如下:
照片墙核心代码如下:
PhotoGallery.xaml
<local:CarouselDemoModule x:Class="PictureMagic.PhotoGallery"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxca="http://schemas.devexpress.com/winfx/2008/xaml/carousel"
xmlns:dxdb="http://schemas.devexpress.com/winfx/2008/xaml/demobase"
xmlns:local="clr-namespace:PictureMagic"
xmlns:collection="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" >
<dxdb:DemoModuleControl>
<Grid ClipToBounds="False" Background="#FFB6C1">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition Height="" />
</Grid.RowDefinitions>
<dxca:CarouselPanel RenderOptions.BitmapScalingMode="HighQuality"
x:Name="carousel"
AnimationTime=""
VisibleItemCount=""
AttractorPointIndex=""
PathSizingMode="Stretch"
PathPadding="10,5,10,20"
IsAutoSizeItem="True"
ClipToBounds="True"
PathVisible="False"
IsInvertedDirection="True"
Grid.RowSpan=""
IsRepeat="True"
ItemSize="100,100" >
<dxca:CarouselPanel.Resources>
<ControlTemplate x:Key="itemTemplate" TargetType="{x:Type ContentControl}">
<Grid VerticalAlignment="Center">
<Border Margin="3,3,0,0" Background="Black" Opacity="0.25" CornerRadius="" />
<Border Margin="0,0,3,3" Padding="" BorderBrush="#5F000000" BorderThickness="" Background="White">
<Image Source="{Binding Path=DataContext, RelativeSource={RelativeSource TemplatedParent}}" Stretch="Uniform" />
</Border>
</Grid>
</ControlTemplate>
<Style TargetType="{x:Type FrameworkElement}" x:Key="itemStyle">
<Setter Property="RenderTransformOrigin" Value="0.5, 0.5" />
<Setter Property="Opacity" Value="{Binding Path=(dxca:CarouselPanel.Parameters).Opacity, RelativeSource={RelativeSource Self}}" />
<Setter Property="Panel.ZIndex" Value="{Binding Path=(dxca:CarouselPanel.Parameters).ZIndex, Converter={local:DoubleToIntConverter}, RelativeSource={RelativeSource Self}}" />
<Setter Property="RenderTransform">
<Setter.Value>
<TransformGroup>
<ScaleTransform ScaleX="{Binding Path=(dxca:CarouselPanel.Parameters).Scale, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContentControl}}}"
ScaleY="{Binding Path=(dxca:CarouselPanel.Parameters).Scale, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContentControl}}}"
/>
<TranslateTransform X="{Binding Path=(dxca:CarouselPanel.Parameters).OffsetX, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContentControl}}}"
Y="{Binding Path=(dxca:CarouselPanel.Parameters).OffsetY, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContentControl}}}"
/>
</TransformGroup>
</Setter.Value>
</Setter>
</Style>
</dxca:CarouselPanel.Resources>
<dxca:CarouselPanel.ParameterSet>
<dxca:ParameterCollection>
<dxca:Parameter Name="Opacity" DistributionFunction="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type dxca:CarouselPanel}, ResourceId=Linear3PConvexNr}}" />
<dxca:Parameter Name="Scale" DistributionFunction="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type dxca:CarouselPanel}, ResourceId=Linear3PConvexNr}}" />
<dxca:Parameter Name="ZIndex" DistributionFunction="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type dxca:CarouselPanel}, ResourceId=Linear3PConvexERIntMax}}" />
</dxca:ParameterCollection>
</dxca:CarouselPanel.ParameterSet>
<dxca:CarouselPanel.ItemMovingPath>
<PathGeometry Figures="M255,0 C352.86864,0.5 454.5,61.389274 454.5,136.5 C454.5,211.61073 352.86864,272.5 227.5,272.5 C102.13136,272.5 0.5,211.61073 0.5,136.5 C0.5,61.389274 102.13136,0.5 200,0.5 " />
</dxca:CarouselPanel.ItemMovingPath>
</dxca:CarouselPanel>
<dxdb:ImageControl Margin="30,30,30,0" Source="{Binding ElementName=carousel, Path=ActiveItem.DataContext}" VerticalAlignment="Center" HorizontalAlignment="Center" />
<dxca:CarouselNavigator
VerticalAlignment="Center"
HorizontalAlignment="Center"
MinHeight=""
MinWidth=""
Grid.Row=""
Carousel="{Binding ElementName=carousel}"
/>
</Grid>
</dxdb:DemoModuleControl>
</local:CarouselDemoModule>
PhotoGallery.xaml.cs
using System.Windows;
using System.Windows.Controls;
using DevExpress.Xpf.Carousel;
using System.Collections.Generic; namespace PictureMagic
{
public partial class PhotoGallery : CarouselDemoModule { private List<PicutureInfo> m_picureInfoList = null;
public PhotoGallery() {
InitializeComponent();
m_picureInfoList = new List<PicutureInfo>();
}
protected override void AddItems(string path, ItemType it, CarouselPanel carousel) {
var items = CreateItems(path, it);
foreach (var item in items) {
ContentControl control = new ContentControl();
control.Template = carousel.Resources["itemTemplate"] as ControlTemplate;
control.Style = carousel.Resources["itemStyle"] as Style;
control.DataContext = ((Image)item).Source;
carousel.Children.Add(control);
}
} public void ShowNextImage()
{
carousel.MoveNext();
} public void SearchImage(string path)
{
carousel.Children.Clear();
AddItems(path, ItemType.BinaryImage, carousel);
}
}
}
CarouselDemoModule.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DevExpress.Utils;
using DevExpress.Xpf.Carousel;
using DevExpress.Xpf.DemoBase; namespace PictureMagic
{
public class CarouselDemoModule : DemoModule
{
static CarouselDemoModule()
{
Type ownerType = typeof(CarouselDemoModule);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
CommandManager.InvalidateRequerySuggested();
}
protected virtual List<FrameworkElement> CreateItems(string path, ItemType it)
{
ContentLoadHelper contentLoadHelper = new ContentLoadHelper();
contentLoadHelper.Path = path;
var itemList = new List<FrameworkElement>(contentLoadHelper.LoadItems(it).ToArray());
for (int i = ; i < itemList.Count; i++)
{
itemList[i].Name = "Item" + i.ToString();
((Image)itemList[i]).Stretch = System.Windows.Media.Stretch.Fill;
}
return itemList;
}
protected virtual void AddItems(string path, ItemType it, CarouselPanel carousel)
{
var itemList = CreateItems(path, it);
foreach (var item in itemList)
{
item.Name = "item" + carousel.Children.Count;
carousel.Children.Add(item);
}
}
}
}
Utils.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Data;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging; namespace PictureMagic
{
public enum ItemType { BinaryImage, DrawingImage }
public class ContentLoadHelper
{
public string Path { get; set; }
public ContentLoadHelper()
{
}
public List<FrameworkElement> LoadItems(ItemType it)
{
LoadDelegate loadDelegate = null;
switch (it)
{
case ItemType.BinaryImage:
loadDelegate = LoadImage;
break;
case ItemType.DrawingImage:
// loadDelegate = LoadDrawingImage;
break;
}
var items = new List<FrameworkElement>();
if (loadDelegate != null)
{
DirectoryInfo folder = new DirectoryInfo(Path); foreach (FileInfo file in folder.GetFiles("*.*"))
{
if (IsPhotoFile(file.Extension))
{
items.Add(loadDelegate(file.FullName));
}
}
}
return items;
} private bool IsPhotoFile(string extension)
{
string[] exArray = { ".PNG", ".JPG", ".BMP", ".GIF", ".JPEG" };
foreach (string strExe in exArray)
{
if (extension.ToUpper().Equals(strExe))
{
return true;
}
}
return false;
} public delegate FrameworkElement LoadDelegate(string strPath);
public Image LoadDrawingImage(Stream stream)
{
var rd = (ResourceDictionary)XamlReader.Load(stream);
var di = (DrawingImage)rd["Layer_1"];
return new Image() { Source = di };
}
public Image LoadImage(string strPath)
{
var image = new Image();
image.Source = new BitmapImage(new Uri(strPath));
return image;
}
} public class DoubleToIntConverter : MarkupExtension, IValueConverter
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (targetType != typeof(int))
throw new InvalidOperationException();
return (int)((double)value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
}
WPF 照片墙的实现的更多相关文章
- 牛逼哄哄的Qt库
目录 一.有价值 - 好的网站 - 好的文章 二.Qt开源库-工具 - QtXlsx--excel读写库 三.Qt开源库-控件 - libqxt编译 - Qwt - QCustomPlot - 其他 ...
- WPF 制作聊天窗口获取历史聊天记录
腾讯从QQ2013版起开始在聊天记录里添加了历史记录查看功能,个人聊天窗口可以点击最上边的‘查看历史消息’,而群组里的未读消息可以通过滚动鼠标中键或者拖动滚动条加载更多消息,那这个用wpf怎么实现呢? ...
- wpf 自定义窗口,最大化时不覆盖任务栏
相信很多人使用wpf时会选择自定义美观的窗口,因此会设置WindowStyle="None" 取消自带的标题栏.但这样使用 WindowState="Maximized& ...
- 在WPF中使用依赖注入的方式创建视图
在WPF中使用依赖注入的方式创建视图 0x00 问题的产生 互联网时代桌面开发真是越来越少了,很多应用都转到了浏览器端和移动智能终端,相应的软件开发上的新技术应用到桌面开发的文章也很少.我之前主要做W ...
- MVVM框架从WPF移植到UWP遇到的问题和解决方法
MVVM框架从WPF移植到UWP遇到的问题和解决方法 0x00 起因 这几天开始学习UWP了,之前有WPF经验,所以总体感觉还可以,看了一些基础概念和主题,写了几个测试程序,突然想起来了前一段时间在W ...
- MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息
MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二 ...
- MVVM模式解析和在WPF中的实现(五)View和ViewModel的通信
MVVM模式解析和在WPF中的实现(五) View和ViewModel的通信 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 M ...
- MVVM设计模式和WPF中的实现(四)事件绑定
MVVM设计模式和在WPF中的实现(四) 事件绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
- MVVM模式解析和在WPF中的实现(三)命令绑定
MVVM模式解析和在WPF中的实现(三) 命令绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
随机推荐
- 号外号外!解决github+hexo+yilia评论插件的问题!!!
先走一波效果图! 本人网站--http://www.wenzheng.club/ ps:效果还是不错的,支持QQ微信登录,支持表情,甚至gif动图评论! 插件采用韩国服务器的来必力评论插件--h ...
- 合理使用软引用和弱引用,提升JVM内存使用性能
在项目运行时,OOM异常是比较处理的,因为从日志看出的发生异常的代码点可能仅仅是最后一根稻草,从中可能未必能发现OOM的原因,而且OOM未必是固定重现的. 上医治未病,与其等OOM问题发生时再通过看日 ...
- 珍爱生命,远离JS=>JS避坑记
JavaScript避坑记 转载请注明源地址: http://www.cnblogs.com/funnyzpc/p/8407952.html 上图=> 有意思的漫画,不知大家看懂了没,这里我想说 ...
- TensorFlow-相关 API(学习笔记 )
1.tf.nn.conv2d conv2d( input, filter, strides, padding, use_cudnn_on_gpu=True, data_format='NHWC', n ...
- 锐捷配置telnet
.组网需求 通过Telnet功能远程登录管理设备. 二.组网拓扑 三.配置要点 1.需要给交换机配置一个管理IP,如果PC与交换机不是同一个网段,需要给交换机配置一个默认网关 2.需要配置一个 ...
- php应用pack函数转unicode为utf8
因为时常用到json_encode去处理数据,json_encode在处理字符串遇上中文时,会把中文转换成\u5371这种格式的字符串,如果想让它能正常显示中文,则可以用pack打包函数进行处理. 以 ...
- iOS-电子书开发 笔记
前言 刚接手电子书项目时,和安卓开发者pt Cai老师[aipiti Cai,一个我很敬佩很资深的开发工程师,设计领域:c++.Java.安卓.QT等]共同商议了一下,因为项目要做要同步,移动端[手机 ...
- Go笔记-继承
[Go中继承的实现] 当一个匿名类型被内嵌在结构体中时,匿名类型的可见方法也同样被内嵌,这在效果上等同于外层类型 继承 了这些方法:将父类型放在子类型中来实现亚型 package main i ...
- Hadoop学习笔记三
一.设置HDFS不进行权限检查 默认的HDFS上的文件类似于Linux中的文件,是有权限的.例如test用户创建的文件,root用户如果没有写权限,则不能进行删除. 有2种办法进行修改,修改文件的权限 ...
- 济南清北学堂游记 Day 1.
快住手!这根本不是暴力! 刷了一整天的题就是了..上午三道题的画风还算挺正常,估计是第一天,给点水题做做算了.. rqy大佬AK了上午的比赛! 当时我t2暴力写挂,还以为需要用啥奇怪的算法,后来发现, ...