gpt4 book ai didi

f# - 有没有办法将图像直接通过管道传输到温和的响应流?

转载 作者:行者123 更新时间:2023-12-04 15:19:50 25 4
gpt4 key购买 nike

我想在服务器端动态生成一个图像并将其发送到浏览器。

目前我正在使用 MemoryStream将其转换为 byte array然后是通常的 suave api。见下图:

let draw (text:string) =

let redBr = new SolidBrush(Color.Red)
let whiteBr = new SolidBrush(Color.White)
let i = new Bitmap(600,400)
let g = Graphics.FromImage i
let f = new Font("Courier", float32 <| 24.0)
g.FillRectangle(whiteBr, float32 <| 0.0, float32 <| 0.0, float32 <| 600.0, float32 <| 400.0 )
g.DrawString(text, f, redBr, float32 <| 10.0, float32 <| 40.0)
g.Flush()
let ms = new System.IO.MemoryStream()
i.Save(ms, ImageFormat.Png)
ms.ToArray()

let app : WebPart =
Writers.setMimeType "image/png"
>=> (Successful.ok <| draw "bibi")

我觉得 MemoryStream如果 suave 允许我们直接管道到响应流,则可以避免部分。

谢谢!

最佳答案

你基本上这样做:

open System.IO
open Suave
open Suave.Sockets
open Suave.Sockets.Control

path "/byte-stream" >=> (fun ctx ->

let write (conn, _) = socket {
use ms = new MemoryStream()
ms.Write([| 1uy; 2uy; 3uy |], 0, 3)
ms.Seek(0L, SeekOrigin.Begin) |> ignore
// do things here
let! (_,conn) = asyncWriteLn (sprintf "Content-Length: %d\r\n" ms.Length) conn
let! conn = flush conn
do! transferStream conn ms
return conn
}

{ ctx with
response =
{ ctx.response with
status = HTTP_200.status
content = SocketTask write } }
|> succeed
)

关于f# - 有没有办法将图像直接通过管道传输到温和的响应流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37804071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com