14 Replies - 2922 Views - Last Post: 29 August 2010 - 02:19 PM Rate Topic: -----

Topic Sponsor:

#1 Ahmedn1  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 17
  • View blog
  • Posts: 513
  • Joined: 04-August 09

image download web request

Posted 29 August 2010 - 09:11 AM

I want to load yahoo avatar in a picture box

my function that gets the stream is

private byte[] download(string url)
        {
            byte[] buffer = new byte[1024];
            try
            {
                WebRequest req = WebRequest.Create(url);
                WebResponse response = req.GetResponse();
                buffer = new byte[response.ContentLength];
                Stream stream = response.GetResponseStream();
                int numBytesRead = stream.Read(buffer, 0, buffer.Length);

            }
            catch (Exception)
            {
                MessageBox.Show("There was a problem downloading the file");
            }
            return buffer;
        }



where the url is like "http://img.msg.yahoo.com/avatar.php?yids=ahmedn32004"

and the code in the button click is

byte[] imageData = download(url);
                    MemoryStream stream = new MemoryStream(imageData);
                    Image img = Image.FromStream(stream);
                    avatar.Image = img;
                    stream.Close();



after this executes it only shows about tenth of the picture not all of it
why the rest of the picture doesn't appear?
it is just a part of it and the rest is black

and another thing

I realized that while the app is sending the request and waiting for an answer it hangs till the answer is received

so I used threading

but when I used that all variables (that are supposed to come from the request answer) are empty
it seems that the thread doesn't wait till the connect method finishes and continue to the next line

I used a void method as the thread starter and in this one there are calls to other methods

what can I do ?

Is This A Good Question/Topic? 0
  • +

Replies To: image download web request

#2 Imdsm  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: image download web request

Posted 29 August 2010 - 09:34 AM

You are not returning the stream, you're only returning an array of bytes, 1024 bytes in fact. So you'll only ever have 1024 bytes of the picture. What you need to do is either loop through and read all of the image and return a full buffer, or return the stream.

Scratch that, I didn't see buffer = new byte[response.ContentLength];

What does response.ContentLength return?

Also what does numBytesRead return?

private byte[] download(string url)
{
	byte[] buffer = new byte[1024]; // buffer is only 1024 bytes
	try
	{
		WebRequest req = WebRequest.Create(url);
		WebResponse response = req.GetResponse();
		buffer = new byte[response.ContentLength];
		Stream stream = response.GetResponseStream();
		int numBytesRead = stream.Read(buffer, 0, buffer.Length); // you only read one chunk
	}
	catch (Exception)
	{
		MessageBox.Show("There was a problem downloading the file");
	}
	return buffer;
}

This post has been edited by Imdsm: 29 August 2010 - 09:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 Ahmedn1  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 17
  • View blog
  • Posts: 513
  • Joined: 04-August 09

Re: image download web request

Posted 29 August 2010 - 10:29 AM

never mind numBytesRead it returns number of bytes in the stream
it was part of old function but now it is not used I'm just using stream.Read() to copy the stream to the byte array

and response.ContentLength returns the length of the response from the server
Was This Post Helpful? 0
  • +
  • -

#4 Imdsm  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: image download web request

Posted 29 August 2010 - 10:35 AM

I already know what they are suppose to do, but in order to debug this for you it helps me to see these figures and if they are abnormal.
Was This Post Helpful? 0
  • +
  • -

#5 Ahmedn1  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 17
  • View blog
  • Posts: 513
  • Joined: 04-August 09

Re: image download web request

Posted 29 August 2010 - 10:39 AM

here is a screen shot of the avatar that appears after this code execution

Attached Image
Was This Post Helpful? 0
  • +
  • -

#6 Core  Icon User is offline

  • Microsoft Student Insider
  • member icon

Reputation: 753
  • View blog
  • Posts: 5,085
  • Joined: 08-December 08

Re: image download web request

Posted 29 August 2010 - 10:55 AM

I think you are making the situation a bit more complicated for yourself. What you can do instead is this:

Image Download(string url)
{
    Image image;
    WebRequest req = WebRequest.Create(url);
    using (WebResponse response = req.GetResponse())
    {
        image = Image.FromStream(response.GetResponseStream());
    }

    return image;
}


Was This Post Helpful? 3
  • +
  • -

#7 Imdsm  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: image download web request

Posted 29 August 2010 - 12:19 PM

View PostAhmedn1, on 29 August 2010 - 09:39 AM, said:

here is a screen shot of the avatar that appears after this code execution

Attachment yahooAvatar.jpg


