我有一个文本框,其图像链接为背景。
我想使用此图片的base64版本。
我使用了一个非常简单的windows -form-来确保实现base64的代码正常工作,确实如此。
我遇到问题的部分是用xpl以wpf样式gui链接它。
<TextBox Name="OutTBox" Grid.Column="0" Grid.ColumnSpan="7" Grid.Row="4" Grid.RowSpan="6" Margin="20,5,20,20" Foreground="#FF8CFF8C" IsReadOnly="True" >
<TextBox.Background>
<ImageBrush ImageSource="C:\background.jpg" Stretch="UniformToFill"/>
</TextBox.Background>
</TextBox>
所以这是我用于windows窗体的base64语言:
Add-Type -AssemblyName PresentationFramework
# Setup the XAML
[xml]$script:xaml = '<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="base64 image demo" Height="350" Width="650" Background="Black">
<Grid>
<Image Name="thisistheimage" Stretch="UniformToFill" />
</Grid>
</Window>'
# Create the form and set variables
$script:form = [Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader $xaml))
$xaml.SelectNodes("//*[@Name]") | ForEach-Object { Set-Variable -Name ($_.Name) -Value $form.FindName($_.Name) -Scope Script }
# here's the base64 string
$base64 = "iVBORw0KGgoAAAANSUhEUgAAAEAAAAAuCAIAAAAX9YijAAAABmJLR0QA/wD/AP+gvaeTAAAGCElEQVRogc1Za0xTZxh+TukVWlrB6rjUzrmqM855Q0CGCBOdJiMSZ1zMYvizZaLGchFUrqVFuTjQLWrcjMnc3Mh+LFF3cbDItsyoGOaMJg4cdEZ6k4tlQFvobT8gSMopPd+hEJ9f55zved4+T7/3O985LeX1ekEHrxf79/945swd2tEXBxx/AxSF06e3HT2aPJtuWMBvgFFUVqYVFb3QGQIEAKDTBT+DSMTduvVV5vyMjCU8Hr3VwAEA6HRpxcUbmH9eQGRnx126tEMk4jIhy2TChoZ3s7JW0o4yCgBAq00tKQlahsRExZw5whUr5jMhr1z5kkjETUl5mXaUaQAAFRWppaUpzPm0kEj4YjHfZnMCcLu90dGSqflRUeKhoREAbrdHKhWEhfF8CAQBAGg0G6eTQSjkdn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UFRFYAADI30dMPlx9AMCNgOEEur9iJPTzMCcrBHfB81Phm+C9Aq8LVICHP3buAdjb4bFBoAQApwX2dqZCPy000f0oQqIDujdVVrJzD2CwBc7esWNnLwZbmAqn+0IzDpNOZywuDlY15ghOAJNWaywpIZLId0O0GOI1kO+eiiNeA9HiqThBeKExabXG0lIiiXAR5LvBV4ArhVCF7q/paRGZcDyCqx/SZAzchoPuXWO6AUwVFcayMlJVeDKEKoheA4ARA3hyAM+XGMUdu8KfD/FqAPC6EJ5MH2Bav0qYNBoj2xe6JQ0QxzElD95B23v0Q+zXwHTcBxEsW8hw+LC5ujq4VgAMPwEAgYJAwmYGZsh99yU8SMWDNHR/Q6AiDmAoLJwJ9wAsnwNewAPLOQIVWQt1FRRYamvJfDGG2+Z7wAQEAboOHbKcOEFiaTbAtIWC6z58w9hzG0MIlAj38wcdoxnoys+3fOz7i/F0wOFj4LbvxbAV8AwDAEeAvqs0EloE3si68vIsdXVsbM4KAsxAV26upb5+dqyww1Rr4MV3D+B/cukdthlrA6MAAAAASUVORK5CYII="
# Create a streaming image by streaming the base64 string to a bitmap streamsource
$bitmap = New-Object System.Windows.Media.Imaging.BitmapImage
$bitmap.BeginInit()
$bitmap.StreamSource = [System.IO.MemoryStream][System.Convert]::FromBase64String($base64)
$bitmap.EndInit()
# Freeze() prevents memory leaks.
$bitmap.Freeze()
# Set source here. Take note in the XAML as to where the variable name was taken.
$thisistheimage.source = $bitmap
# Show form
$null = $form.ShowDialog()
我会给你我的实际图像,但它超过5k行。加载只需要几分之一秒。 要清楚,base64将加载到Xaml中。我从那些网格坐标中删除了文本框,并将其替换为base64并且加载正常。似乎无法将两者结合起来。
谢谢!
答案 0 :(得分:0)
我从未使用过powershell,但它应该像这样简单:
$brush = New-Object System.Windows.Media.ImageBrush
$brush.ImageSource = $bitmap
$thetextbox.Background = $brush