您需要爲圖像生成HTML IMG標記並提供正確的內容。一種方法是直接把圖像源在網頁中作爲data uri來源:
#lang web-server/insta
(require pict
net/base64
file/convertible)
;; pict->data-uri : Pict -> String
(define (pict->data-uri pict)
(format "data:image/png;base64,~a"
(base64-encode (convert pict 'png-bytes))))
;; start : Request -> Response
(define (start request)
(send/suspend/dispatch
(lambda (make-url)
(response/xexpr
`(html
(body
(p "hello, here's a circle:")
(img ([src ,(pict->data-uri (circle 10))]))))))))
另一種方法是使圖像源的URL,將圖像發送作爲其響應:
#lang web-server/insta
(require pict
file/convertible)
;; pict-response : Pict -> Response
(define (pict-response pict)
(response/full 200 #"Ok"
(current-seconds) #"image/png"
(list)
(list (convert pict 'png-bytes))))
;; start : Request -> Response
(define (start request)
(send/suspend/dispatch
(lambda (make-url)
(response/xexpr
`(html
(body
(p "hello, here's a circle:")
(img ([src ,(make-url
(lambda (req)
(pict-response (circle 10))))]))))))))
感謝您的回答,但是我可以使用多參數程序繪製圖片嗎?這似乎是一個問題(圓20「solid」「blue」) – Dimitry
@Dimitry,來自'pict'庫的'circle'函數與來自'2htdp/image'的函數不同。使用'pict'庫的純藍色圓圈的代碼是'(colorize(disk 20)'blue')'。或者你可以用'2hdp/image'替換'pict'的require行,因爲這個庫的圖像也可以轉換成PNG字節。你甚至可以一起使用這兩個庫,因爲'pict'庫函數通常可以接受'2htdp/image'圖像作爲參數(但據我所知,其他方式不會)。你需要使用'prefix-in'來處理名稱衝突。 –
謝謝你的解釋。我已經做了一個修復,它運行良好 – Dimitry