Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <Window x:Class="BitmapTests.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:BitmapTests"
- mc:Ignorable="d"
- Title="MainWindow" Height="350" Width="525">
- <Grid>
- <ListView ItemsSource="{Binding Images}"
- VirtualizingStackPanel.VirtualizationMode="Recycling"
- VirtualizingStackPanel.CleanUpVirtualizedItem="ItemsControl_CleanUpVirtualizedItem">
- <ListView.ItemTemplate>
- <DataTemplate>
- <StackPanel Orientation="Horizontal">
- <Image Source="{Binding Image50}" Width="50" HorizontalAlignment="Left" />
- <TextBlock Text="{Binding Name}" Padding="10" />
- </StackPanel>
- </DataTemplate>
- </ListView.ItemTemplate>
- </ListView>
- </Grid>
- </Window>
- using System;
- using System.Collections.ObjectModel;
- using System.Diagnostics.Eventing;
- using System.IO;
- using System.Reflection;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- namespace BitmapTests
- {
- public partial class MainWindow : Window
- {
- private ImageModel _model;
- public MainWindow()
- {
- InitializeComponent();
- _model = new ImageModel(@"put your path here"); // TODO
- DataContext = _model;
- }
- private void ItemsControl_CleanUpVirtualizedItem(object sender, CleanUpVirtualizedItemEventArgs e)
- {
- ImageModel.Log("Clean '" + ((ImageObject)e.Value).FilePath + "' pressure: " + ImageModel.GetMemoryPressureMega());
- }
- }
- public class ImageObject
- {
- private ImageSource _image;
- public ImageObject(string path)
- {
- FilePath = path;
- Name = Path.GetFileName(path);
- }
- public string Name { get; }
- public string FilePath { get; }
- public ImageSource Image50
- {
- get
- {
- if (_image == null)
- {
- _image = ImageModel.CreateImageForThumbnail(FilePath, 50);
- ImageModel.Log("Load '" + FilePath + "'. pressure: " + ImageModel.GetMemoryPressureMega());
- }
- return _image;
- }
- }
- }
- public class ImageModel
- {
- private static EventProvider _trace = new EventProvider(new Guid("9f704449-67aa-4888-8484-3fdfb6ce4854"));
- private static FieldInfo _totalMemory;
- public ImageModel(string directoryPath)
- {
- if (directoryPath == null)
- throw new ArgumentNullException("directoryPath");
- Log("Rendering Tier: " + (RenderCapability.Tier >> 16));
- Images = new ObservableCollection<ImageObject>();
- foreach (var file in Directory.EnumerateFiles(directoryPath))
- {
- string ext = Path.GetExtension(file).ToLowerInvariant();
- if (ext != ".gif" && ext != ".jpg" && ext != ".jpeg" && ext != ".png")
- continue;
- var im = new ImageObject(file);
- Images.Add(im);
- }
- }
- public ObservableCollection<ImageObject> Images { get; private set; }
- public static void Log(string value)
- {
- _trace.WriteMessageEvent(value);
- }
- public static long GetMemoryPressureMega()
- {
- if (_totalMemory == null)
- {
- _totalMemory = typeof(Clipboard).Assembly.GetType("MS.Internal.MemoryPressure").GetField("_totalMemory", BindingFlags.Static | BindingFlags.NonPublic);
- }
- return (long)_totalMemory.GetValue(null) / 1000000;
- }
- public static ImageSource CreateImageForThumbnail(string path, int decodePixelWidth)
- {
- var bmp = new BitmapImage();
- using (var stream = File.OpenRead(path))
- {
- bmp.BeginInit();
- bmp.StreamSource = stream;
- if (decodePixelWidth > 0)
- {
- bmp.DecodePixelWidth = decodePixelWidth;
- }
- bmp.CacheOption = BitmapCacheOption.OnLoad;
- bmp.EndInit();
- bmp.Freeze();
- return bmp;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement