Domanda

I tried this code to get thumbnail images fo websites using Websites Screenshot DLL but the images are not displayed inside my itemscontrol and I get no errors.

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

and this is the code of my 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>

This is my sep class

public class sep
{
    public string title { get; set; }
    public System.Drawing.Image thumbnail { get; set; }
}

Can anyone please help me.

È stato utile?

Soluzione

Essentially you have been drawing nulls. The older style of bitmaps do not really travel that well into WPF. You can change this code...

   sep s = new sep();
    s.title = item.title;
    s.thumbnail = (System.Drawing.Image)GetThumbImage(item.urlsite);

to this...

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

This code runs an ad-hoc conversion on the Bitmap so that it can be used as a WPF ImageSource.

The 'sep' class is declared with an ImageSource like this...

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
}

In the longer run, you can consider refactoring the conversion into an IValueConverter.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top