I'm afraid this is about as helpful to debugging as your first reply was. So sorry but, without more info, I can't help. But as Core has shown you, there are easier ways.

This post has been edited by Imdsm: 29 August 2010 - 12:19 PM

Was This Post Helpful? 0
  • +
  • -

#8 Ahmedn1  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 17
  • View blog
  • Posts: 513
  • Joined: 04-August 09

Re: image download web request

Posted 29 August 2010 - 01:14 PM

Core's Solution worked great
but what about the thread problem ?
no one have a solution?
Was This Post Helpful? 0
  • +
  • -

#9 Imdsm  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: image download web request

Posted 29 August 2010 - 01:19 PM

In order for us to debug something we have to see the code...
Was This Post Helpful? 0
  • +
  • -

#10 Core  Icon User is offline

  • Microsoft Student Insider
  • member icon

Reputation: 753
  • View blog
  • Posts: 5,085
  • Joined: 08-December 08

Re: image download web request

Posted 29 August 2010 - 01:30 PM

What you need is a BackgroudWorker:

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);

worker.RunWorkerAsync();



The DoWork event handler:

void worker_DoWork(object sender, DoWorkEventArgs e)
{
    _Image = Download("YOUR_URL_HERE");
}



The property I am referencing here is a simple Image instance:

public Image _Image { get; set; }

Was This Post Helpful? 1
  • +
  • -

#11 Ahmedn1  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 17
  • View blog
  • Posts: 513
  • Joined: 04-August 09

Re: image download web request

Posted 29 August 2010 - 01:57 PM

Very good Core
it worked as I wanted but there is an exception occurring

in the DoWork handler
here is my code

void bWork_DoWork(object sender, DoWorkEventArgs e)
        {
            txtStat.Text = "Working";
            if (ydet.result[2] != "")
            {
                try
                {
                    avatar.Image = download(ydet.result[2]);
                }
                catch (Exception)
                {
                    MessageBox.Show("There was a problem downloading the file");
                }
            }
        }



it gives me this exception
"An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code

Additional information: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.
"

the exception is on the line

txtStat.Text = "Working";

Was This Post Helpful? 0
  • +
  • -

#12 Imdsm  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: image download web request

Posted 29 August 2010 - 02:03 PM

View PostAhmedn1, on 29 August 2010 - 12:57 PM, said:

Very good Core
it worked as I wanted but there is an exception occurring

in the DoWork handler
here is my code

void bWork_DoWork(object sender, DoWorkEventArgs e)
        {
            txtStat.Text = "Working";
            if (ydet.result[2] != "")
            {
                try
                {
                    avatar.Image = download(ydet.result[2]);
                }
                catch (Exception)
                {
                    MessageBox.Show("There was a problem downloading the file");
                }
            }
        }



it gives me this exception
"An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code

Additional information: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.
"

the exception is on the line

txtStat.Text = "Working";


This is a textbook cross thread call exception, rather than write it all out again, I'll link you to my blog post as my Dream.In.Code tutorial of the same subject hasn't been validated yet.

http://imdsm.blogspo...-made-easy.html
Was This Post Helpful? 1
  • +
  • -

#13 Core  Icon User is offline

  • Microsoft Student Insider
  • member icon

Reputation: 753
  • View blog
  • Posts: 5,085
  • Joined: 08-December 08

Re: image download web request

Posted 29 August 2010 - 02:04 PM

You cannot make cross-thread calls between the background thread and the UI thread. Your best bet would be using a property and setting its value from DoWork. Then you can set the control property based on the one set by the thread.

Also, in your case, you can set the value to "Working" before calling RunWorkerAsync, in the calling method.
Was This Post Helpful? 1
  • +
  • -

#14 Imdsm  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: image download web request

Posted 29 August 2010 - 02:19 PM

View PostCore, on 29 August 2010 - 01:04 PM, said:

You cannot make cross-thread calls between the background thread and the UI thread. Your best bet would be using a property and setting its value from DoWork. Then you can set the control property based on the one set by the thread.

Also, in your case, you can set the value to "Working" before calling RunWorkerAsync, in the calling method.


It would be simple enough to call a paramaterized thread and pass a delegate to a method which then invokes itself on the UI thread when updating the UI, as I linked above
Was This Post Helpful? 1
  • +
  • -

#15 Ahmedn1  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 17
  • View blog
  • Posts: 513
  • Joined: 04-August 09

Re: image download web request

Posted 29 August 2010 - 02:19 PM

Imdsm solution worked like a charm
thanks mate
thank you both Core and Imdsm
I'll re-read the article to understand what is really going on
but I just applied the technique and it worked
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1