2012-03-11 59 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

相關問題