hi

i'm writing a c# script to be used in ssis....
i'm trying to pull an image from a website...
i'm new to c# and i'm not sure whether the data types i'm using are right...
i'm posting my code here...

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Net;
using System.IO;


namespace ST_09069b9ea31e4c45836292539999e637.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        public void Main()
        {
            string url = generateUrl();

            getImage(url);

            Dts.TaskResult = (int)ScriptResults.Success;
        }

        public string generateUrl()
        {
            WebRequest request = WebRequest.Create("http://www.movieposterdb.com/embed.inc.php?movie_id=0333766");
            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader responseReader =
                  new StreamReader(response.GetResponseStream()))
                {
                    string responseData = responseReader.ReadToEnd();
                    int beg_pos = (responseData.IndexOf("src=")) + 6;
                    int end_pos = (responseData.Length) - 12;
                    string tempUrl = responseData.Substring(beg_pos, 76);
                    string imgUrl = tempUrl.Replace("t_", "l_");

                    return (imgUrl);

                }
            }

        }

        public void getImage(string url)
        {
            WebRequest request1 = WebRequest.Create(url);
            using (WebResponse response1 = request1.GetResponse())
            {
                using (StreamReader responseReader =
                  new StreamReader(response1.GetResponseStream()))
                {
                    object responseData1 = responseReader.ReadToEnd();

                    using (StreamWriter writer =
                      new StreamWriter(@"C:\SaveResponse\sample.jpg"))
                    {
                        writer.Write(responseData1);
                    }

                }
            }

        }

    }

}

when i open sample.jpg it says invalid image....

please help....

Recommended Answers

All 3 Replies

That is because the address is not returning an image! Try it in your browser:
http://www.movieposterdb.com/embed.inc.php?movie_id=0333766

I am getting a page of javascript:

document.write('<a target=\"_new\" href=\"http://www.movieposterdb.com/movie/0333766/Garden-State.html\"><img title=\"Garden State\" alt=\"Garden State\" style=\"width: 100px; height: 143px; border: 0px;\" src=\"http://www.movieposterdb.com/posters/08_11/2004/333766/t_333766_b9a6e423.jpg\" /></a>');

dotacohen....
the script extracts the image url from the javascript and then sends a request to that url.....


oki...
i have handled the issue....
attached is the code snippet which downloads images from a specified url.....

public Image DownloadImage(string _URL)
        {
            Image _tmpImage = null;

            try
            {
                // Open a connection
                System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(_URL);

                _HttpWebRequest.AllowWriteStreamBuffering = true;

                // Request response:
                System.Net.WebResponse _WebResponse = _HttpWebRequest.GetResponse();

                // Open data stream:
                System.IO.Stream _WebStream = _WebResponse.GetResponseStream();

                // convert webstream to image
                _tmpImage = Image.FromStream(_WebStream);

                // Cleanup
                _WebResponse.Close();
                _WebResponse.Close();
            }
            catch (Exception _Exception)
            {
                // Error
                Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
                return null;
            }

            return _tmpImage;
        }

you need to include the System.Drawing namespace....

Great, I'm glad it worded out!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.