C# 使用纹理图像更改纹理颜色
我正在做一个自定义窗体的滑块,我发现了一个简单的代码,问题是它使用带有原色的矩形,我需要一个图像 以下是绘制纹理的代码部分:C# 使用纹理图像更改纹理颜色,c#,textures,C#,Textures,我正在做一个自定义窗体的滑块,我发现了一个简单的代码,问题是它使用带有原色的矩形,我需要一个图像 以下是绘制纹理的代码部分: // Draw the Thumb Object using (SolidBrush brush = new SolidBrush(Color.Black)) { if (ThumbFocused) brush.Color = Color.Green; if (ThumbDragging)
// 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=新的SolidBrush(图片)代码>
还有下一个
我应该怎么做?您需要使用以下内容:
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;
参考源代码:我想我明白了,但是如何实现我上面给出的代码示例?好吧,根据您的问题,您可以替换纯色笔刷。Color=Color.Green;我的示例中有一个使用纹理的位图图像。