简体   繁体   English

Silverlight控件中的平铺图像

[英]Tile Images in Silverlight Controls

I'm just starting looking at Silverlight. 我刚刚开始关注Silverlight。 It just became apparent that brushes are not tile-able in Silverlight, unlike WPF. 很明显,与WPF不同,Silverlight中的画笔不能平铺。 I'd like to tile a graphic in a grid control. 我想在网格控件中平铺图形。 The image is basically tile able per grid cell. 图像基本上是每个网格单元格。 Can a use multiple brushes per control, or should I use lots of image controls, or? 每个控件可以使用多个画笔,还是应该使用大量的图像控件,或者?

I had to stick with SL 2 for this project, so unfortunately shaders were not an option. 我不得不坚持使用SL 2进行这个项目,所以不幸的是着色器不是一个选择。 My control had a fairly strict predetermined size, so I manually tiled the graphic onto a larger canvas. 我的控件有一个相当严格的预定尺寸,所以我手动将图形平铺到更大的画布上。

The best way I've found thus far is to steal the control from Microsofts 'JetPack' theme. 到目前为止,我发现的最好的方法是从微软的“JetPack”主题中窃取控制权。

It comes as part of the project template and does a pretty good job. 它作为项目模板的一部分,做得非常好。 Just set the SourceUri property and you're good to go. 只需设置SourceUri属性,就可以了。

Here's the source - 这是来源 -

public class TiledBackground : UserControl
{
    private Image tiledImage = new Image();
    private BitmapImage bitmap;
    private int lastWidth, lastHeight = 0;
    private WriteableBitmap sourceBitmap;

    public TiledBackground()
    {
        // create an image as the content of the control
        tiledImage.Stretch = Stretch.None;
        this.Content = tiledImage;

        // no sizechanged to override
        this.SizeChanged += new SizeChangedEventHandler(TiledBackground_SizeChanged);
    }

    void TiledBackground_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        UpdateTiledImage();
    }

    private void UpdateTiledImage()
    {
        if (sourceBitmap != null)
        {
            int width = (int)Math.Ceiling(this.ActualWidth);
            int height = (int)Math.Ceiling(this.ActualHeight);

            // only regenerate the image if the width/height has grown
            if (width < lastWidth && height < lastHeight) return;
            lastWidth = width;
            lastHeight = height;

            WriteableBitmap final = new WriteableBitmap(width, height);

            for (int x = 0; x < final.PixelWidth; x++)
            {
                for (int y = 0; y < final.PixelHeight; y++)
                {
                    int tiledX = (x % sourceBitmap.PixelWidth);
                    int tiledY = (y % sourceBitmap.PixelHeight);
                    final.Pixels[y * final.PixelWidth + x] = sourceBitmap.Pixels[tiledY * sourceBitmap.PixelWidth + tiledX];
                }
            }

            tiledImage.Source = final;
        }
    }

    #region SourceUri (DependencyProperty)

    /// <summary>
    /// A description of the property.
    /// </summary>
    public Uri SourceUri
    {
        get { return (Uri)GetValue(SourceUriProperty); }
        set { SetValue(SourceUriProperty, value); }
    }
    public static readonly DependencyProperty SourceUriProperty =
        DependencyProperty.Register("SourceUri", typeof(Uri), typeof(TiledBackground),
        new PropertyMetadata(null, new PropertyChangedCallback(OnSourceUriChanged)));

    private static void OnSourceUriChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((TiledBackground)d).OnSourceUriChanged(e);
    }

    protected virtual void OnSourceUriChanged(DependencyPropertyChangedEventArgs e)
    {
        bitmap = new BitmapImage(e.NewValue as Uri);
        bitmap.CreateOptions = BitmapCreateOptions.None;
        bitmap.ImageOpened += new EventHandler<RoutedEventArgs>(bitmap_ImageOpened);
    }

    void bitmap_ImageOpened(object sender, RoutedEventArgs e)
    {
        sourceBitmap = new WriteableBitmap(bitmap);
        UpdateTiledImage();
    }

    #endregion
}

HTH. HTH。

If the desired tile is a basic geometric pattern, another option is to get creative with repeating GradientBrushes. 如果所需的图块是基本几何图案,则另一个选项是使用重复的GradientBrushes获得创意。

Horizontal 1px stripes... 水平1px条纹......

<LinearGradientBrush EndPoint="0,16" StartPoint="0,0" MappingMode="Absolute" SpreadMethod="Repeat">
    <GradientStop Color="Black" Offset="0"/>
    <GradientStop Color="Black" Offset="0.062"/>
    <GradientStop Offset="0.0625"/>
</LinearGradientBrush>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM