首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以编程方式将图像附加到Ellipse

以编程方式将图像附加到Ellipse
EN

Stack Overflow用户
提问于 2012-03-12 03:13:34
回答 1查看 5.4K关注 0票数 2

当我运行这段代码时,我得到一个黑屏,直到我最大化应用程序?此外,我认为它也不会拾取图像文件。在Visual Studio中,我创建了一个新文件夹,并将图像添加到此文件夹中。

代码语言:javascript
复制
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);
}
EN

回答 1

Stack Overflow用户

发布于 2012-03-12 03:25:25

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

不是很确定,但也许你应该使用ImageBrush

代码语言:javascript
复制
ImageBrush myBrush = new ImageBrush();
myBrush.ImageSource = 
    new BitmapImage(new Uri("pack://application:,,,/Images/image.jpg"));
myEllipse.Fill = myBrush;
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9658051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档