Click here to Skip to main content
15,881,766 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an Url with me....I need to show a image in picture box....Pls give some idea ji

What I have tried:

C#
string newUrl1 = null;
newUrl1 = txtURL.Text + "Modules/UserManagement/ImageLoader.ashx" + "?PortalSettingId=1";
var request = WebRequest.Create(newUrl1);

using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
{
    pictureBox11.Image = Bitmap.FromStream(stream);
}
Posted
Updated 9-Feb-21 23:09pm
v2

See answer here: c# load an image from URL into picturebox - Stack Overflow[^]

Not all URL's will work, but this one should: https://codeproject.global.ssl.fastly.net/App_Themes/CodeProject/Img/logo250x135.gif[^]

If you have any spaces in your URL you should escape them with %20, see:
url - How do I replace all the spaces with %20 in C#? - Stack Overflow[^]
 
Share this answer
 
v3
Comments
Member 15028582 10-Feb-21 5:10am    
Actually I use that Code but I didn't get the output.....
newUrl1 = txtURL.Text + "Modules/UserManagement/ImageLoader.ashx" + "?PortalSettingId=1";
pictureBox11.ImageLocation = newUrl1;
RickZeeland 10-Feb-21 5:12am    
Maybe you need an extra slash: "/Modules/ ..."
Member 15028582 10-Feb-21 5:31am    
No Ji....its not working...I have added the slash also....
RickZeeland 10-Feb-21 5:33am    
And when you try the CodeProject URL I gave you?
Member 15028582 10-Feb-21 5:40am    
Yes ji....Ur code project is working
First off, don't just concatenate strings to form the path: unless the user remembers the trailing '/' the URL will be significantly different and will fail.
Use Path.Combine[^] instead.

Second, we can't check what is happening at all, as the site that the image is being sourced from depends on what the user enters, and we have no access to that.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
I'd start by looking at the completed URL closely, and then instead of building an image directly, I'd read the stream into a byte array and have a good look at that. Chances are that either the URL is wrong, or the site is returning an error message instead of the expected image stream - but only you can tell that via a close look using the debugger.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

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