Partial Class GetImage
Inherits System.Web.UI.Page
'在内存中绘图
'作者:武广敬
Sub sendFile()
Dim PicMode As String = Request.QueryString("m")
Dim PicFolder As String
Dim ImageSize = CInt(Request("s"))
If PicMode = "" Then
PicMode = "p"
End If
Select Case PicMode
Case "p"
PicFolder = "image/picpro/"
Case "n"
PicFolder = "Image/Picnews/"
Case Else
PicFolder = "image/picpro/"
End Select
If File.Exists(Server.MapPath(PicFolder & Request("id") & ".jpg")) And Request("s") <> "" And Request("s") < 10000 And Request("s") > 0 Then
Dim g As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath(PicFolder & Request("id") & ".jpg"))
Dim thisFormat = g.RawFormat
Dim ImageWidth As Integer = ImageSize
Dim ImageHeight As Integer = ImageSize
If g.Width > ImageSize Or g.Height > ImageSize Then
If g.Width > g.Height Then
ImageHeight = CInt((ImageHeight / g.Width) * g.Height)
Else
ImageWidth = CInt((ImageSize / g.Height) * g.Width)
End If
Else
ImageWidth = g.Width
ImageHeight = g.Height
End If
Dim imgOutput As New System.Drawing.Bitmap(g, ImageWidth, ImageHeight)
If thisFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif) Then
Response.ContentType = "image/gif"
Else
Response.ContentType = "image/jpeg"
End If
imgOutput.Save(Response.OutputStream, thisFormat)
g.Dispose()
imgOutput.Dispose()
Else
Call SendError()
End If
End Sub
'如果图片不存在,动态绘制一个No Image的图片
Sub SendError()
Dim imgOutput As New System.Drawing.Bitmap(120, 120, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(imgOutput)
g.Clear(System.Drawing.Color.White)
g.DrawString("No Image", New System.Drawing.Font("Arial", 14, System.Drawing.FontStyle.Bold), System.Drawing.SystemBrushes.WindowText, New System.Drawing.PointF(2, 2))
Response.ContentType = "image/gif"
imgOutput.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif)
g.Dispose()
imgOutput.Dispose()
End Sub
End Class