gpt4 book ai didi

wpf - 来自 Base64 的图像不会显示在 WPF 数据网格行中

转载 作者:行者123 更新时间:2023-12-02 23:55:10 26 4
gpt4 key购买 nike

我正在尝试使用 Powershell 在 WPF 数据网格的每一行的开头添加一个图标。我不想存储图像,所以我使用 base64 进行转换。

这是显示带有图标 + 进程名称的数据网格的代码:

Add-Type -AssemblyName PresentationFramework, System.Windows.Forms, System.Drawing
[xml]$xaml=@"
<Window
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"
Title="MainWindow" Height="410" Width="670" WindowStartupLocation="CenterScreen">
<Grid>
<DataGrid x:Name="dgResults" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Icon" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image x:Name="icon" Source="{Binding Icon}" Width="24" Height="24" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Process}" Header="Process" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
"@

$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load($reader)

$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'x:Name')]]") | ForEach-Object{
Set-Variable -Name ($_.Name) -Value $Window.FindName($_.Name)
}

#start conversion
$strBase64 = [convert]::ToBase64String([System.Drawing.Icon]::ExtractAssociatedIcon("C:\windows\System32\cmd.exe"))
$bitmap = New-Object System.Windows.Media.Imaging.BitmapImage
$bitmap.BeginInit()
$bitmap.StreamSource = [System.IO.MemoryStream][System.Convert]::FromBase64String($strBase64)
$bitmap.EndInit()
$bitmap.Freeze()
#end conversion

$arrayItems = @()

Get-Process | Select-Object Name -First 5 | ForEach-Object{
$itemObject = New-Object System.Object
$itemObject | Add-Member -Type NoteProperty -Name "Icon" -Value $bitmap
$itemObject | Add-Member -Type NoteProperty -Name "Process" -Value $_.Name
$arrayItems += $itemObject
}

$dgResults.ItemsSource = $arrayItems

#Display Form
$Window.ShowDialog() | Out-Null

Base64 字符串的转换在这里完成:
#start conversion
$strBase64 = [convert]::ToBase64String([System.Drawing.Icon]::ExtractAssociatedIcon("C:\windows\System32\cmd.exe"))
$bitmap = New-Object System.Windows.Media.Imaging.BitmapImage
$bitmap.BeginInit()
$bitmap.StreamSource = [System.IO.MemoryStream][System.Convert]::FromBase64String($strBase64)
$bitmap.EndInit()
$bitmap.Freeze()
#end conversion
$bitmap对象返回 System.Windows.Media.Imaging.BitmapImage
当我用 $Window.Icon = $bitmap 更改 Window Logo 时,没问题,图标设置正确,但是当我存储它并使用 ItemsSource 调用数据时来自 $arrayItems (包含图标+进程名称),图像不会出现在每行的开头。

你有什么主意吗?

谢谢。

使用基于@ArcSet 答案的函数更新:
Add-Type -AssemblyName PresentationFramework, System.Windows.Forms, System.Drawing, System.IO
[xml]$xaml=@"
<Window
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"
Title="MainWindow" Height="410" Width="670" WindowStartupLocation="CenterScreen">
<Grid>
<DataGrid x:Name="dgResults" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Icon" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image x:Name="icon" Source="{Binding Icon}" Width="24" Height="24" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Process}" Header="Process" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
"@

$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load($reader)

$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'x:Name')]]") | ForEach-Object{
Set-Variable -Name ($_.Name) -Value $Window.FindName($_.Name)
}

function ConvertTo-Icon{
Param(
[Parameter(Mandatory=$true)][object]$Icon
)

$bmp = $Icon.ToBitmap()
$stream = New-Object System.IO.MemoryStream
$bmp.Save($stream, [System.Drawing.Imaging.ImageFormat]::Png)
$imageSource = [System.Windows.Media.Imaging.BitmapFrame]::Create($stream)

# Set source here. Take note in the XAML as to where the variable name was taken.
return $imageSource
}

$arrayItems = @()

Get-Process | Select-Object Name -First 5 | ForEach-Object{
$itemObject = New-Object System.Object
$itemObject | Add-Member -Type NoteProperty -Name "Icon" -Value (ConvertTo-Icon -Icon ([System.Drawing.Icon]::ExtractAssociatedIcon("C:\windows\System32\cmd.exe")))
$itemObject | Add-Member -Type NoteProperty -Name "Process" -Value $_.Name
$arrayItems += $itemObject
}

$dgResults.ItemsSource = $arrayItems

#Display Form
$Window.ShowDialog() | Out-Null

最佳答案

所以这个问题与 WPF 如何不真正支持图标格式有关。您需要将该格式转换为 WPF Image 标签可读的格式。请改用下面的代码。你目前正在使用的

Add-Type -AssemblyName PresentationFramework, System.Windows.Forms, System.Drawing
[xml]$xaml=@"
<Window
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"
Title="MainWindow" Height="410" Width="670" WindowStartupLocation="CenterScreen">
<Grid>
<DataGrid x:Name="dgResults" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Icon" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image x:Name="icon" Source="{Binding Icon}" Width="24" Height="24" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Process}" Header="Process" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
"@

$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load($reader)

$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'x:Name')]]") | ForEach-Object{
Set-Variable -Name ($_.Name) -Value $Window.FindName($_.Name)
}

#start conversion
$icon = [System.Drawing.Icon]::ExtractAssociatedIcon("C:\windows\System32\cmd.exe")
$bmp = $icon.ToBitmap()
$stream = new-object System.IO.MemoryStream
$bmp.Save($stream, [System.Drawing.Imaging.ImageFormat]::Png)
$imageSource = [System.Windows.Media.Imaging.BitmapFrame]::Create($stream);
#end conversion

$arrayItems = @()

Get-Process | Select-Object Name -First 5 | ForEach-Object{
$itemObject = New-Object System.Object
$itemObject | Add-Member -Type NoteProperty -Name "Icon" -Value $imageSource
$itemObject | Add-Member -Type NoteProperty -Name "Process" -Value $_.Name
$arrayItems += $itemObject
}

$dgResults.ItemsSource = $arrayItems
#Display Form
$Window.ShowDialog() | Out-Null

不同之处在于转换为PNG...
$icon = [System.Drawing.Icon]::ExtractAssociatedIcon("C:\windows\System32\cmd.exe")
$bmp = $icon.ToBitmap()
$stream = new-object System.IO.MemoryStream
$bmp.Save($stream, [System.Drawing.Imaging.ImageFormat]::Png)
$imageSource = [System.Windows.Media.Imaging.BitmapFrame]::Create($stream);

关于wpf - 来自 Base64 的图像不会显示在 WPF 数据网格行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46684809/

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