Imaging for UWP
画像のエクスポート
Imaging for UWP > Bitmap for UWP > C1Bitmap の使い方 > 画像のエクスポート

簡単なコードと汎用のボタンコントロールを使用して、トリミングした画像をエクスポートすることができます。汎用のボタンコントロールの XAML マークアップを以下に示します。

XAML マークアップ
コードのコピー
<Button Content="Export selection" Click="ExportImage" Grid.Column="1" Width="140" />

エクスポート機能の制御に使用するコードは、エクスポート対象になるトリミングした部分がない場合に、エクスポートオプションをブロックします。エクスポート対象になるトリミングした部分がある場合は、ファイルタイプとエクスポート先を選択できます。

C# コードの書き方

C#
コードのコピー
private async void ExportImage(object sender, RoutedEventArgs e)
{
if(selection.Width == 0 || selection.Height == 0)
{
MessageDialog md = new MessageDialog("Can't export, selection is empty");
md.ShowAsync();
return;
}
var picker = new FileSavePicker();
picker.FileTypeChoices.Add("png", new List<string>{".png"});
picker.DefaultFileExtension = ".png";

StorageFile file = await picker.PickSaveFileAsync();

if (file != null)
{
var saveStream = await file.OpenStreamForWriteAsync();
var crop = new C1Bitmap((int)selection.Width, (int)selection.Height);
crop.BeginUpdate();
for (int x = 0; x < selection.Width; ++x)
{
for (int y = 0; y < selection.Height; ++y)
{
crop.SetPixel(x, y, bitmap.GetPixel(x + (int)selection.X, y + (int)selection.Y));
}
}
関連トピック