mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-15 03:59:15 +08:00
49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Security.Cryptography;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
|
|
namespace Wox.Infrastructure.Image
|
|
{
|
|
public interface IImageHashGenerator
|
|
{
|
|
string GetHashFromImage(ImageSource image);
|
|
}
|
|
public class ImageHashGenerator : IImageHashGenerator
|
|
{
|
|
public string GetHashFromImage(ImageSource imageSource)
|
|
{
|
|
if (!(imageSource is BitmapSource image))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
using (var outStream = new MemoryStream())
|
|
{
|
|
// PngBitmapEncoder enc2 = new PngBitmapEncoder();
|
|
// enc2.Frames.Add(BitmapFrame.Create(tt));
|
|
|
|
var enc = new JpegBitmapEncoder();
|
|
var bitmapFrame = BitmapFrame.Create(image);
|
|
bitmapFrame.Freeze();
|
|
enc.Frames.Add(bitmapFrame);
|
|
enc.Save(outStream);
|
|
var byteArray = outStream.GetBuffer();
|
|
using (var sha1 = new SHA1CryptoServiceProvider())
|
|
{
|
|
var hash = Convert.ToBase64String(sha1.ComputeHash(byteArray));
|
|
return hash;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|
|
} |