我在测试应用程序中乱搞后找到了一个词云NuGet包我设法让世界云工作,但我使用的是imgWordcloud.Source = bi。当尝试使用属性绑定时,图像不会出现在视图中,但在单步执行程序时,位图图像存在于WordcloudImage属性中。
C#代码:
private ImageSource _wordcloudImage;
public ImageSource WordcloudImage
{
get { return _wordcloudImage; }
set
{
if (_wordcloudImage != value)
{
_wordcloudImage = value;
RaisePropertyChanged("WordcloudImage");
}
}
}
更多C#代码:
public void CreateWordcloud()
{
List<string> Data = new List<string>();
List<int> Data2 = new List<int>();
WordCloud.WordCloud wc = new WordCloud.WordCloud(200, 200, false, System.Drawing.Color.FromArgb(100, 168, 58, 117), 40, 1);
foreach (var item in WordcloudExportItems[0].snapshots[0].items.items)
{
Data.Add(item.title);
Data2.Add(2);
}
// ImageSource ...
System.Drawing.Image imgWinForms = wc.Draw(Data, Data2);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
MemoryStream ms = new MemoryStream();
// Save to a memory stream...
imgWinForms.Save(ms, ImageFormat.Png);
// Rewind the stream...
ms.Seek(0, SeekOrigin.Begin);
// Tell the WPF image to use this stream...
bi.StreamSource = ms;
var img = System.Drawing.Image.FromStream(ms);
bi.EndInit();
_wordcloudImage = bi;
}
WPF:
<ListView
x:Name="lvWordcloudExport"
Grid.Row="1"
ItemsSource="{Binding WordcloudExportItems}"
SelectedItem="{Binding SelectedWordcloudExport, Mode=TwoWay}"
IsSynchronizedWithCurrentItem="False"
Visibility="{Binding Path=IsVisibleWordcloudExport, Converter={StaticResource BoolToVisConverter} }"
>
<ListView.View>
<GridView>
<GridViewColumn x:Name="Wordcloud" Header="Data" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="whhiofw"></TextBlock>
<Image
Name="imgWordcloud"
Source="{Binding WordcloudImage}"
Height="400"
Width="200"
/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>