0

我在 .Net Core 中创建了一个 Web 服务。此服务返回像素格式为 8pp 索引的图像 png:

using (Bitmap bmp = new Bitmap(img.width, img.height, PixelFormat.Format8bppIndexed))
{
    using (MemoryStream ms = new MemoryStream())
    {
        bmp.Save(ms, imgFormat);
        return new FileContentResult(ms.ToArray(), $"image/png");
    }
}

我想测试这个服务,所以我用这段代码创建了一个 C# 测试:

Task<HttpResponseMessage> responseTask = client.PostAsync(url, content);
responseTask.Wait();
var response = responseTask.Result;

HttpContent ct = response.Content;
byte[] data = await ct.ReadAsByteArrayAsync();
using (MemoryStream m = new MemoryStream(data))
{                         
    Bitmap img = new Bitmap(m);
    img.Save(filePath, ImageFormat.Png);
}

但位图img是 Format32bppArgb。如何以原始格式 (Format8bppIndexed) 获取我的图像?

4

1 回答 1

1

直接保存你得到的东西:

using (var fs = new FileStream(filePath, FileMode.Create))
    fs.Write(data, 0, data.Length);
于 2019-02-07T08:41:40.010 回答