Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用纹理图像更改纹理颜色_C#_Textures - Fatal编程技术网

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;我的示例中有一个使用纹理的位图图像。