I have a page http://www.mysite.com/image.aspx, that I want to load and display an image instead of rendering HTML.
I have the ContentType of the page set to image/png, and here's my code:
using (Bitmap image = new Bitmap("http://www.google.com/images/img.png"))
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.WriteTo(Response.OutputStream);
}
}
But I get an error saying:
URI formats are not supported.
How can I load an external image and render it to the page?
You can't load a Bitmap using a URI - it has to be a local file to your computer.
If you want to load an image from off the web and then render it, you need to make a web request off to that specific resource and then render the bytes to the stream as you are doing.
AKA
WebRequest webRequest = WebRequest.Create("http://www.google.com/images/img.png");
using(WebResponse response = webRequest.GetResponse())
{
using(MemoryStream stream = new MemoryStream(response.GetResponseStream())
{
stream.WriteTo(Response.OutputStream);
}
}
Related
i am getting stream from httpwebresponse which is send by the another web server, here i want to convert this web stream to Bitmap and then this converted image is used to show as response in c#.
HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse myResp = (HttpWebResponse)myWebRequest.GetResponse())
{
if (myResp.ContentType.Contains("image/jpeg"))
{
Stream myStream = myResp.GetResponseStream();
System.Drawing.Image.FromStream(myResp.GetResponseStream());
You can convert stream to image like that
I need to fill my picturebox (or panel) with google map, but it won't whenever I change size in url. How to do that, or is there a better provider (msn, openstreetmap ...ect)?
string urlmaps = "http://maps.googleapis.com/maps/api/staticmap?center=43.56,4.48&size=600x600&sensor=true&format=png&maptype=roadmap&zoom=10";
var request = WebRequest.Create(urlmaps);
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
{
pictureBox1.Image = Bitmap.FromStream(stream);
You can use PictureBox.Load(string url) to load the remote image directly into the picure box.
I downloaded a image by using this method .
public Image getImageFromURL(String sURL)
{
using (WebClient wc = new WebClient())
{
var data = wc.DownloadData(sURL);
var image = Image.FromStream(new MemoryStream(data));
return image;
}
}
In my razor view and it returns me Bitmap object , but when i want to show the downloaded in image then it doesn't work
Thanks in advance
If you get Image in response from your getImageFromURL(String sURL) method.
you need to store image at any location in your application, then you can render it on view.
you can use this code.
var image = Image.FromStream(new MemoryStream(data));
string path=Path.Combine(Server.MapPath("~/images"), imagename);
image.Save(path);
this will save image at your images folder, and then you can render your image from this images folder location on view.
<img src="~/images/imagename" alt="" />
I have a products sale module in which products are uploaded from cj and saved in to database..today i noticed few records contained image url but returns 404(eg image url:http://www.bridalfashionmall.com/images/satin-2.jpg) hence shows no image in the repeater ..how can i check whether the url called dynamically has image in it
The method suggested by sean could be used as first pass. As second pass you can try loading the stream into image and see if it is actually image?
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(imageFilePath);
request.Timeout = 5000;
request.ReadWriteTimeout = 20000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.Drawing.Image img = System.Drawing.Image.FromStream(response.GetResponseStream());
// Save the response to the output stream
Response.ContentType = "image/gif";
this one helped---
http://stackoverflow.com/questions/1639878/how-can-i-check-if-an-image-exists-at-http-someurl-myimage-jpg-in-c-asp-net
this one too worked
try
{
WebClient client = new WebClient();
client.DownloadData(ImageUrl);
}
catch
{
imgPhoto.ImageUrl = ../User/Images/ResourceImages/Candychocolate1.jpg";//default image path
}
How can I view images from a website in my application using language C#.
I don't want to download all the page, I just want view the pictures in my application.
You can use the HTML Agility Pack to find <img> tags.
You will need to download the html for the page using WebRequest class in System.Net.
You can then parse the HTML (using HTML Agility Pack) extract the URLs for the images and download the images, again using the WebRequest class.
Here is some sample code to get you started:
static public byte[] GetBytesFromUrl(string url)
{
byte[] b;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
WebResponse myResp = myReq.GetResponse();
Stream stream = myResp.GetResponseStream();
using (BinaryReader br = new BinaryReader(stream))
{
b = br.ReadBytes(100000000);
br.Close();
}
myResp.Close();
return b;
}
You can use this code to download the raw bytes for a given URL (either the web page or the images themselves).
/// Returns the content of a given web adress as string.
/// </summary>
/// <param name="Url">URL of the webpage</param>
/// <returns>Website content</returns>
public string DownloadWebPage(string Url)
{
// Open a connection
HttpWebRequest WebRequestObject = (HttpWebRequest)HttpWebRequest.Create(Url);
// You can also specify additional header values like
// the user agent or the referer:
WebRequestObject.UserAgent = ".NET Framework/2.0";
WebRequestObject.Referer = "http://www.example.com/";
// Request response:
WebResponse Response = WebRequestObject.GetResponse();
// Open data stream:
Stream WebStream = Response.GetResponseStream();
// Create reader object:
StreamReader Reader = new StreamReader(WebStream);
// Read the entire stream content:
string PageContent = Reader.ReadToEnd();
// Cleanup
Reader.Close();
WebStream.Close();
Response.Close();
return PageContent;
}