获取网站的缩略图

本文关键字:略图 网站 获取 | 更新日期: 2023-09-27 18:01:07

我尝试使用网站截图DLL获取网站的缩略图但图像没有显示在我的项目控制和我没有得到任何错误。

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 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; }
}

有人能帮我吗?

获取网站的缩略图

从本质上讲,您一直在绘制null。旧样式的位图在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;
        }

此代码在位图上运行一个特殊转换,以便将其用作WPF ImageSource。

"sep"类是用这样的ImageSource声明的。。。

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。