This commit is contained in:
Paddy Xu
2017-04-29 21:42:37 +03:00
parent a5442faa12
commit f5d9dc3e14
56 changed files with 5308 additions and 288 deletions

View File

@@ -0,0 +1,53 @@
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
namespace QuickLook.Plugin.ImageViewer
{
internal static class ImageFileHelper
{
internal static Size GetImageSize(string path)
{
var ori = GetOrientationFromExif(path);
using (var stream = File.OpenRead(path))
{
var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.None);
var frame = decoder.Frames[0];
if (ori == ExifOrientation.Rotate90CW || ori == ExifOrientation.Rotate270CW)
return new Size {Width = frame.PixelHeight, Height = frame.PixelWidth};
return new Size {Width = frame.PixelWidth, Height = frame.PixelHeight};
}
}
internal static ExifOrientation GetOrientationFromExif(string path)
{
using (var stream = File.OpenRead(path))
{
var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.None);
var frame = decoder.Frames[0];
var orientation = ((BitmapMetadata) frame.Metadata).GetQuery(@"/app1/{ushort=0}/{ushort=274}");
if (orientation == null)
return ExifOrientation.Horizontal;
return (ExifOrientation) (ushort) orientation;
}
}
internal enum ExifOrientation
{
Horizontal = 1,
MirrorHorizontal = 2,
Rotate180 = 3,
MirrorVertical = 4,
MirrorHorizontal270CW = 5,
Rotate90CW = 6,
MirrorHorizontal90CW = 7,
Rotate270CW = 8
}
}
}

View File

@@ -0,0 +1,15 @@
<UserControl x:Class="QuickLook.Plugin.ImageViewer.ImagePanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:QuickLook.Plugin.ImageViewer"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<ScrollViewer x:Name="viewPanel" BorderThickness="0" HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto" Focusable="False">
<Image x:Name="viewPanelImage" Stretch="None" RenderOptions.BitmapScalingMode="HighQuality" />
</ScrollViewer>
</Grid>
</UserControl>

View File

@@ -0,0 +1,124 @@
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace QuickLook.Plugin.ImageViewer
{
/// <summary>
/// Interaction logic for ImagePanel.xaml
/// </summary>
public partial class ImagePanel : UserControl
{
private Point? _dragInitPos;
private double _minZoomFactor = 1d;
private double _zoomFactor = 1d;
public ImagePanel(string path)
{
InitializeComponent();
var ori = ImageFileHelper.GetOrientationFromExif(path);
var bitmap = new BitmapImage();
using (var fs = File.OpenRead(path))
{
bitmap.BeginInit();
bitmap.StreamSource = fs;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.Rotation = ori == ImageFileHelper.ExifOrientation.Rotate90CW
? Rotation.Rotate90
: ori == ImageFileHelper.ExifOrientation.Rotate270CW
? Rotation.Rotate270
: Rotation.Rotate0;
bitmap.EndInit();
}
viewPanelImage.Source = bitmap;
Loaded += (sender, e) => { ZoomToFit(); };
viewPanel.PreviewMouseWheel += ViewPanel_PreviewMouseWheel;
viewPanel.PreviewMouseLeftButtonDown += ViewPanel_PreviewMouseLeftButtonDown;
viewPanel.PreviewMouseMove += ViewPanel_PreviewMouseMove;
}
private void ViewPanel_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_dragInitPos = e.GetPosition(viewPanel);
var temp = _dragInitPos.Value; // Point is a type value
temp.Offset(viewPanel.HorizontalOffset, viewPanel.VerticalOffset);
_dragInitPos = temp;
}
private void ViewPanel_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (!_dragInitPos.HasValue)
return;
if (e.LeftButton == MouseButtonState.Released)
{
_dragInitPos = null;
return;
}
e.Handled = true;
var delta = _dragInitPos.Value - e.GetPosition(viewPanel);
viewPanel.ScrollToHorizontalOffset(delta.X);
viewPanel.ScrollToVerticalOffset(delta.Y);
}
private void ViewPanel_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
if ((Keyboard.Modifiers & ModifierKeys.Control) == 0)
return;
e.Handled = true;
var newZoom = _zoomFactor + e.Delta / 120 * 0.1;
newZoom = Math.Max(newZoom, _minZoomFactor);
newZoom = Math.Min(newZoom, 3);
Zoom(newZoom, false);
}
private void ZoomToFit()
{
var factor = Math.Min(viewPanel.ActualWidth / viewPanelImage.Source.Width,
viewPanel.ActualHeight / viewPanelImage.Source.Height);
_minZoomFactor = factor;
Zoom(factor, true);
}
private void Zoom(double factor, bool fromCenter)
{
_zoomFactor = factor;
var position = fromCenter
? new Point(viewPanelImage.Source.Width / 2, viewPanelImage.Source.Height / 2)
: Mouse.GetPosition(viewPanelImage);
viewPanelImage.LayoutTransform = new ScaleTransform(factor, factor);
viewPanel.InvalidateMeasure();
// critical for calcuating offset
viewPanel.ScrollToHorizontalOffset(0);
viewPanel.ScrollToVerticalOffset(0);
UpdateLayout();
var offset = viewPanelImage.TranslatePoint(position, viewPanel) - Mouse.GetPosition(viewPanel);
viewPanel.ScrollToHorizontalOffset(offset.X);
viewPanel.ScrollToVerticalOffset(offset.Y);
UpdateLayout();
}
}
}

