2013-05-12 62 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