首页 新闻 会员 周边 捐助

asp.net如何将上传的图片在页面直接输出

0
悬赏园豆:20 [已关闭问题] 关闭于 2008-10-04 23:42

今天碰到这样一个问题,我想将上传的图片直接用流输出到页面中,但是我照着例子做的时候,它老是输出乱码,代码很简单,请高手指点,到底哪里出问题了。

Code
idoku的主页 idoku | 菜鸟二级 | 园豆:267
提问于:2008-09-24 10:30
< >
分享
所有回答(3)
0

Response.Write(Session["FileName"].ToString());

zjy | 园豆:3194 (老鸟四级) | 2008-09-24 10:53
0

Bitmap myBitmap = new Bitmap(Session["FileName"].ToString());

Response.Write((Session["FileName"]));

有所为,有所不为 | 园豆:1200 (小虾三级) | 2008-09-24 11:01
0

给你个控件,直接通过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

iLove.Net | 园豆:140 (初学一级) | 2008-09-25 09:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册