I have an asp.net app that receives a image file from the Fileupload control. I put the image in an image object so i can manipulate it before puting it to disk if needed.
this is what I have:
Dim
i As System.Drawing.Image = System.Drawing.Image.FromStream(filUpload.FileContent)
I want to display the image that is in i into a image control on an asp.net page but withoiut having to write it to disk permenantly.
You could store the image in the user's Session or where ever you'd like then have another aspx called getimage.aspx which response.writes the contents of the session variable. You then set the ImageUrl of the asp image control to the getimage.aspx page.
You'll need to remember to set the header type of the response corectly before you write the image
Response.Headers.Clear(); Response.Headers.Add();
Please: Don't forget to click "Mark as Answer" on the post that helped you.
Thank you very much Camero-w. that surely sends me the novice in the right direction. But i will have to go dig deep to find exactly how to do all this.
first.
that page Getimage.aspx would have absolutly nothing in it at all excep for the first line of the @page.... correct?
then in the page_load i would do response.write(session("myimageobject")) .... corrct?
but first you are saying that I must set a few headers... but which ones? which are the correct headers? is this relating to the type of image it is? like jpg or gif etc....
I only am dealing with .jpg, .gif, .pnd type images
Glad you got it sorted, just to clear one thing up though, the chunk of code that I used to demonstrate getting the image into a byte[] was something I just found on the net and it happens to use bitmaps. You shouldn't think that you need to convert images
to bitmaps before writing them. In fact this would be a really bad idea since bitmaps are always bigger than the equivalent jpg / png / gif.
Rather just use the base System.Drawing.Image class, don't convert to bitmap and response.binarywrite the image in the same format that it's provided.
Please: Don't forget to click "Mark as Answer" on the post that helped you.
What if we try to asign an image url to System.Drawing.Bitmap object insted of physical address. In my scenario, I don't have physical path of the image. Any Idea?
Fahad Ausaf.
Don't forget to mark the post as "Answer" if it works out. This will help others.
Member
110 Points
868 Posts
displaying a picture in an image control from stream??
May 23, 2008 10:32 AM|bcweed966|LINK
I have an asp.net app that receives a image file from the Fileupload control. I put the image in an image object so i can manipulate it before puting it to disk if needed.
this is what I have:
Dim
i As System.Drawing.Image = System.Drawing.Image.FromStream(filUpload.FileContent)I want to display the image that is in i into a image control on an asp.net page but withoiut having to write it to disk permenantly.
Is this posible?
Participant
1590 Points
336 Posts
Re: displaying a picture in an image control from stream??
May 23, 2008 11:17 AM|cameron_w|LINK
You could store the image in the user's Session or where ever you'd like then have another aspx called getimage.aspx which response.writes the contents of the session variable. You then set the ImageUrl of the asp image control to the getimage.aspx page.
You'll need to remember to set the header type of the response corectly before you write the image
Response.Headers.Clear(); Response.Headers.Add();
Cameron Waldron
Member
110 Points
868 Posts
Re: displaying a picture in an image control from stream??
May 23, 2008 11:26 AM|bcweed966|LINK
Thank you very much Camero-w. that surely sends me the novice in the right direction. But i will have to go dig deep to find exactly how to do all this.
first.
that page Getimage.aspx would have absolutly nothing in it at all excep for the first line of the @page.... correct?
then in the page_load i would do response.write(session("myimageobject")) .... corrct?
but first you are saying that I must set a few headers... but which ones? which are the correct headers? is this relating to the type of image it is? like jpg or gif etc....
I only am dealing with .jpg, .gif, .pnd type images
Participant
1590 Points
336 Posts
Re: displaying a picture in an image control from stream??
May 23, 2008 11:42 AM|cameron_w|LINK
Yes, that's right the getimage.aspx would have nothing except the @Page line
The header you are setting is the content header, you need to specify a valid Mime type. If you don't know what the mime types are check here : http://www.w3schools.com/media/media_mimeref.asp
on your page load you would say
response.clear()
Response.ContentType = "image/jpeg"; //for a jpg image
and then response.write the image from the session variable.
Cameron Waldron
Member
110 Points
868 Posts
Re: displaying a picture in an image control from stream??
May 23, 2008 11:52 AM|bcweed966|LINK
Again thank you.
I am geting my image like that : Dim i As System.Drawing.Image = System.Drawing.Image.FromStream(filUpload.FileContent)
and its working fine but to do what you say, is there any casting needed to or from the session?
or is it just as straight forward as this? (of course setting the correct headers firrst)
Dim i As System.Drawing.Image = System.Drawing.Image.FromStream(filUpload.FileContent)
session("myimageobject") = i
and then send it in the response like that:
response.write(session("myimageobject"))
Participant
1590 Points
336 Posts
Re: displaying a picture in an image control from stream??
May 23, 2008 11:59 AM|cameron_w|LINK
you would actually need to get the image into a byte[] first
System.Drawing.Bitmap bmp = GetTheBitmap();
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
stream.Position = 0;
byte[] data = new byte[stream.Length];
stream.Read(data, 0, stream.Length);
And then use the Response.BinaryWrite method to write it as binary
Response.BinaryWrite(data);
Cameron Waldron
Member
110 Points
868 Posts
Re: displaying a picture in an image control from stream??
May 23, 2008 01:57 PM|bcweed966|LINK
ok. now where does that "getthebitmap()" come from?
again startin with what I have:
Dim i As System.Drawing.Image = System.Drawing.Image.FromStream(filUpload.FileContent)
should i just go like that to put my image object into a bitmap?:
Dim i As System.Drawing.Image = System.Drawing.Image.FromStream(filUpload.FileContent)
dim bitmapO as new bitmap = New Bitmap(i) ???????
or should i try to extract the bitmap directly from the fileupload control's stream?
Member
110 Points
868 Posts
Re: displaying a picture in an image control from stream??
May 23, 2008 01:59 PM|bcweed966|LINK
should i just go like that to put my image object into a bitmap?:
Dim i As System.Drawing.Image = System.Drawing.Image.FromStream(filUpload.FileContent)
dim bitmapO as new bitmap = New Bitmap(i) ???????
or should i try to extract the bitmap directly from the fileupload control's stream?
like such:
Dim
i As System.Drawing.Bitmap = System.Drawing.Bitmap.FromStream(filUpload.FileContent)Member
110 Points
868 Posts
Re: displaying a picture in an image control from stream??
May 23, 2008 02:27 PM|bcweed966|LINK
so this is what I have done:
I have pulled the bitmap object directly from the fileupload control and put in the session
Dim bitO As System.Drawing.Bitmap = System.Drawing.Bitmap.FromStream(filUpload.FileContent)Session(
"BudPollImage") = bitOthen I redirect to another page which has an image source set to a 3rd page called GetBudPollImage.aspx:
<
body> <form id="form1" runat="server"></
body>In that 3rd page I have the Page_load event like this:
Dim bitmapO As System.Drawing.Bitmap = Session("BudPollImage")Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream()bitmapO.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp)
stream.Position = 0
Dim data(stream.Length) As Bytestream.Read(data, 0, stream.Length)
Response.Clear()
Response.ContentType =
"image/jpeg"Response.BinaryWrite(data)
But the problem is that the page_load event of that 3rd page NEVER fires.... Why? and of course i get no image in my page.
Member
110 Points
868 Posts
Re: displaying a picture in an image control from stream??
May 23, 2008 03:09 PM|bcweed966|LINK
OK I got it to work. by setting the
BudPollPicUpload.ImageUrl =
"~/GetBubPollImage.aspx" at runtime and it displayed the image.Thanks!
Participant
1590 Points
336 Posts
Re: displaying a picture in an image control from stream??
May 24, 2008 03:46 AM|cameron_w|LINK
Glad you got it sorted, just to clear one thing up though, the chunk of code that I used to demonstrate getting the image into a byte[] was something I just found on the net and it happens to use bitmaps. You shouldn't think that you need to convert images to bitmaps before writing them. In fact this would be a really bad idea since bitmaps are always bigger than the equivalent jpg / png / gif.
Rather just use the base System.Drawing.Image class, don't convert to bitmap and response.binarywrite the image in the same format that it's provided.
Cameron Waldron
Member
110 Points
868 Posts
Re: displaying a picture in an image control from stream??
May 24, 2008 09:51 AM|bcweed966|LINK
oh. thats nice to know and means back to work for me... again
Member
100 Points
20 Posts
Re: displaying a picture in an image control from stream??
Dec 23, 2009 08:21 AM|fahadausaf|LINK
Nice example Cameron, your code saved my day (y)
Fahad Ausaf
Member
100 Points
20 Posts
Re: displaying a picture in an image control from stream??
Dec 23, 2009 08:50 AM|fahadausaf|LINK
Hello Cameron,
What if we try to asign an image url to System.Drawing.Bitmap object insted of physical address. In my scenario, I don't have physical path of the image. Any Idea?
Fahad Ausaf.
Member
91 Points
127 Posts
Re: displaying a picture in an image control from stream??
Feb 25, 2010 12:14 AM|naimish_hit|LINK
Use HTTPHandler for it.
See this, http://www.revenmerchantservices.com/page/asp-net-image-upload.aspx
ND
None
0 Points
3 Posts
Re: displaying a picture in an image control from stream??
Aug 05, 2013 07:55 AM|Ashish Bachhav|LINK
i also have same problem. i am trying the above, but it's not working for me, plz tell me how to show image from stream.
i am using network socket. from which i get the image in stream format.
it works in VB.net but have some proble in ASP.net