View File

@@ -0,0 +1,55 @@
using System.IO;
using System.Windows;
namespace QuickLook.Plugin.ImageViewer
{
public class Plugin : IViewer
{
private Size _imageSize;
private ImagePanel _ip;
public int Priority => 9999;
public bool CanHandle(string path)
{
// TODO: determine file type by content
if (Directory.Exists(path))
return false;
switch (Path.GetExtension(path).ToLower())
{
case ".bmp":
case ".gif":
case ".ico":
case ".jpg":
case ".png":
case ".wdp":
case ".tiff":
return true;
default:
return false;
}
}
public void Prepare(string path, ViewContentContainer container)
{
_imageSize = ImageFileHelper.GetImageSize(path);
container.SetPreferedSizeFit(_imageSize, 0.8);
}
public void View(string path, ViewContentContainer container)
{
_ip = new ImagePanel(path);
container.SetContent(_ip);
container.Title = $"{Path.GetFileName(path)} ({_imageSize.Width} × {_imageSize.Height})";
}
public void Close()
{
}
}
}

View File

@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("QuickLook.Plugin.ImageViewer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("QuickLook.Plugin.ImageViewer")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("fe5a5111-9607-4721-a7be-422754002ed8")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{FE5A5111-9607-4721-A7BE-422754002ED8}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>QuickLook.Plugin.ImageViewer</RootNamespace>
<AssemblyName>QuickLook.Plugin.ImageViewer</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\..\Build\Debug\Plugins\QuickLook.Plugin.ImageViewer\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\Build\Release\Plugins\QuickLook.Plugin.ImageViewer\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
<Reference Include="WpfAnimatedGif, Version=1.4.14.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\WpfAnimatedGif.1.4.14\lib\net\WpfAnimatedGif.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ImageFileHelper.cs" />
<Compile Include="ImagePanel.xaml.cs">
<DependentUpon>ImagePanel.xaml</DependentUpon>
</Compile>
<Compile Include="Plugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\QuickLook\QuickLook.csproj">
<Project>{8b4a9ce5-67b5-4a94-81cb-3771f688fdeb}</Project>
<Name>QuickLook</Name>
<Private>False</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Page Include="ImagePanel.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="WpfAnimatedGif" version="1.4.14" targetFramework="net452" />
</packages>