gpt4 book ai didi

c# - 将 Icon 对象与 byte[] 相互转换会导致质量下降

转载 作者:太空宇宙 更新时间:2023-11-03 22:40:09 24 4
gpt4 key购买 nike

我正在制作一个 WPF 应用程序,其中 Image 有一个来自 Icon 对象的 ImageSource

为了将其保存在磁盘上,我将它们转换为 byte[]ImageSource 的 getter 转换为 byte[]回到 ImageSource

问题,这样做的时候,结果看起来完全错误,似乎只有少数颜色存活。

这是一个完整的 WPF 解决方案,您可以亲眼看看它看起来有多糟糕:

XAML

<Window x:Class="POCicon.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:POCicon"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:MyViewModel/>
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Source="{Binding WithConversion}" MaxHeight="32" MaxWidth="32" Stretch="Fill" ></Image>
<Image Grid.Column="1" Source="{Binding NoConversion}" MaxHeight="32" MaxWidth="32" Stretch="Fill" ></Image>
</Grid>
</Window>

C# View 模型

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace POCicon
{
public class MyViewModel
{
private System.Drawing.Icon Icon;
public MyViewModel()
{
Icon= System.Drawing.Icon.ExtractAssociatedIcon("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
}
public ImageSource WithConversion
{
get
{
byte[] byteArray;
using (MemoryStream serializer = new MemoryStream())
{
Icon.Save(serializer);
byteArray = serializer.ToArray();
}
using (MemoryStream deserializer = new MemoryStream(byteArray))
{
BitmapImage image = new BitmapImage();
image.BeginInit();
// image.CreateOptions = BitmapCreateOptions.PreservePixelFormat; //totally useless unfortunately
image.StreamSource = deserializer;
image.EndInit();
return image;
}
}
}

public ImageSource NoConversion
{
get
{
var imgSource = Imaging.CreateBitmapSourceFromHIcon(Icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
return imgSource;
}
}

}
}

有没有办法修复这个方法,或者换个方法得到想要的结果?要求是它在某个时候必须是一个 Icon 对象,并最终成为一个 byte[]

非常感谢

最佳答案

不使用 Icon.Save(),而是使用 Icon.ToBitmap() 从图标创建一个 System.Drawing.Bitmap ,并保存:

var icon = System.Drawing.Icon.ExtractAssociatedIcon(
@"C:\Program Files (x86)\JetBrains\dotPeek\v1.1\Bin\dotpeek32.exe");
var bitmap = icon.ToBitmap();

byte[] buffer;

using (var memoryStream = new MemoryStream())
{
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
buffer = memoryStream.ToArray();
}

或者,在转换为 BitmapSource 之后创建 byte[]:

var icon = System.Drawing.Icon.ExtractAssociatedIcon(
@"C:\Program Files (x86)\JetBrains\dotPeek\v1.1\Bin\dotpeek32.exe");
var bitmapSource = Imaging.CreateBitmapSourceFromHIcon(
icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapSource));

byte[] buffer;

using (var memoryStream = new MemoryStream())
{
encoder.Save(memoryStream);
buffer = memoryStream.ToArray();
}

关于c# - 将 Icon 对象与 byte[] 相互转换会导致质量下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52689421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com