Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
im trying to get a image as response from a website using image's absolute url. i tried below code. response is showing but not the image is visible. i think here simple mistake is happened. please help me here.

What I have tried:

// Creates an HttpWebRequest with the specified URL.
                HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create("https://www.planwallpaper.com/static/images/HD-Wallpapers-C76_yCO55lB.jpg");

                myWebRequest.Method = "GET";
                myWebRequest.ContentType = "image/jpeg";

                // Sends the HttpWebRequest and waits for the response.		
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myWebRequest.GetResponse();   //HttpWebRequest_GetResponse():  Returns a response from an Internet resource.

                //get Stream Data from the response
                Stream receiveStream = myHttpWebResponse.GetResponseStream();

                //read the response from stream.
                StreamReader streamReader = new StreamReader(receiveStream);

                byte[] data = System.Text.Encoding.Default.GetBytes(streamReader.ReadToEnd());
                Response.ContentType ="image/jpeg";
                Response.BinaryWrite(data);
                Response.End();
Posted
Updated 11-Jan-18 6:53am

Quote:
//get Stream Data from the response
Stream receiveStream = myHttpWebResponse.GetResponseStream();

//read the response from stream.
StreamReader streamReader = new StreamReader(receiveStream);

byte[] data = System.Text.Encoding.Default.GetBytes(streamReader.ReadToEnd());

You're reading binary data from the remote server. You're sending binary data back to the client. So why are you converting that binary data to a string (using UTF8), and then converting it back to binary (using your server's default encoding)?

Remove that step, and that will probably fix your problem:
Stream receiveStream = myHttpWebResponse.GetResponseStream();

Response.ContentType ="image/jpeg";
receiveStream.CopyTo(Response.OutputStream);
Response.End();
 
Share this answer
 
Comments
rocker_003 12-Jan-18 0:37am    
wowww...awesome bro... its working... thank u very much.
If You want to Download image from url then the best way is used
System.Net.WebClient


Try This:

C#
Using System.Net.WebClient;

string imageUrl = "https://www.planwallpaper.com/static/images/HD-Wallpapers-C76_yCO55lB.jpg";
string SavePath = @"D:\Pictures\HD-Wallpapers-C76_yCO55lB.jpg";
System.Net.WebClient client = new WebClient();
client.DownloadFile(new Uri(imageUrl), SavePath);
 
Share this answer
 
v2
Comments
rocker_003 11-Jan-18 1:23am    
thanks but i need to show only as response, not to download it. is it possible? if possible means whats that way?
rocker_003 11-Jan-18 4:50am    
help me please...im strucked here
Okay then try this:

C#
public void save_file_from_url(string file_name, string url)
    {
        byte[] content;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        WebResponse response = request.GetResponse();
 
        Stream stream = response.GetResponseStream();
 
        using (BinaryReader br = new BinaryReader(stream))
        {
            content = br.ReadBytes(500000);
            br.Close();
        }
        response.Close();
 
        FileStream fs = new FileStream(file_name, FileMode.Create);
        BinaryWriter bw = new BinaryWriter(fs);
        try
        {
            bw.Write(content);
        }
        finally
        {
            fs.Close();
            bw.Close();
        }
    }
 
Share this answer
 
v2
Comments
rocker_003 11-Jan-18 2:34am    
thanks.but i need to show image as response by using absolute path without downloading.
please guide me without downloading image, how to show a image response?
CHill60 11-Jan-18 7:42am    
It's good practice to use the "Improve solution" link and update your original solution rather than posting a second solution - it's confusing - which one do you mean to be correct?
imgCapture is the id of asp image tag
protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (Request.InputStream.Length > 0)
            {
                using (StreamReader reader = new StreamReader(Request.InputStream))
                {
                    string hexString = Server.UrlEncode(reader.ReadToEnd());
                    string imageName = "PI" + DateTime.Now.Year.ToString().Substring(1) + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg";
                    Session["pimage"] = imageName;
                    string imagePath = string.Format("~/pimage/{0}", imageName);
                    File.WriteAllBytes(Server.MapPath(imagePath), ConvertHexToBytes(hexString));
                    imgCapture.ImageUrl = imagePath; 
                    Session["CapturedImage"] = ResolveUrl(imagePath);
                }
            }
        }
    }

    private static byte[] ConvertHexToBytes(string hex)
    {
        byte[] bytes = new byte[hex.Length / 2];
        for (int i = 0; i < hex.Length; i += 2)
        {
            bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
        }
        return bytes;
    }
 
Share this answer
 
Comments
rocker_003 11-Jan-18 2:34am    
thanks.but i need to show image as response by using absolute path without downloading.
please guide me without downloading image, how to show a image response?
Dotnet_Dotnet 11-Jan-18 6:22am    
hiI told that .First save the image in the server.
Then display in a image control.I have maintioned above.
I take the image with id imgCapture <asp:image id="imgCapture" runat="server">

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900