2013-05-12 63 views
0

我正在做一个自定义窗体的滑块,我发现一个smaple代码,问题是它使用矩形与股票的颜色,我需要一个图像,而不是。用纹理图像更改纹理颜色

这里就是纹理绘制代码的一部分:

// Draw the Thumb Object 
    using (SolidBrush brush = new SolidBrush(Color.Black)) 
    { 
     if (ThumbFocused) 
      brush.Color = Color.Green; 
     if (ThumbDragging) 
      brush.Color = Color.Gray; 
     e.Graphics.FillRectangle(brush, this.Thumb); 
    } 

    //Draw Focus 
    if (this.Focused && this.ShowFocusCues) 
     ControlPaint.DrawFocusRectangle(e.Graphics, this.ClientRectangle); 

} 

我想用一张图片来代替颜色在此代码:

brush = new SolidBrush(Color.Black); 

例:brush = new SolidBrush(PICTURE);

并在下一个。

我该怎么办?

回答

0

您需要使用类似以下内容:

Rectangle exampleRectangle = new Rectangle(); 
exampleRectangle.Width = 75; 
exampleRectangle.Height = 75; 

// Create an ImageBrush and use it to 
// paint the rectangle. 
ImageBrush myBrush = new ImageBrush(); 
myBrush.ImageSource = 
    new BitmapImage(new Uri(@"sampleImages\pinkcherries.jpg", UriKind.Relative)); 

exampleRectangle.Fill = myBrush; 

参考来源:http://msdn.microsoft.com/en-us/library/aa970904.aspx

+0

我觉得我得到它,但你是怎么实现的代码示例我上面给了? – Joscplan 2013-05-12 20:17:11

+0

那么,根据你的问题,你可以更换纯色的brush.Color = Color.Green;与我的示例中使用您的纹理的位图图像。 – 2013-05-12 20:44:15