Imaging Library for Silverlight
元に戻す/やり直しの履歴

誰でもミスはします。どのようなアプリケーションでも、間違いを元に戻す(さらにやり直す)ことができれば、最初からやり直す手間が省けるのでたいへん便利です。このサンプルでは、画像が変更されるたびに画像のコピーを3つまで保存するという簡単な方法で、Undo/Redo を実現します。変更には、トリミング、サイズ変更、ワープがあります。ここでは、変更履歴を前後に移動すると共に、ユーザーがさらに変更を追加できるようにするテクニックを示します。実際のコードは、C1Bitmap または Silverlight 5 の拡張機能とは無関係なため、任意のコントロールを元に戻す/やり直すために使用できます。

C#
コードのコピー
List<C1Bitmap> undoBitmaps = new List<C1Bitmap>();
List<C1Bitmap> redoBitmaps = new List<C1Bitmap>();
private void btnUndo_Click(object sender, RoutedEventArgs e)
{
    if (undoBitmaps.Count > 1)
    {
        bitmap = new C1Bitmap(undoBitmaps.ElementAt(undoBitmaps.Count - 2));
        redoBitmaps.Add(new C1Bitmap(undoBitmaps.ElementAt(undoBitmaps.Count - 1)));
        undoBitmaps.RemoveAt(undoBitmaps.Count - 1);
        UpdateImage(false);
    }
    UpdateEditButtons();  
}
private void btnRedo_Click(object sender, RoutedEventArgs e)
{
    if (redoBitmaps.Count > 0)
    {
        bitmap = new C1Bitmap(redoBitmaps.ElementAt(redoBitmaps.Count - 1));
        undoBitmaps.Add(new C1Bitmap(redoBitmaps.ElementAt(redoBitmaps.Count - 1)));
        redoBitmaps.RemoveAt(redoBitmaps.Count - 1);
        UpdateImage(false);
    }
    UpdateEditButtons();
}
      
void UpdateHistory()
{
    //現在のビットマップをメモリに追加します
    undoBitmaps.Add(new C1Bitmap(bitmap));
    redoBitmaps.Clear();
          
    //元に戻す/やり直しの履歴として C1Bitmap に加えられた変更を3つまで保持できるように制限します
    if (undoBitmaps.Count > 4)
        undoBitmaps.RemoveAt(0);
    UpdateEditButtons();
}

 

 


Copyright c GrapeCity inc. All rights reserved.