我想用我保存到硬盤的文件填充矩形。我知道我必須使用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) };
}
對不起。我正在使用瀏覽器。基於你的迴應,我現在假設我可以使用CommonDialog打開它,類似於我保存它的方式。我會嘗試,並會回來更新這個線程。 –