[C#][Win8] RichEditBox 插入圖片

2012-12-26

上一篇文章 [C#][Win8] RichEditBox 選取文字改粗體 說道如何將選取文字改為粗體.

這一篇來實作一下如何插入圖片..

 

2012-12-26_171839


 

這篇案例,按下按鈕後,我會請User選一張圖,之後將它插入到游標的位置..

private async void btnInsertImage_Click(object sender, RoutedEventArgs e)
{
    //制式的選取檔案寫法
    FileOpenPicker fop = new FileOpenPicker();
    fop.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
    fop.FileTypeFilter.Clear();
    fop.FileTypeFilter.Add(".jpg");
 
    //讀取檔案
    StorageFile fileStream = await fop.PickSingleFileAsync();
    if (fileStream==null) return;
 
    //讀取成 IRandomAccessStream
    IRandomAccessStream stream = await fileStream.OpenAsync(FileAccessMode.Read);
 
    //插入
    richEditBox.Document.Selection.InsertImage(200, 200, 200, VerticalCharacterAlignment.Baseline, "插入圖片", stream);
    
    //Close the stream 
    stream.Dispose();
 
    //更新
    richEditBox.Document.ApplyDisplayUpdates();
}
      
 
 

說明就寫在註解裡了,基本上你copy paste就能用了..


 


參考文章:


http://social.msdn.microsoft.com/Forums/en-SG/winappswithcsharp/thread/387b8659-caf2-40f7-b997-ddaabe7be486

下載 Sample


當麻許的超技八 2014 | Donma Hsu Design.