Advertisement
Guest User

Untitled

a guest
Mar 20th, 2016
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.69 KB | None | 0 0
  1. <Window x:Class="BitmapTests.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.         xmlns:local="clr-namespace:BitmapTests"
  7.         mc:Ignorable="d"
  8.         Title="MainWindow" Height="350" Width="525">
  9.     <Grid>
  10.         <ListView ItemsSource="{Binding Images}"
  11.                   VirtualizingStackPanel.VirtualizationMode="Recycling"
  12.                   VirtualizingStackPanel.CleanUpVirtualizedItem="ItemsControl_CleanUpVirtualizedItem">
  13.             <ListView.ItemTemplate>
  14.                 <DataTemplate>
  15.                     <StackPanel Orientation="Horizontal">
  16.                         <Image Source="{Binding Image50}" Width="50" HorizontalAlignment="Left" />
  17.                         <TextBlock Text="{Binding Name}" Padding="10" />
  18.                     </StackPanel>
  19.                 </DataTemplate>
  20.             </ListView.ItemTemplate>
  21.         </ListView>
  22.     </Grid>
  23. </Window>
  24.  
  25. using System;
  26. using System.Collections.ObjectModel;
  27. using System.Diagnostics.Eventing;
  28. using System.IO;
  29. using System.Reflection;
  30. using System.Windows;
  31. using System.Windows.Controls;
  32. using System.Windows.Media;
  33. using System.Windows.Media.Imaging;
  34.  
  35. namespace BitmapTests
  36. {
  37.     public partial class MainWindow : Window
  38.     {
  39.         private ImageModel _model;
  40.         public MainWindow()
  41.         {
  42.             InitializeComponent();
  43.             _model = new ImageModel(@"put your path here"); // TODO
  44.             DataContext = _model;
  45.         }
  46.  
  47.         private void ItemsControl_CleanUpVirtualizedItem(object sender, CleanUpVirtualizedItemEventArgs e)
  48.         {
  49.             ImageModel.Log("Clean '" + ((ImageObject)e.Value).FilePath + "' pressure: " + ImageModel.GetMemoryPressureMega());
  50.         }
  51.     }
  52.  
  53.     public class ImageObject
  54.     {
  55.         private ImageSource _image;
  56.  
  57.         public ImageObject(string path)
  58.         {
  59.             FilePath = path;
  60.             Name = Path.GetFileName(path);
  61.         }
  62.  
  63.         public string Name { get; }
  64.         public string FilePath { get; }
  65.  
  66.         public ImageSource Image50
  67.         {
  68.             get
  69.             {
  70.                 if (_image == null)
  71.                 {
  72.                     _image = ImageModel.CreateImageForThumbnail(FilePath, 50);
  73.                     ImageModel.Log("Load '" + FilePath + "'. pressure: " + ImageModel.GetMemoryPressureMega());
  74.                 }
  75.                 return _image;
  76.             }
  77.         }
  78.     }
  79.  
  80.     public class ImageModel
  81.     {
  82.         private static EventProvider _trace = new EventProvider(new Guid("9f704449-67aa-4888-8484-3fdfb6ce4854"));
  83.         private static FieldInfo _totalMemory;
  84.  
  85.         public ImageModel(string directoryPath)
  86.         {
  87.             if (directoryPath == null)
  88.                 throw new ArgumentNullException("directoryPath");
  89.  
  90.             Log("Rendering Tier: " + (RenderCapability.Tier >> 16));
  91.  
  92.             Images = new ObservableCollection<ImageObject>();
  93.             foreach (var file in Directory.EnumerateFiles(directoryPath))
  94.             {
  95.                 string ext = Path.GetExtension(file).ToLowerInvariant();
  96.                 if (ext != ".gif" && ext != ".jpg" && ext != ".jpeg" && ext != ".png")
  97.                     continue;
  98.  
  99.                 var im = new ImageObject(file);
  100.                 Images.Add(im);
  101.             }
  102.         }
  103.  
  104.         public ObservableCollection<ImageObject> Images { get; private set; }
  105.  
  106.         public static void Log(string value)
  107.         {
  108.             _trace.WriteMessageEvent(value);
  109.         }
  110.  
  111.         public static long GetMemoryPressureMega()
  112.         {
  113.             if (_totalMemory == null)
  114.             {
  115.                 _totalMemory = typeof(Clipboard).Assembly.GetType("MS.Internal.MemoryPressure").GetField("_totalMemory", BindingFlags.Static | BindingFlags.NonPublic);
  116.             }
  117.             return (long)_totalMemory.GetValue(null) / 1000000;
  118.         }
  119.  
  120.         public static ImageSource CreateImageForThumbnail(string path, int decodePixelWidth)
  121.         {
  122.             var bmp = new BitmapImage();
  123.             using (var stream = File.OpenRead(path))
  124.             {
  125.                 bmp.BeginInit();
  126.                 bmp.StreamSource = stream;
  127.                 if (decodePixelWidth > 0)
  128.                 {
  129.                     bmp.DecodePixelWidth = decodePixelWidth;
  130.                 }
  131.                 bmp.CacheOption = BitmapCacheOption.OnLoad;
  132.                 bmp.EndInit();
  133.                 bmp.Freeze();
  134.                 return bmp;
  135.             }
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement