Skip to content

Instantly share code, notes, and snippets.

@veigr
Created February 4, 2015 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veigr/83d4f127e59950528f8b to your computer and use it in GitHub Desktop.
Save veigr/83d4f127e59950528f8b to your computer and use it in GitHub Desktop.
IWICBitmapSource to WriteableBitmap
ComPtr<IWICBitmapSource> bitmapSource = ReadFile(stream); // 読み込みは適当に
UINT width;
UINT height;
ThrowIfFailed(bitmapSource->GetSize(&width, &height));
auto bitmapBytes = ref new Array<byte>(width * height * 4); // 32bpp前提
ThrowIfFailed(
bitmapSource->CopyPixels(nullptr, width * 4, bitmapBytes->Length, bitmapBytes->Data)
);
auto bitmap = ref new WriteableBitmap(width, height);
ComPtr<IBufferByteAccess> bufferByteAccess;
ComPtr<IInspectable> inspectable(reinterpret_cast<IInspectable*>(bitmap->PixelBuffer));
ThrowIfFailed(inspectable.As(&bufferByteAccess));
byte* pixels;
ThrowIfFailed(bufferByteAccess->Buffer(&pixels));
for (int i = 0; i < bitmapBytes->Length; i++)
{
pixels[i] = bitmapBytes[i];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment