c# - 以编程方式将图像附加到椭圆

标签 c# wpf

当我运行此代码时,我会出现黑屏,直到我最大化应用程序?另外,我认为它也没有获取图像文件。在 Visual Studio 中,我创建了一个新文件夹并将图像添加到该文件夹​​中。

public MainWindow()
{
    InitializeComponent();

    Canvas canvas = new Canvas();
    canvas.Width = 300;
    canvas.Height = 300;
    canvas1.Children.Add(canvas);

    Ellipse hand = new Ellipse();
    hand.Height = 30;
    hand.Width = 30;
    /*
    BrushConverter bc = new BrushConverter();
    Brush brush = (Brush)bc.ConvertFrom("Red");
    hand.Fill = new SolidColorBrush(Colors.Red);
    */
    ImageBrush myBrush = new ImageBrush();
    myBrush.ImageSource =
        new BitmapImage(new Uri(@"Images/Hand.png", UriKind.Relative));
    hand.Fill = myBrush;

    Canvas.SetLeft(hand, 100);
    Canvas.SetTop(hand, 100);
    canvas.Children.Add(hand);
}

最佳答案

您使用 TextureBrush 有什么特殊原因吗?

不太确定,但也许您应该使用 ImageBrush 来代替。

ImageBrush myBrush = new ImageBrush();
myBrush.ImageSource = 
    new BitmapImage(new Uri("pack://application:,,,/Images/image.jpg"));
myEllipse.Fill = myBrush;

关于c# - 以编程方式将图像附加到椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9658051/

相关文章:

c# - HttpWebRequest.Headers[HttpRequestHeader.Referer] 因错误而失败

C# WPF 列表框聊天

c# - 使用运行时生成的控件保存控件窗口并重新加载为以前的状态

c# - 所有不继承自 System.ValueType 的对象都是引用类型吗?

c# - 单击图像并根据单击的位置显示消息框

WPF WebBrowser 控件 - 没有 LinkClicked 事件

c# - OnPropertyChange不会更新组合框

c# - 默认文件名 SaveFileDialog

c# - IActionResult 误解

c# - C# 中语法正确的正常运行时间