Bitmap for UWP
画像の読み込みおよび保存
機能 > 画像の読み込みおよび保存

Bitmap では、C1Bitmap クラスの Load メソッドを使用して、C1Bitmap オブジェクト内に画像を読み込むことができます。Load および LoadAsync のオーバーロードメソッドを使用すると、画像を StorageFile、IInputStream、または System.IO.Stream から読み込むことができます。Bitmap では、画像のサイズ、解像度(DPI)、またはピクセル形式を決定するために使用できる画像メタデータを読み込むこともできます。さらに、いくつかの画像を C1Bitmap の同じインスタンスに1つずつ読み込みまたはインポートすることができます。

読み込みと同様に、C1Bitmap の画像は、StorageFile、IOutputStream、あるいは System.IO.Stream に保存することができます。ビットマップは、コンテナ形式を引数として受け入れる C1Bitmap クラスの一般的な Save メソッドを提供します。また、サポートされている各コンテナ形式に対して個別の SaveAs メソッドを提供します。

ここでは、ファイルから任意の画像を読み込む方法について説明します。ストリームから画像を読み込む方法については「クイックスタート」を参照してください。

以下の手順は、ファイルから任意の画像を読み込む方法を示しています。 このコードは、LoadAsync メソッドを使用して StorageFile から画像を読み込みます。

  1. 以下のクラスオブジェクトを作成して初期化します。
    Dim btmp As New C1Bitmap()
    Dim sb As SoftwareBitmap
    Dim sbs As SoftwareBitmapSource
    
    C1Bitmap btmp = new C1Bitmap();
    SoftwareBitmap sb;
    SoftwareBitmapSource sbs;
    
  2. StorageFile から画像を読み込むために、以下のコードを追加します。
    Dim picker = New FileOpenPicker()
    
    picker.FileTypeFilter.Add(".ico")
    picker.FileTypeFilter.Add(".bmp")
    picker.FileTypeFilter.Add(".gif")
    picker.FileTypeFilter.Add(".png")
    picker.FileTypeFilter.Add(".jpg")
    picker.FileTypeFilter.Add(".jpeg")
    picker.FileTypeFilter.Add(".jxr")
    picker.FileTypeFilter.Add(".tif")
    picker.FileTypeFilter.Add(".tiff")
    
    Dim file As StorageFile = Await picker.PickSingleFileAsync()
    
    If file IsNot Nothing Then
        Await btmp.LoadAsync(file, New FormatConverter(PixelFormat.Format32bppPBGRA))
        Await UpdateImageSource()
    End If
    
    var picker = new FileOpenPicker();
    
    picker.FileTypeFilter.Add(".ico");
    picker.FileTypeFilter.Add(".bmp");
    picker.FileTypeFilter.Add(".gif");
    picker.FileTypeFilter.Add(".png");
    picker.FileTypeFilter.Add(".jpg");
    picker.FileTypeFilter.Add(".jpeg");
    picker.FileTypeFilter.Add(".jxr");
    picker.FileTypeFilter.Add(".tif");
    picker.FileTypeFilter.Add(".tiff");
    
    StorageFile file = await picker.PickSingleFileAsync();
    
    if (file != null)
    {
            await btmp.LoadAsync(file, new FormatConverter(PixelFormat.Format32bppPBGRA));
            await UpdateImageSource();
    }
    
    メモ: イベント上で非同期メソッドを呼び出すため、async キーワードと await オペレータを使用しています。
  3. UpdateImageSource という名前のメソッドを作成します。これは、SoftwareBitmap を作成し、それをソースの SoftwareBitmap としてイメージソースに割り当てます。
    Private Async Function UpdateImageSource() As Task
        sb = btmp.ToSoftwareBitmap()
        sbs = New SoftwareBitmapSource()
        Await sbs.SetBitmapAsync(sb)
        img.Source = sbs
    End Function
    
    async Task UpdateImageSource()
    {
        sb = btmp.ToSoftwareBitmap();
        sbs = new SoftwareBitmapSource();
        await sbs.SetBitmapAsync(sb);
        img.Source = sbs;
    }
    
  4. 次のコードを使用して、読み込まれた任意の画像を保存します。ここでは、SaveAsPngAsync メソッドを使用して、画像を PNG 形式の StorageFile に保存します。
    Dim picker = New FileSavePicker()
    picker.FileTypeChoices.Add("png", New List(Of String)() From {
        ".png"
    })
    picker.DefaultFileExtension = ".png"
    
    Dim file As StorageFile = Await picker.PickSaveFileAsync()
    
    If file IsNot Nothing Then
        Await btmp.SaveAsPngAsync(file, Nothing)
        Dim md As New MessageDialog("ファイルが保存されました。")
    
        Await md.ShowAsync()
    End If
    
    btmp.Dispose()
    
    var picker = new FileSavePicker();
    picker.FileTypeChoices.Add("png", new List<string> { ".png" });
    picker.DefaultFileExtension = ".png";
    
    StorageFile file = await picker.PickSaveFileAsync();
    
    if (file != null)
    {
        await btmp.SaveAsPngAsync(file, null);
        MessageDialog md = new MessageDialog("ファイルが保存されました。");
        await md.ShowAsync();
    }
    btmp.Dispose();
    
関連トピック