How to save image Formed in Response.Write - asp.net

Hi Iam generating an image using Response.Write in a web page, I want to save the image generated in a folder, help me doing it please.
Response.Write("<img src='image/Black.gif'>")

`Dim Image As String Image="" For i=0 to 10
Image=Image &""
Next Response.Write(Image) `
An image will be displayed using this code, now the question is I want to save that as a single image in my folder(server side)
use a fileupload control
//Get Filename from fileupload control
string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
//Save images into Images folder
fileuploadimages.SaveAs(Server.MapPath("Images/"+filename));
if your image generated is a stream use Image class to convert it
Image.FromStream(yourstream);
to save it without upload control:
Image.Save(Server.MapPath("Images/"+filename"));
`Dim Image As String
Image=""
For i=0 to 10
Image=Image &""
Next
Response.Write(Image)
`
An image will be displayed using this code, now the question is I want to save that as a single image in my folder(server side)

Related

Display resized image as a return value and not as page content

On Page_Load, I want the page to display an image the same way it displays when you "Open image in a new tab". In other words, I want the page to display as content type Image/jpeg instead of rendering the image in img HTML tag.
I successfully displayed an image this way using the below code on Page_Load:
Page.Response.ContentType="image/jpeg"
Page.Response.WriteFile("Path_to_the_Image")
However, my case requires displaying the image after being resized. I created a function that resizes the image and returns the resized image as Drawing.Bitmap
ResizeImage(bmSource As Drawing.Bitmap, TargetWidth As Int32, TargetHeight As Int32)
How can I use the returned value of that function and display it the same way I displayed the first image?
Page.Response.WriteFile() requires a String as an input. Is there a way where I can display the image of type Image or System.Bitmap directly into the page without having to save it in a temp folder and then use its path (As a String) and pass it to Page.Response.WriteFile?
Page.Response.ContentType="image/jpeg"
Using resizedImage As Bitmap = ResizeImage(source, 100, 100)
source.Dispose()
resizedImage.Save(Page.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
End Using
Page.Response.Flush()
That should work, maybe there's some error in my syntax. I do not often code in vb.net so.
Edited the code because of #Joel Coehoorn note

Add Browse button in dynamically created view - Entity Framework

In my model I want to add a string property that will save the url/path of an image. For that I need to add "Browse" button next to a textbox for the user to select file & also a Image to display the selected image.
How can I save this image somewhere else and not in the DB - as that will increase the size of DB and save time for transferring image from byte[] to file & vice-versa ? Like this, through out the project, I will have many images to upload & show. Right now I am on local DB, Is this possible to achieve, if so, how?
The view file where I want to add browse & Image is like this:
<asp:DynamicControl Mode="Insert" DataField="PANNumber" runat="server" />
it's inside <InsertItemTemplate> <fieldset> & <Validate-Summary>
The view is created by scaffolding.

How to display image on web page from ADODB.stream object

I have a classic ASP question.
I need to show images on my webpage.
But because of the security reasons, I cannot simply put all of my images in the images folder in my website folder and I need to put them outside of my website folder.
For example, my website folder is located at:
C:\Inetpub\wwwroot\mysite\
But I need to put images (which I want to show on my web pages) at:
C:\images\
I am trying to use ADODB.stream object to pull the images using ASP vb codes as shown below:
<%
Response.ContentType = "image/png"
Set adoStream = Server.CreateObject("ADODB.Stream")
adoStream.Open
adoStream.Type = 1
FPath = "C:\images\1.png"
adoStream.LoadFromFile FPath
Response.BinaryWrite adoStream.Read
adoStream.Close
Set adoStream = Nothing
Response.End
%>
When I launch this webpage in internet explorer, the page shows a File Download window/message for downloading the image file "1.png" rather than displaying the image on the web page.
How can I fix the code to show the image on the web page rather than downloading the image file?
That code generates an image as the return type, so there is no HTML to display the image in, if you call this code directly say it's called image.asp then you would do something like this in another page that displays HTML.
<img src="image.asp" />
This will call your code and output the raw image binary as this is what <img> expects.
If you want to produce multiple images from the file system or database pass a querystring parameter such as ?id=yourimage and change the code to select different images.

How to assign Bitmap to ImageControl

I am trying to bind image to image control, but image is bitmap data. How can I?
Do you mean that you have an image as binary data and you want to display it without saving it in a file? If yes, then one solution will be to create a separate aspx page or an http handler to return the image. You need to set the content type and length accordingly and use Response.BinaryWrite for serving the image.
See here for some more suggestions.

How can I set an Image received from a method to the ImageButton?

I have a method that returns an Image.
How can I assign that Image to my ImageButton control so it has that image set?
Since you're dealing with HTML, you'll need to save the Image to a file, then use that file in the ImageButton's ImageUrl property.
I believe an ImageButton takes the path to an Image, not an actualy Image object, as the browser renders it a an tag.
What you could do is save thr image to disk, return the path to the image form your method and then have
<asp:ImgageButton id="imgButton1" runat="server" imageUrl="<%= GetImageUrl()>" />
The above syntax is not exact, it might be "<% Response.Write(GetImageUrl())>" but you get the picture
If this method returns an Image object, you will need to save out the image to a physical location on your webserver (somewhere like Server.MapPath("~/images/")). Then you will need to set the ImageUrl property of the ImageButton control to this location.
If this method returns a relative path to the image file, simply set the ImageUrl property of the ImageButton to the path returned by the method.
In short, you can't. Well not directly anyway.
You will have to write your image to file and point the image button at your image file
or you can have a web page that returns the image in the response and use that as your ImageUrl
The reason for this is that your ImageButton just renders to HTML which has no support for attaching images ATM.
You can try the following approach:
1) Firstly, get the binary data from the image:
public byte[] ImageToByteArray(System.Drawing.Image image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
2) Secondary use Inline Images with Data URLs idea:
Inline images use the data URI scheme
to embed images directly within web
pages. As defined by RFC 2397, data
URIs are designed to embed small data
items as "immediate" data, as if they
were referenced externally. Using
inline images saves HTTP requests over
externally referenced objects.
System.Drawing.Image image = GetImageFromSomewhere(...);
byte[] imageData = ImageToByteArray(image);
string imageBase64 = Convert.ToBase64String(imageData);
string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);
imgButton.Src = imageSrc;
But unfortunately, this won't work for IE5-7 (should work in IE8).

Resources