.NET-Core字节数组到Image

时间:2017-06-13 10:57:11

标签: asp.net-core .net-core

如何在.NET Core中将using (var ms = new MemoryStream(byteArrayIn)) { return Image.FromStream(ms); } 转换为图像?

我找到this

Image

但似乎.NET-Core中不存在TITLE: Connect to Server Cannot connect to *_DESKTOP_*. A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 2)

3 个答案:

答案 0 :(得分:3)

确实还没有发布。您可以使用库ImageSharp

答案 1 :(得分:1)

要从字节数组返回图像,您可以:

  1. 返回base64

    Byte[] profilePicture = await _db.Players
                                     .Where(p => p.Id == playerId)
                                     .Select(p => p.ProfilePicture)
                                     .FirstOrDefaultAsync();
    
    return Ok(Convert.ToBase64String(profilePicture));
    

    然后您可以使用将base64转换为图像的任何online tool进行测试。

  2. 或返回FileContentResult File(byte [] fileContents,字符串contentType)

    return File(profilePicture, "image/png");
    

    您应该能够从邮递员那里进行测试,或者类似任何类似的图片一样会在其中显示出来。

答案 2 :(得分:0)

如果您使用的是javascript

img标签:

html

class OrderStatus extends Mailable
{
    use Queueable, SerializesModels;

    public $user;
    public $items;

    public function __construct(User $user, Order $items)
    {
        $this->user = $user;
        $this->items = $items;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.order_status');
    }
}

js:

<img id="logoPic" src="" />

with insertCell:

document.getElementById("logoPic").src = "data:image/png;base64," + yourByte;

与td:

row.insertCell(2).innerHTML = "<img src='" + "data:image/png;base64," + yourByte + "'></>";

如果您使用的是jquery:

html

"<td>"+ "<img src='" + "data:image/png;base64," + yourByte + "'></>" + "</td>"

jQuery:

<img id="logoPic" src="" />