Response.Write(Session["FileName"].ToString());
Bitmap myBitmap = new Bitmap(Session["FileName"].ToString());
Response.Write((Session["FileName"]));
给你个控件,直接通过Stream流来显示图片的:
Imports System
Imports System.Collections.Specialized
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.Design
Imports System.Web.UI.WebControls
Imports System.Reflection
Imports System.ComponentModel
Namespace core
Public Enum emImageType
Jpeg
Gif
End Enum
Public Enum emPersistence
Cache
Session
End Enum
Public Class ImageControlDesigner
Inherits System.Web.UI.Design.ControlDesigner
Public Sub New()
End Sub
Public Overrides Function GetDesignTimeHtml() As String
Return GetDesignTimeHtml()
End Function
Protected Overrides Function GetEmptyDesignTimeHtml() As String
Return CreatePlaceHolderDesignTimeHtml("<div>[Image is set at runtime.Place control inside Table ID or DIV for absolute positioning.]</div>")
End Function
End Class
<Designer("lib8SCommon.core.ImageControlDesigner"), _
ToolboxData("<{0}:ImageControl Runat='server'></{0}:ImageControl>")> _
Public Class ImageControl
Inherits Control
Protected ImageUrl As String
Private _imageType As emImageType
<Description("Image Type")> _
<Category("Data")> _
<DefaultValue("Jpeg")> _
<Browsable(True)> _
Public Property ImageType() As emImageType
Get
Return _imageType
End Get
Set(ByVal value As emImageType)
_imageType = value
End Set
End Property
Private _persistenceType As emPersistence
<Description("Cache or Session Persistence")> _
<Category("Data")> _
<DefaultValue("Cache")> _
<Browsable(True)> _
Public Property PersistenceType() As emPersistence
Get
Return _persistenceType
End Get
Set(ByVal value As emPersistence)
_persistenceType = value
End Set
End Property
Private _bitmap As Bitmap
<Browsable(False)> _
Public Property proBitmap() As Bitmap
Get
If Me.PersistenceType = emPersistence.Session Then
Return CType(Context.Session(String.Concat(CreateUniqueIDString(), "Bitmap")), Bitmap)
Else
Return CType(Context.Cache(String.Concat(CreateUniqueIDString(), "Bitmap")), Bitmap)
End If
End Get
Set(ByVal value As Bitmap)
If Me.PersistenceType = emPersistence.Session Then
Context.Session(String.Concat(CreateUniqueIDString(), "Bitmap")) = value
Else
Context.Cache(String.Concat(CreateUniqueIDString(), "Bitmap")) = value
End If
End Set
End Property
Private Function CreateUniqueIDString() As String
Dim idStr As String = String.Empty
Dim tmpId As String = String.Empty
If Me.PersistenceType = emPersistence.Session Then
idStr = "__" + Context.Session.SessionID.ToString() + "_"
Else
If Context.Cache("idStr") Is Nothing Then
tmpId = Guid.NewGuid().ToString()
Context.Cache("idStr") = tmpId
End If
idStr = "__" + Context.Cache("idStr").ToString() + "_"
End If
idStr = String.Concat(idStr, UniqueID)
idStr = String.Concat(idStr, "_")
idStr = String.Concat(idStr, Page.ToString())
idStr = String.Concat(idStr, "_")
Return idStr
End Function
Private Sub ImageControl_Init(ByVal e As EventArgs)
Dim httpRequest As HttpRequest = Context.Request
Dim httpResponse As HttpResponse = Context.Response
Dim vsParam As String = String.Concat("ImageControl_", UniqueID)
If Not (httpRequest.Params(vsParam) Is Nothing) Then
httpResponse.Clear()
Try
If Me.ImageType = emImageType.Gif Then
httpResponse.ContentType = "Image/Gif"
proBitmap.Save(httpResponse.OutputStream, ImageFormat.Gif)
proBitmap.Dispose()
httpResponse.OutputStream.Dispose()
httpResponse.OutputStream.Flush()
Else
httpResponse.ContentType = "Image/Jpeg"
proBitmap.Save(httpResponse.OutputStream, ImageFormat.Jpeg)
httpResponse.OutputStream.Dispose()
httpResponse.OutputStream.Flush()
End If
Catch ex As Exception
End Try
httpResponse.End()
End If
Dim str As String = httpRequest.Url.ToString()
If str.IndexOf("?") = -1 Then
ImageUrl = String.Concat(str, "?ImageControl_", UniqueID, "=1")
Else
ImageUrl = String.Concat(str, "&ImageControl_", UniqueID, "=1")
End If
End Sub
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
ImageControl_Init(e)
End Sub
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
writer.Write("<img id={0} src={1}", Me.UniqueID, ImageUrl)
End Sub
End Class
End Namespace
使用控件:
Dim ms As MemoryStream = Nothing
Dim buffer() As Byte = Nothing
Dim tmpObject As Object = Nothing
tmpObject = DataBinder.Eval(e.Item.DataItem, "imgPImage") //我是在Gridview中的显示的,"imgPImage"是数据库的Image字段的值
If Not Convert.IsDBNull(tmpObject) Then
buffer = tmpObject
If buffer.Length <> 0 Then
ms = New MemoryStream()
ms.Write(buffer, 0, buffer.Length)
ImageControl1.proBitmap = CType(System.Drawing.Image.FromStream(ms), Bitmap)
End If
End If