修复 保存图像会降低分辨率的问题

This commit is contained in:
Hakoyu 2023-11-01 14:44:44 +08:00
parent a91d7e2835
commit 4e9ed4630c
2 changed files with 11 additions and 7 deletions

View File

@ -79,14 +79,21 @@ public static class Extensions
/// </summary>
/// <param name="image">图片资源</param>
/// <param name="path">路径</param>
public static void SaveToPng(this BitmapSource image, string path)
public static void SaveToPng(this BitmapImage image, string path)
{
if (image is null)
return;
if (path.EndsWith(".png") is false)
path += ".png";
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
var stream = image.StreamSource;
// 保存位置
var position = stream.Position;
// 必须要重置位置, 否则EndInit将出错
stream.Seek(0, SeekOrigin.Begin);
encoder.Frames.Add(BitmapFrame.Create(image.StreamSource));
// 恢复位置
stream.Seek(position, SeekOrigin.Begin);
using var fs = new FileStream(path, FileMode.Create);
encoder.Save(fs);
}

View File

@ -53,11 +53,8 @@ public static class Utils
bitmapImage.BeginInit();
try
{
var ms = new MemoryStream();
using var sr = new StreamReader(imagePath);
sr.BaseStream.CopyTo(ms);
bitmapImage.StreamSource = ms;
bitmapImage.DecodePixelWidth = DecodePixelWidth;
var bytes = File.ReadAllBytes(imagePath);
bitmapImage.StreamSource = new MemoryStream(bytes);
}
finally
{