2011-06-20 43 views
2

我有一個表單,其中顯示一堆圖像。我希望能夠通過另一個可執行文件(如paint)修改此圖像,並在我退出時刷新圖像(例如,通過按鈕)。從WPF中的圖像標記中修改圖像

當我嘗試將圖像保存在繪圖中時,它會告訴我圖像被保護,這是由於圖像以某種方式鏈接到圖像。

有什麼辦法可以使這項工作?任何想法如何繞過這個問題?

謝謝。

編輯:這裏是strucure(heaviliy簡體)我用

 <ListView Style="{StaticResource BaseListView}" x:Name="LstMoulures" Grid.Column="0" Background="#00000000" SelectionMode="Single" IsTextSearchEnabled="False" ScrollViewer.VerticalScrollBarVisibility="Auto"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <Border BorderBrush="White" Margin="1,1,1,1" BorderThickness="2,2,2,2" CornerRadius="4"> 
         <StackPanel Orientation="Horizontal" Margin="5,0,0,5" Height="200"> 
          <Image x:Name="ImgPicture" Width="220" Height="195" Margin="2,5,2,2" GotFocus="ImgPicture_GotFocus" MouseLeftButtonDown="ImgPicture_MouseLeftButtonDown" MouseEnter="ImgPicture_MouseEnter" MouseLeave="ImgPicture_MouseLeave"> 
           <Image.Source> 
            <BitmapImage UriSource="{Binding DessinSource}" CacheOption="OnLoad" /> 
           </Image.Source> 
          </Image> 
         </StackPanel> 
        </Border> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 

回答

0
<Image> 
    <Image.Source> 
     <BitmapImage UriSource="test.jpg" CacheOption="OnLoad" /> 
    </Image.Source> 
</Image> 

你也可以讀取該文件,並將其改寫爲MemoryStream對象:

MemoryStream ms = new MemoryStream(); 
BitmapImage bi = new BitmapImage(); 

byte[] bytArray = File.ReadAllBytes(@"test.jpg"); 
ms.Write(bytArray, 0, bytArray.Length); 
ms.Position = 0; 
bi.BeginInit(); 
bi.StreamSource = ms; 
bi.EndInit(); 
image.Source = bi; 

對於監控圖像改變和刷新你可以使用的圖像FileSystemWatcher

+0

@navid:什麼是'CacheOption'的意義?如果你使用CacheOption,你的回答並沒有真正回答主要問題 – Sung

+0

看[這裏](http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapcacheoption.aspx) –

+0

=「OnLoad」'圖像數據的所有請求都從內存存儲中填充。 –