2011-08-08 21 views
0

我想用我保存到硬盤的文件填充矩形。我知道我必須使用ImageBrush,並且我認爲如果圖像是包含的資源,我知道該怎麼做。在這種情況下,文件被創建並設置在硬盤上,但是當我嘗試使用下面的代碼時,矩形會發生變化,但它會更改爲顯示背景顏色,而不是像我期望的那樣顯示圖像(幾乎就像圖像是無形)。如何使用ImageBrush使用硬盤中的文件填充矩形

using (dynamic CommonDialog = AutomationFactory.CreateObject("WIA.CommonDialog")) 
    { 
     dynamic imageFile = CommonDialog.ShowAcquireImage(); 
     if (imageFile != null) 
     { 
      string filePath = string.Format("d:\\{0}.jpg", Guid.NewGuid()); 
      imageFile.SaveFile(filePath); 

      rectangle2.Fill = new ImageBrush() 
      { 
       ImageSource = new BitmapImage(new Uri(filePath, UriKind.Absolute)) 
      }; 
     } 
    } 

更新:我能夠得到這個用替換如果裏面的代碼塊,然後工作如下:

  { 
       string filePath = string.Format("d:\\{0}.jpg", Guid.NewGuid()); 
       imageFile.SaveFile(filePath); 

       BitmapImage bitmapBase = new BitmapImage(); 
       dynamic fileData = imageFile.FileData; 
       byte[] imageData = fileData.BinaryData; 
       MemoryStream ms = new MemoryStream(imageData); 
       bitmapBase.SetSource(ms); 
       WriteableBitmap writableBitmap = new WriteableBitmap(bitmapBase); 

       rectangle2.Fill = new ImageBrush() { ImageSource = (writableBitmap) }; 

      } 

回答

1

無法訪問圖像,或任何文件,從本地驅動器(隔離存儲或文件打開對話框中的流除外)。這些都是安全措施。

正如你沒有提到離開瀏覽器,我認爲這只是一個網絡/客戶端應用程序。

+0

對不起。我正在使用瀏覽器。基於你的迴應,我現在假設我可以使用CommonDialog打開它,類似於我保存它的方式。我會嘗試,並會回來更新這個線程。 –

1

使用Silverlight 4時,您可以創建一個瀏覽器外應用程序,它可以訪問(部分)本地磁盤。

See here how