トリミング機能の実装
範囲指定ができたらいよいよ次はトリミングの実装です。MainPage.xaml.vb(MainPage.xaml.cs)ではボタンがクリックされたらViewModelを経由してPictureModelのTrimメソッドを呼び出すようにコーディングされています。すべての処理はPictureModelクラスにあるので、PictureModelクラスの内容を見てみましょう。
Public Sub Trim(rect As SelectionSize)
Dim sizeX As Integer = (Me.ActureSize.Width - (rect.Right + rect.Left))
Dim sizeY As Integer = (Me.ActureSize.Height - (rect.Bottom + rect.Top))
Dim crop As New C1.Phone.Imaging.C1Bitmap(sizeX, sizeY)
Dim posX As Integer = (rect.Left - rect.ActureSize.Left)
Dim posY As Integer = (rect.Top - rect.ActureSize.Top)
crop.BeginUpdate()
For row As Integer = 0 To sizeY - 1
For col As Integer = 0 To sizeX - 1
crop.SetPixel(col,
row,
Me.Item.GetPixel(col + posX,
row + posY))
Next
Next
crop.EndUpdate()
Me.Item = crop
End Sub
public void Trim(SelectionSize rect)
{
int sizeX = (int)(this.ActureSize.Width - (rect.Right + rect.Left));
int sizeY = (int)(this.ActureSize.Height - (rect.Bottom + rect.Top));
C1.Phone.Imaging.C1Bitmap crop = new C1.Phone.Imaging.C1Bitmap(sizeX, sizeY);
int posX = (int)(rect.Left - rect.ActureSize.Left);
int posY = (int)(rect.Top - rect.ActureSize.Top);
crop.BeginUpdate();
for (int row = 0; row <= sizeY - 1; row++)
{
for (int col = 0; col <= sizeX - 1; col++)
{
crop.SetPixel(col,
row,
this.Item.GetPixel(col + posX,
row + posY));
}
}
crop.EndUpdate();
this.Item = crop;
}
残念ながらCopyコマンドのようにトリミング専用のコマンドは存在しませんが、ピクセル単位に取得と設定ができるGetPixelとSetPixelがあるので、この2つを使って必要なピクセルを1つずつ転記します。
後はトリミング後のデータをSNSに投稿したりSkyDriveにアップしたりする機能をつければ、十分実用的なアプリに仕上がるでしょう。
まとめ
今回は、C1DockPanel、C1Bitmap、C1ProgressBar、C1GestureListenerの4つを組み合わせて画像処理アプリを作成してみました。1つ1つ使うのも良いですが、このような形で組み合わせて使うことでさらに作りやすくなります。ぜひ、単独で利用するだけなく、複数のコントロールを組み合わせ、効率的な開発を実現してみてください。

