📄

C#版DxLibでResourceから画像・音声を読み込む

2025/02/12に公開

画像読み込み

static int ToHandle(Image image)
{
    using MemoryStream ms = new();
    image.Save(ms, ImageFormat.Png);
    var buff = ms.ToArray();
    var ptr = GCHandle.Alloc(buff, GCHandleType.Pinned).AddrOfPinnedObject();
    return DX.CreateGraphFromMem(ptr, buff.Length);
}

音声読み込み

static int ToHandle(UnmanagedMemoryStream sound)
{
    using MemoryStream ms = new();
    sound.CopyTo(ms);
    var buff = ms.ToArray();
    var ptr = GCHandle.Alloc(buff, GCHandleType.Pinned).AddrOfPinnedObject();
    return DX.LoadSoundMemByMemImage(ptr, (ulong) buff.Length);
}

Discussion