由于您已经获得了包含数据库中图像的数据库,因此我将忽略整个“我应该将图像存储在数据库问题中吗”。我只在这里提到它,因为我相信其他人会对此发表评论并给我扣分,因为我没有提到这不是最好的主意。我会尽我所能回答你的问题。
您可以让 Web 服务直接返回图像。这很简单...
这是我编写的 Web 服务的代码片段,只是为了看看你是否可以做到。希望您可以根据需要对其进行修改。
<WebMethod()> _
Public Function GetImage() As Byte()
Try
Dim outStream As New System.IO.MemoryStream
Dim REturnValue As New System.Drawing.Bitmap(500, 500)
Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(REturnValue)
'g.RotateTransform(5)
Dim f As New System.Drawing.Font(System.Drawing.FontFamily.GenericMonospace, 16, Drawing.FontStyle.Regular, Drawing.GraphicsUnit.Point)
Dim b As System.Drawing.Brush = Drawing.Brushes.Lime
g.DrawString("Hello", f, b, 0, 0)
g.DrawString("Would you like to play a game? (Y/N)", f, b, 0, 40)
g.DrawString("> Y", f, b, 0, 80)
g.DrawString("Loading Global Thermonuclear War,", f, b, 0, 120)
g.DrawString("please wait...", f, b, 0, 160)
REturnValue.Save(outStream, System.Drawing.Imaging.ImageFormat.Jpeg)
Return outStream.ToArray()
Catch ex As Exception
Throw New Exception(ex.ToString())
End Try
End Function
然后是显示图像的 Asp.Net 页面..
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ts As New TestServices
Dim b As System.Drawing.Bitmap
Dim bytes As Byte()
Dim inStream As System.IO.MemoryStream
bytes = ts.GetImage()
inStream = New System.IO.MemoryStream(bytes)
b = New System.Drawing.Bitmap(inStream)
Response.ContentType = "image/jpeg"
b.Save(Response.OutputStream, b.RawFormat)
b.Dispose()
End Sub