.net 页面如何返回一张图片?

pjw100 2010-11-01 11:41:06
首先我的前台页面是空的,通过后台代码来返回图片。
我有两个页面TestOpen.aspx和Open.aspx(这个页面返回图片)

我通过两种方式都不能得到图片,不知道什么原因,望解答。
1。在Open.aspx.cs中通过Response.Write("http://localhost/OpenTest/11.jpg")直接写一个路径,
在TestOpen.aspx中这样使用<img src="tOpen.aspx">

2。在Open.aspx.cs中写如下方法。
然后再Page_Load中调用GenerateImage()方法。

Private Sub GenerateImage()

Try

Dim gifImage As Image = Image.FromFile(Server.MapPath("~/11.jpg"))
Dim bm As New Bitmap(gifImage.Width, gifImage.Height, PixelFormat.Format8bppIndexed)

' Get the paletter from the bitmap
Dim pal As ColorPalette = bm.Palette

' Set Alpha to 0
For i As Integer = 0 To pal.Entries.Length - 1
Dim col As Color = pal.Entries(i)
pal.Entries(i) = Color.FromArgb(0, col.R, col.G, col.B)
Next

' assign palette
bm.Palette = pal

' to copy the bitmap data we need to lock the source & destination bits
Dim src As BitmapData = CType(gifImage, Bitmap).LockBits(New Rectangle(0, 0, gifImage.Width, gifImage.Height), ImageLockMode.ReadOnly, gifImage.PixelFormat)
Dim dst As BitmapData = bm.LockBits(New Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.WriteOnly, bm.PixelFormat)

' finished, unlock the bits
CType(gifImage, Bitmap).UnlockBits(src)
bm.UnlockBits(dst)

' Set Response Type to "image/gif" and write the gif directly into the Output-Stream
Response.ContentType = "image/jpeg"
bm.Save(Response.OutputStream, ImageFormat.Jpeg)

' cleaning up
bm.Dispose()
gifImage.Dispose()

' Send output stream
Response.Flush()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub


这两种方法最终得到的结果就是在TestOpen中,显示的一个叉图片。(在IIS环境下)
但是我直接通过项目Browse能看到图片(非IIS),Host是带端口的那种。
...全文
181 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
pjw100 2010-11-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lihuan19870723 的回复:]

src后面也可以直接写网页路径吗?<img src="">是直接显示图片的格式吧 ,不明白。。。学习中。。。
[/Quote]
.NET页面可以通过留写出一个图片的。
毕竟Response.ContentType里面支持有很多是图片类型的。
lihuan19870723 2010-11-05
  • 打赏
  • 举报
回复
src后面也可以直接写网页路径吗?<img src="">是直接显示图片的格式吧 ,不明白。。。学习中。。。
ljzpzh 2010-11-05
  • 打赏
  • 举报
回复
有可能是路径指定不对.搜一下验证码图片示例代码,跟这个有点类似
Bullatus 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 liuwei2500 的回复:]

那就是路径的问题了,程序写的是对的,关键就是路径的问题了。
[/Quote]
同意,取的时候用的路径不对吧
liuwei2500 2010-11-01
  • 打赏
  • 举报
回复
那就是路径的问题了,程序写的是对的,关键就是路径的问题了。
liuwei2500 2010-11-01
  • 打赏
  • 举报
回复
~/Open.aspx?

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