2012-03-11 90 views
1

当我运行此代码时,直到最大化应用程序时,我才会看到一个黑色屏幕?另外,我不认为它挑选图像文件。在Visual Studio中,我创建了一个新文件夹并将图像添加到此文件夹中。以编程方式将图像附加到Ellipse

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); 
} 

回答

3

是否有任何特别的原因,您正在使用TextureBrush

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

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

谢谢,我是继指南:http://msdn.microsoft.com/en-us/library/cwka53ef%28v=vs.71%29.aspx – Michael 2012-03-11 19:28:07

+0

我似乎有问题加载图像?请看我编辑的OP。 – Michael 2012-03-12 12:22:32

+0

我认为Fill()会使应用程序显示为黑色...... – Michael 2012-03-12 12:31:39

相关问题