110,571
社区成员
发帖
与我相关
我的任务
分享
<!--数据字典中,类似的Image有200多个-->
<ResourceDictionary x:Class="MineRecipes.MineDictionary"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Image x:Shared="False" x:Key="small" Source="/MineRecipes;component/Imgs/BackGround/Grid_layout_None_%28small%29.png"/>
<!--前台XAML的绑定,可以正常访问-->
<Border Grid.Column="0" Grid.Row="0" Child="{StaticResource ResourceKey=small}" />
//后台代码
//通过IO获取程序目录下Imgs文件夹内的所有文件
string[] strRecipes = Directory.GetFiles("..\\..\\Imgs");
foreach (var v in strRecipes)
{
if (!v.ToLower().EndsWith(".png", StringComparison.CurrentCulture)) continue;//过滤器
string ImgPath = _strPath + "\\" + v;//Imgs下所有png的全路径
Image img = new Image() { Source = new BitmapImage(new Uri(ImgPath)), Stretch = Stretch.None };
stkRecipes.Children.Add(img);//加进一个StackPanel
}
book.Open();
foreach (var each in book) {
if (first == null) { first = each; }
Image image = new Image();
image.Tag = each;
image.MouseDown += new MouseButtonEventHandler(image_MouseDown);
image.Source = each.ImageSource;
Binding binding = new Binding("ImageSource");
binding.Source = each;
image.SetBinding(Image.SourceProperty, binding);
image.Stretch = Stretch.Uniform;
WrapPanel1.Children.Add(new Border { BorderThickness = new Thickness(1), BorderBrush = Brushes.DarkGray, Child = image });
}
<ScrollViewer>
<WrapPanel x:Name="WrapPanel1" Margin="10, 5">
<WrapPanel.Resources>
<Style TargetType="Border">
<Setter Property="Width" Value="300" />
<Setter Property="Height" Value="370" />
<Setter Property="Margin" Value="5,3" />
</Style>
<Style TargetType="Image">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Stretch" />
</Style>
</WrapPanel.Resources>
</WrapPanel>
</ScrollViewer>
public class Book : ObservableCollection<Page> {
public void Open() {
string sampleFolder = @"C:\Users\nonocast.DEV.000\Desktop\BookSolution\Sample - 副本";
int i = 0;
foreach (var each in Directory.GetFiles(sampleFolder)) {
this.Add(new Page(i++, each));
}
}
public void Load(Page page) {
if (cache.Contains(page)) return;
if (cache.Count >= 3) {
cache.Dequeue().BeginLoadLowQuality();
}
page.BeginLoad();
cache.Enqueue(page);
}
private Queue<Page> cache = new Queue<Page>();
}
public class Page : DependencyObject {
public int Index { get; private set; }
public string ImagePath { get; private set; }
public BitmapImage ImageSource {
get { return (BitmapImage)GetValue(ImageSourceProperty); }
set { SetValue(ImageSourceProperty, value); }
}
public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ImageSource", typeof(BitmapImage), typeof(Page), new UIPropertyMetadata(null));
public Page(int index, string imagePath) {
this.Index = index;
this.ImagePath = imagePath;
this.ImageSource = defaultImage;
}
private BitmapImage GetLowQualityImage() {
BitmapImage result = null;
if (lowQualityImage == null) {
result = new BitmapImage();
result.BeginInit();
result.StreamSource = new MemoryStream(File.ReadAllBytes(ImagePath));
result.CacheOption = BitmapCacheOption.OnLoad;
result.DecodePixelWidth = 100;
result.EndInit();
lowQualityImage = result;
} else {
result = lowQualityImage;
}
return result;
}
static Page() {
var ms = new MemoryStream(File.ReadAllBytes(@"Resources\blank.jpg"));
defaultImage = new BitmapImage();
defaultImage.BeginInit();
defaultImage.CacheOption = BitmapCacheOption.OnLoad;
defaultImage.StreamSource = ms;
defaultImage.EndInit();
}
public void BeginLoad() {
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = new MemoryStream(File.ReadAllBytes(ImagePath));
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
ImageSource = bitmap;
}
public void BeginLoadLowQuality() {
ImageSource = GetLowQualityImage();
}
private BitmapImage lowQualityImage;
private static BitmapImage defaultImage;
}