20150603卒研進捗LT (share)
- 19. スコープの範囲
18
public async Task<Bitmap> GetBitmap()
{
if (!_initialized)
{
throw new DeviceNotInitializedException();
}
Bitmap bitmap;
using (var ras = new InMemoryRandomAccessStream())
{
await _capture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreatePng(), ras);
ras.Seek(0);
bitmap = new Bitmap(ras.AsStream());
}
return bitmap;
}
- 20. スコープの範囲
19
public async Task<Bitmap> GetBitmap()
{
if (!_initialized)
{
throw new DeviceNotInitializedException();
}
Bitmap bitmap;
Stream stream;
using (var ras = new InMemoryRandomAccessStream())
{
await _capture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreatePng(), ras);
ras.Seek(0);
stream = ras.AsStream();
}
bitmap = new Bitmap(stream);
return bitmap;
}
- 26. スコープの範囲
25
public async Task<Bitmap> GetBitmap()
{
if (!_initialized)
{
throw new DeviceNotInitializedException();
}
Bitmap bitmap;
Stream stream;
using (var ras = new InMemoryRandomAccessStream())
{
await _capture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreatePng(), ras);
ras.Seek(0);
stream = ras.AsStream(); // ras のメモリアドレスを参照しているだけ
} // ここで Dispose() されて stream が null になる
bitmap = new Bitmap(stream); // null から Bitmap を生成できなくて死ぬ
return bitmap;
}
- 27. スコープの範囲
26
public async Task<Bitmap> GetBitmap()
{
if (!_initialized)
{
throw new DeviceNotInitializedException();
}
Bitmap bitmap;
using (var ras = new InMemoryRandomAccessStream())
{
await _capture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreatePng(), ras);
ras.Seek(0);
bitmap = new Bitmap(ras.AsStream()); // スコープ外の bitmap に Bitmap オブジェクトを作っておく
}
return bitmap; // ras は使っていないので Dispose() されてもOK
}