Click here to Skip to main content
15,943,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to extract images from other websites and display it in gridview in asp.net with c#
Posted

Step 1: Extract images from a website

Have a look at this thread discussing[^] the same.

Once you extract the images, you need to store it locally or somewhere you like to.

Step 2: Now, fetch the images wherever stored to show it in a gridview. This is a well discussed over internet with lots of articles. Few are here at CP itself.

Try!
 
Share this answer
 
Comments
Espen Harlinn 19-Feb-11 10:19am    
Nice reply :)
Sandeep Mewara 19-Feb-11 10:21am    
Thanks again.
Hi,

As an example....
You may have a separate aspx file and at it's page load event have this code..
C#
    protected void Page_Load(object sender, EventArgs e)
    {
        string path = Request.QueryString["image_path"];
        Response.ContentType = "image/jpg";
        WebRequest objReq = WebRequest.Create(path);
        WebResponse objResp = objReq.GetResponse();
        Stream objStream = objResp.GetResponseStream();
        int length = (int)objResp.ContentLength;
        BinaryReader reader = new BinaryReader(objStream, Encoding.ASCII);
        byte[] bytes = reader.ReadBytes((int)length);
        Response.OutputStream.Write(bytes, 0, length);
        reader.Close();
        objStream.Close();
    }

you need to use these additional namespaces in that page...
<pre lang="cs">using System.IO;
using System.Net;
using System.Text;


In your main aspx file have an image and a button

XML
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:Image ID="Image1" runat="server" ImageUrl="" />



on the button click event

C#
protected void Button1_Click(object sender, EventArgs e)
{
    Image1.ImageUrl = "~/image.aspx?image_path=http://www.a1indiaflowers.in/wp-content/uploads/2010/12/47-150x150.jpg";
}



This example you can extend to gridview row bound event. This loads the image dynamically. But as Sandeep said you can have a local cache of those images and have that image list in a database. So that you can check the image availability and if available load from local.

To download to local you can get the response stream in the above example and use a file stream to save it on the server..
C#
//here the objStream is the response stream we got in the above example.
StreamReader reader = new StreamReader(objStream,Encoding.ASCII);

FileStream fStream = new FileStream("Test.jpg", FileMode.Create);
StreamWriter writer=new StreamWriter(fStream);
writer.Write(reader.ReadToEnd());
objStream.Close();
reader.Close();
writer.Close();
fStream.Close();
 
Share this answer
 
Comments
Espen Harlinn 19-Feb-11 10:20am    
Good effort :)
Albin Abel 19-Feb-11 12:11pm    
Thanks

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