I'm using Visual Studio 2012 btw, VB.net.
So I have an Imagebutton, and Image. What I want to do is when I click on Imagebutton, it loads up the image.
The thing is, I have 8 Imagebuttons, so the image must have a code to load up different images from my directory, which is the images folder from root.
This is the current code I have, which has an error.
Dim img As String
Image1.Visible = True
img = Image1.ImageUrl("/Images/Example.png")
The error is "Conversion from string "/Images/Example.png" to type 'Integer' is not valid."
So I changed Dim img As Integer now.
I get this error:
'Char' values cannot be converted to 'Integer'. Use 'Microsoft.VisualBasic.AscW' to interpret a character as a Unicode value or 'Microsoft.VisualBasic.Val' to interpret it as a digit.
I am at a loss now.
The error is rised when you call Image1.ImageUrl("/Images/Example.png")
If you want to chance the Image in Image1 when you click in button use this.
Image1.ImageUrl = "~/Images/Example.png"
and to set value form ImageUrl in img variable
img = Image1.ImageUrl
The ImageUrl property is the image that is displayed on the image button itself. If you want an image to be displayed on its own when the button is clicked then you need to set the image as the PostBackURL of the button.
e.g. Image1.PostBackUrl="~/Images/Example.png"
Related
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
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)
I have an ASP.NET 2010 app and am trying something very simple. I use Image control through out and basically wanted to say, before I assign an image to it (from a class) look for the image; if it's not there, use the default no_image jpeg I have. I have verified the image is there ad nauseam. If I set the property in the IDE to point to this image, it'll display. However, if I set it programmatically, even if I hard-code the path programmatically, it doesn't show.
Forgettign about getting the proper path and all that, (of which Ive tried several things) I can;t even hard code the relative path. Here I copied & pasted the relative path from the properties window...
Image1.ImageUrl = "~/UploadedPictures/Users/no_profile_picture.jpg"
This doesn't work. It translates into src="../../UploadedPictures/Users/no_profile_picture.jpg" which is correct but the image doesn't display.
If I hardcode the absolute path it doesn't work either...
Image1.ImageUrl = "C:\Inetpub\wwwroot\myApp\UploadedPictures\Users\no_profile_picture.jpg"
You have to use a virtual path. Try adding ~ before your path.
Image1.ImageUrl = "~/UploadedPictures/Users/no_profile_picture.jpg"
I have an ASP.NET web app. What I am attempting should be simple: after an image is displayed, I have a Rotate button which should allow the user to rotate the image 90 degrees. Here is the button click code...
Dim i As Image
i = Image.FromFile("C:\Inetpub\wwwroot\myWebApp\MyImage.jpg")
'rotate the picture by 90 degrees
i.RotateFlip(RotateFlipType.Rotate90FlipNone)
're-save the picture as a Jpeg
i.Save("C:\Inetpub\wwwroot\myWebApp\MyImage.jpg",System.Drawing.Imaging.ImageFormat.Jpeg)
'tidy up after we've finished
i.Dispose()
The image and button used here are non-remarkable. When I created a sample app with just 1 page this works perfectly. However, when I put in into my main app, even if its in a new page, all by itself with nothing else, not even a masterpage, it does, in fact rotate the image and write it back to the file system, but it doesn't show the rotated file. It shows the image as it was. UNTIL I hit F5, then, no matter how many times I hit the button, it works perfectly. I have tried eveything i can think of to clear the cashe to no avail.
Are you re-requesting the image using an update panel or some other mechanism? You will be able to force a refresh by putting something like a timestamp or similar GET parameter on the IMG src, or in the ASP:Image source.
Instead of saving the image back to the web root you can keep the original image as is and just stream the rotated image to the client.
<img src="RotateImage.aspx?Image=MyImage.jpg&Rotate=90" />
and then in your RoateImage.aspx do the same load/rotate work and:
i.Save(Response.OutputStream, ImageFormat.Jpeg)
Response.ContentType = "image/jpeg"
The image is served from the cache, that is why it is not shown updated. To show the newer version of the same image. You may attach a query string to the path of the image. For example,
<img src='/images/image.jpg?<%= DateTime.Now.Ticks %>' />
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).