由于您已经获得了包含数据库中图像的数据库,因此我将忽略整个“我应该将图像存储在数据库问题中吗”。我在这里只提到它是因为我相信其他人会对此发表评论并为我扣分,因为我没有提到这不是最好的主意。我会尽量回答你的问题。
您可以让网络服务直接返回图像。这很简单……
这是一个来自网络服务的代码 sn-p,我只是为了看看你能不能做到这一点。希望您可以根据需要对其进行修改。
<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