我尝试使用网站截图DLL获取此代码来获取网站的缩略图 但图像不会显示在我的itemscontrol中,我没有错误。
public void ImagesActivities()
{
//sep s = new sep();
var images = new ObservableCollection<sep>();
var wcf = new ServiceReferenceSites.SiteEnPlusServiceClient();
foreach (var item in wcf.GetAll())
{
sep s = new sep();
s.title = item.title;
s.thumbnail = (System.Drawing.Image)GetThumbImage(item.urlsite);
images.Add(s);
}
_activityList.ItemsSource = images;
}
private Bitmap GetThumbImage(string s)
{
WebsitesScreenshot.WebsitesScreenshot _Obj;
_Obj = new WebsitesScreenshot.WebsitesScreenshot();
WebsitesScreenshot.WebsitesScreenshot.Result _Result;
_Result = _Obj.CaptureWebpage(s);
if (_Result == WebsitesScreenshot.
WebsitesScreenshot.Result.Captured)
{
_Obj.ImageWidth = 200;
_Obj.ImageHeight = 100;
_Obj.ImageFormat = WebsitesScreenshot.
WebsitesScreenshot.ImageFormats.PNG;
return _Obj.GetImage();
}
else
return null;
}
这是我的itemscontrol的代码:
<ItemsControl x:Name="_activityList" HorizontalAlignment="Right" Margin="0,10,10,10" Width="760">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="5" IsItemsHost="True"/>
<!--<StackPanel Orientation="Horizontal" IsItemsHost="True"/>-->
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Margin="10,20,0,0" BorderThickness="0" Height="100" Width="200">
<Button.Background>
<ImageBrush ImageSource="{Binding thumbnail}" />
</Button.Background>
</Button>
<TextBlock Grid.Row="1" x:Name="nom" Margin="10,0,0,0" TextAlignment="Center" Text="{Binding title}" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这是我的sep类
public class sep
{
public string title { get; set; }
public System.Drawing.Image thumbnail { get; set; }
}
任何人都可以帮助我。
答案 0 :(得分:1)
基本上你一直在绘制空值。较旧的位图风格并没有真正进入WPF。您可以更改此代码......
sep s = new sep();
s.title = item.title;
s.thumbnail = (System.Drawing.Image)GetThumbImage(item.urlsite);
到此......
Sep sep = new Sep();
sep.Title = "title";
var bmp = GetThumbImage("xxx");
using (MemoryStream memory = new MemoryStream())
{
bmp.Save(memory, ImageFormat.Png);
memory.Position = 0;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
sep.ThumbnailImage = bitmapImage;
}
此代码在Bitmap上运行ad-hoc转换,以便可以将其用作WPF ImageSource。
使用像这样的ImageSource声明'sep'类......
public class Sep : INotifyPropertyChanged
{
private string _title;
public string Title
{
[DebuggerStepThrough]
get { return _title; }
[DebuggerStepThrough]
set
{
if (value != _title)
{
_title = value;
OnPropertyChanged("Title");
}
}
}
private ImageSource _thumbnailImage;
public ImageSource ThumbnailImage
{
[DebuggerStepThrough]
get { return _thumbnailImage; }
[DebuggerStepThrough]
set
{
if (value != _thumbnailImage)
{
_thumbnailImage = value;
OnPropertyChanged("ThumbnailImage");
}
}
}
#region INotifyPropertyChanged Implementation
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string name)
{
var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null);
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
从长远来看,您可以考虑将转换重构为IValueConverter。