mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
[FileExplorer] StlThumbnailProvider (#15568)
* Adds StlThumbnailProvider * Spell checker fixes * Adds missing changes * Attempts to fix alpha background issue * Adds missing dependency references * Upgrades .NET Core 3.1 to .NET 5 * Updates Helix Toolkit to fix .net5 compatibility * Return null bitmap If STL model is empty
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Media3D;
|
||||
using Common.ComInterlop;
|
||||
using Common.Utilities;
|
||||
using HelixToolkit.Wpf;
|
||||
using Bitmap = System.Drawing.Bitmap;
|
||||
|
||||
namespace Microsoft.PowerToys.ThumbnailHandler.Stl
|
||||
{
|
||||
/// <summary>
|
||||
/// Stl Thumbnail Provider.
|
||||
/// </summary>
|
||||
[Guid("8BC8AFC2-4E7C-4695-818E-8C1FFDCEA2AF")]
|
||||
[ClassInterface(ClassInterfaceType.None)]
|
||||
[ComVisible(true)]
|
||||
public class StlThumbnailProvider : IInitializeWithStream, IThumbnailProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the stream object to access file.
|
||||
/// </summary>
|
||||
public IStream Stream { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The maximum dimension (width or height) thumbnail we will generate.
|
||||
/// </summary>
|
||||
private const uint MaxThumbnailSize = 10000;
|
||||
|
||||
/// <summary>
|
||||
/// Loads the Stl model into a Viewport3D and renders a bitmap of it.
|
||||
/// </summary>
|
||||
/// <param name="stream">The Stream instance for the Stl content.</param>
|
||||
/// <param name="cx">The maximum thumbnail size, in pixels.</param>
|
||||
/// <returns>A thumbnail rendered from the Stl model.</returns>
|
||||
public static Bitmap GetThumbnail(Stream stream, uint cx)
|
||||
{
|
||||
if (cx > MaxThumbnailSize || stream == null || stream.Length == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
Bitmap thumbnail = null;
|
||||
|
||||
var stlReader = new StLReader
|
||||
{
|
||||
DefaultMaterial = new DiffuseMaterial(new SolidColorBrush(Color.FromRgb(255, 201, 36))),
|
||||
};
|
||||
|
||||
var model = stlReader.Read(stream);
|
||||
|
||||
if (model.Bounds == Rect3D.Empty)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var viewport = new System.Windows.Controls.Viewport3D();
|
||||
|
||||
viewport.Measure(new Size(cx, cx));
|
||||
viewport.Arrange(new Rect(0, 0, cx, cx));
|
||||
|
||||
var modelVisual = new ModelVisual3D()
|
||||
{
|
||||
Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), 180)),
|
||||
};
|
||||
viewport.Children.Add(modelVisual);
|
||||
viewport.Children.Add(new DefaultLights());
|
||||
|
||||
var perspectiveCamera = new PerspectiveCamera
|
||||
{
|
||||
Position = new Point3D(1, 2, 1),
|
||||
LookDirection = new Vector3D(-1, -2, -1),
|
||||
UpDirection = new Vector3D(0, 0, 1),
|
||||
FieldOfView = 20,
|
||||
NearPlaneDistance = 0.1,
|
||||
FarPlaneDistance = double.PositiveInfinity,
|
||||
};
|
||||
viewport.Camera = perspectiveCamera;
|
||||
|
||||
modelVisual.Content = model;
|
||||
|
||||
perspectiveCamera.ZoomExtents(viewport);
|
||||
|
||||
var bitmapExporter = new BitmapExporter
|
||||
{
|
||||
Background = new SolidColorBrush(Colors.Transparent),
|
||||
OversamplingMultiplier = 1,
|
||||
};
|
||||
|
||||
var bitmapStream = new MemoryStream();
|
||||
|
||||
bitmapExporter.Export(viewport, bitmapStream);
|
||||
|
||||
bitmapStream.Position = 0;
|
||||
|
||||
thumbnail = new Bitmap(bitmapStream);
|
||||
|
||||
return thumbnail;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Initialize(IStream pstream, uint grfMode)
|
||||
{
|
||||
// Ignore the grfMode always use read mode to access the file.
|
||||
this.Stream = pstream;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void GetThumbnail(uint cx, out IntPtr phbmp, out WTS_ALPHATYPE pdwAlpha)
|
||||
{
|
||||
phbmp = IntPtr.Zero;
|
||||
pdwAlpha = WTS_ALPHATYPE.WTSAT_UNKNOWN;
|
||||
|
||||
if (cx == 0 || cx > MaxThumbnailSize)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
using (var stream = new ReadonlyStream(this.Stream as IStream))
|
||||
{
|
||||
using (var memStream = new MemoryStream())
|
||||
{
|
||||
stream.CopyTo(memStream);
|
||||
|
||||
memStream.Position = 0;
|
||||
|
||||
using (Bitmap thumbnail = GetThumbnail(memStream, cx))
|
||||
{
|
||||
if (thumbnail != null && thumbnail.Size.Width > 0 && thumbnail.Size.Height > 0)
|
||||
{
|
||||
phbmp = thumbnail.GetHbitmap(System.Drawing.Color.Transparent);
|
||||
pdwAlpha = WTS_ALPHATYPE.WTSAT_ARGB;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<Platforms>x64</Platforms>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
<ProjectGuid>{F7C8C0F1-5431-4347-89D0-8E5354F93CF2}</ProjectGuid>
|
||||
<RootNamespace>Microsoft.PowerToys.ThumbnailHandler.Stl</RootNamespace>
|
||||
<AssemblyName>PowerToys.StlThumbnailProvider</AssemblyName>
|
||||
<AssemblyTitle>PowerToys.StlThumbnailProvider</AssemblyTitle>
|
||||
<AssemblyDescription>PowerToys StlPreviewHandler</AssemblyDescription>
|
||||
<AssemblyCompany>Microsoft Corporation</AssemblyCompany>
|
||||
<AssemblyCopyright>Copyright (C) 2020 Microsoft Corporation</AssemblyCopyright>
|
||||
<AssemblyProduct>PowerToys</AssemblyProduct>
|
||||
<TargetFramework>net5.0-windows</TargetFramework>
|
||||
<EnableComHosting>true</EnableComHosting>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<Company>Microsoft Corporation</Company>
|
||||
<Product>PowerToys</Product>
|
||||
<NeutralLanguage>en-US</NeutralLanguage>
|
||||
<Description>PowerToys StlPreviewHandler</Description>
|
||||
<Copyright>Copyright (C) 2020 Microsoft Corporation</Copyright>
|
||||
<OutputPath>$(SolutionDir)$(Platform)\$(Configuration)\modules\FileExplorerPreview\</OutputPath>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
|
||||
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
|
||||
<UseWPF>true</UseWPF>
|
||||
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
|
||||
</PropertyGroup>
|
||||
|
||||
<Import Project="..\..\..\Version.props" />
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="HelixToolkit" Version="2.20.1" />
|
||||
<PackageReference Include="HelixToolkit.Core.Wpf" Version="2.20.1" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.VersionCheckAnalyzer" Version="3.3.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.CodeQuality.Analyzers" Version="3.3.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NetCore.Analyzers" Version="3.3.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NetFramework.Analyzers" Version="3.3.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\..\..\codeAnalysis\GlobalSuppressions.cs" Link="GlobalSuppressions.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\common\ManagedTelemetry\Telemetry\ManagedTelemetry.csproj" />
|
||||
<ProjectReference Include="..\Common\PreviewHandlerCommon.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<AdditionalFiles Include="..\..\..\codeAnalysis\StyleCop.json">
|
||||
<Link>StyleCop.json</Link>
|
||||
</AdditionalFiles>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user