Response.OutputStream

J

Jenny

Hi all,

I'm using a second page to write dynamical generated images
into the outputstream. This avoids using tmp-files on disc.

My code-behind in the start aspx file is:

'Use second file to write into outputstream
Application("grafix-obj") = ob
button.ImageUrl = "Grafix.aspx"
Panel.Controls.Add(button)

ob is the bitmap with the image! The file Grafix.aspx
contains only code behind:

Public Class grafix
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

......

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles MyBase.Load

Dim ob As Bitmap = New Bitmap(830, 30)
ob = CType(Application("grafix-obj"), Bitmap)
ob.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
End Sub

End Class

I normally expect this to be working! But there is only the
'no image' sign on the imagebutton if I try.
I used the following code before, which worked well:

'Store as tmp file on disk
Dim tempfilepath As String = System.IO.Path.GetTempPath
ob.Save(tempfilepath &"temp.jpg",Imaging.ImageFormat.Jpeg
button.ImageUrl = tempfilepath & "temp.jpg"

What is going wrong?

Thanks for each help
Jenny
 
R

Roberto López

IN the Image.Save method, save it to a MemoryStream and after write the
memorystream to the response.outputstream of the page.

Dim memstr as new memorystream()
Image.Save(memstr,imageformat.jpeg)
memstr.writeto(response.outputstream)
 
M

Michal A. Valasek

| Private Sub Page_Load(ByVal sender As System.Object, ByVal
| e As System.EventArgs) Handles MyBase.Load
|
| Dim ob As Bitmap = New Bitmap(830, 30)
| ob = CType(Application("grafix-obj"), Bitmap)
| ob.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
| End Sub
|
| End Class
|
| I normally expect this to be working! But there is only the
| 'no image' sign on the imagebutton if I try.

Similar code works well for me, just specify content-type:
Response.ContentType = "image/jpeg".

The following is code I use at http://webcam.rider.cz:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim BMP As Bitmap, GPH As Graphics
Dim RQ As System.Net.HttpWebRequest
Dim RP As System.Net.HttpWebResponse

Response.ContentType = "image/jpeg"
Try
RQ = System.Net.WebRequest.Create("http://epona:8000/" &
Request.UserHostAddress)
RQ.UserAgent = Request.UserAgent
RP = RQ.GetResponse()
BMP = New Bitmap(RP.GetResponseStream())
GPH = System.Drawing.Graphics.FromImage(BMP)
Catch EX As Exception
BMP = BMP.FromFile(Server.MapPath("/_gfx/wc-error.bmp"))
GPH = System.Drawing.Graphics.FromImage(BMP)
GPH.DrawString("The following exception occured:" & vbCrLf &
EX.Message, New Font("Courier New", 10, FontStyle.Bold, GraphicsUnit.Point),
New SolidBrush(Color.White), 5, 400)
End Try
GPH.DrawImage(New Bitmap(Server.MapPath("/_gfx/wc-footer.bmp")), New
RectangleF(0, 460, 640, 20), New RectangleF(0, 0, 640, 20),
GraphicsUnit.Pixel)
GPH.DrawString("www.rider.cz | Prague CZ-EU | " &
Now.ToUniversalTime.ToString("yyyy-MM-dd HH:mm:ss") & " GMT", New
Font("Courier New", 10, FontStyle.Bold, GraphicsUnit.Point), New
SolidBrush(Color.White), 5, 462)
BMP.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg)
End Sub
 
V

vMike

Here is how I did it. I didn't bother with code behind on this one because
there isn't anything other than code. I call the image by setting it's
ImageURL or NavigateURLproperty to
pic.aspx?_p=abc.jpg&_w=123&_h=123&_q=1 (for image, or 2 for thumb)

<%@ Page Language="VB" SmartNavigation="false" Strict="true" %>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Drawing.Drawing2D" %>


<html>

<script language="vb" runat="server">
sub Page_Load(sender as object, e as eventArgs)

dim strPic as string = request.querystring("_p")
dim intWidth as int32 = ctype(request.querystring("_w"),int32)
dim intHeight as int32 = ctype(request.querystring("_h"),int32)
dim intQuality as int32 = ctype(request.querystring("_q"),int32)
dim myNewImage as system.drawing.image
dim strFileLoc as string =
server.mappath(configurationSettings.AppSettings("picroot"))


try
select case intQuality
case is = 1
if intWidth = 0 then intWidth = 200
if intHeight = 0 then intHeight = 200

mynewimage = mgAdjustBitmap(strFileLoc & strPic, intWidth, intHeight)
case else
if intWidth = 0 then intWidth = 90
if intHeight = 0 then intHeight = 90
mynewimage = mggetthumbnail(strFileLoc & strPic,intWidth,intHeight)

end select
mynewimage.Save(Response.OutputStream, ImageFormat.Jpeg)
catch
mynewimage= mggetthumbnail(strFileLoc & "npicture.jpg",90,90)
mynewimage.Save(Response.OutputStream, ImageFormat.Jpeg)
end try
End Sub

Function mgGetThumbNail(strFile as string, intWidth as integer, intHeight as
integer) as system.drawing.image
Dim myBitmap As bitmap = New Bitmap(strFile)
return mybitmap.getthumbnailimage(intWidth,intHeight,nothing,nothing)
mybitmap.dispose
End Function

Function mgAdjustBitmap(strFile as string, intHorz as integer, intVert as
integer) as bitmap

Dim mysize as size = new size(inthorz,intVert)
Dim myBitmap As bitmap = New Bitmap(strFile)
dim myImg as bitmap = new bitmap(mybitmap, mysize)
mybitmap.dispose
return myImg
End Function
</script>

</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top