mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 02:36:19 +02:00
* [Analyzers][AdvancedPaste] Apply fix for SA1516 * [Analyzers][EnvironmentVariables] Apply fix for SA1516 * [Analyzers][RegistryPreview] Apply fix for SA1516 * [Analyzers][Peek] Apply fix for SA1516 * [Analyzers][PreviewPane] Apply fix for SA1516 * [Analyzers][FancyZones] Apply fix for SA1516 * [Analyzers][PT Run][Plugins] Apply fix for SA1516 * [Analyzers][PT Run] Apply fix for SA1516 * [Analyzers][PT Run][Wox] Apply fix for SA1516 * [Analyzers][Common] Apply fix for SA1516 * [Analyzers][ImageResizer] Apply fix for SA1516 * [Analyzers][ColorPicker] Apply fix for SA1516 * [Analyzers][MouseUtils] Apply fix for SA1516 * [Analyzers][DSC Schema Generator] Apply fix for SA1516 * [Analyzers][FileLocksmith] Apply fix for SA1516 * [Analyzers][Hosts] Apply fix for SA1516 * [Analyzers][MeasureTool] Apply fix for SA1516 * [Analyzers][MouseWithoutBorders] Apply fix for SA1516 * [Analyzers][TextExtractor] Apply fix for SA1516 * [Analyzers][Workspaces] Apply fix for SA1516 * [Analyzers][Awake] Apply fix for SA1516 * [Analyzers][PowerAccent] Apply fix for SA1516 * [Analyzers][RegistryPreview] Apply fix for SA1516 * [Analyzers][Settings] Apply fix for SA1516 * [Analyzers][MouseWithoutBorders] Apply fix for SA1616
67 lines
2.5 KiB
C#
67 lines
2.5 KiB
C#
// 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.Reflection;
|
|
using System.Security.Cryptography;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
|
|
using Wox.Plugin.Logger;
|
|
|
|
namespace Wox.Infrastructure.Image
|
|
{
|
|
public class ImageHashGenerator : IImageHashGenerator
|
|
{
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5350:Do Not Use Weak Cryptographic Algorithms", Justification = "Level of protection needed for the image data does not require a security guarantee")]
|
|
public string GetHashFromImage(ImageSource image, string filePath)
|
|
{
|
|
if (!(image is BitmapSource bitmapSource))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
using (var outStream = new MemoryStream())
|
|
{
|
|
// Dynamically selecting the encoder based on the file extension to preserve the original image format characteristics as much as possible.
|
|
BitmapEncoder encoder = GetEncoderByFileExtension(filePath);
|
|
var bitmapFrame = BitmapFrame.Create(bitmapSource);
|
|
bitmapFrame.Freeze();
|
|
encoder.Frames.Add(bitmapFrame);
|
|
encoder.Save(outStream);
|
|
var byteArray = outStream.GetBuffer();
|
|
return Convert.ToBase64String(SHA1.HashData(byteArray));
|
|
}
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
Log.Exception($"Failed to get hash from image", e, MethodBase.GetCurrentMethod().DeclaringType);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static BitmapEncoder GetEncoderByFileExtension(string filePath)
|
|
{
|
|
string fileExtension = Path.GetExtension(filePath).ToLowerInvariant();
|
|
|
|
switch (fileExtension)
|
|
{
|
|
case ".png":
|
|
return new PngBitmapEncoder();
|
|
case ".jpg":
|
|
case ".jpeg":
|
|
return new JpegBitmapEncoder();
|
|
case ".bmp":
|
|
return new BmpBitmapEncoder();
|
|
default:
|
|
// Default to PNG if the format is unknown or unsupported because PNG is a lossless compression format
|
|
return new PngBitmapEncoder();
|
|
}
|
|
}
|
|
}
|
|
}
|