Thread: Images and streams

  1. #1
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120

    Images and streams

    So I was looking for the best way to do this.
    I want to use a URL to pull an image that can be in PNG JPG or GIF depending on my call.
    Also, if the image does not meet my query, it will return XML and I will need to capture when that happens but not necessarily need to keep the XML returned.
    I only want to make the request once.
    Viewing the image isn’t necessarily important to the person running the application as long as the request is actually made.
    I am going to make this a multi-threaded application (which for me is the easy part, hah) but I am still working on this request evaluation.

    Would the best way to approach this be create a stream then figure out if that stream in text or if the response was one of the image headers? I am fairly new to c# and I am learning new stuff every day, I just have not come across this yet.

    I am trying to make a simple tool that can load test and give percentages of error on an application for work and our engineers and QA staff (including myself) use a java utility to do this currently I just thought it would be a fun project to dive into c# with.

    Any ideas or comments would be greatly appreciated.
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  2. #2
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    The magic of the boards strikes again. Every time I ask a question on the boards, it gives me insight and I come up with a conclusion.

    this time is no different but I am still open to suggestion on it. Here is how I did it, although I am not sure if it is making multiple requests to populate. I will be able to verify that tomorrow at work, but for now this is the basics.

    Code:
    string imgUrl = "http://url.to.image";
    string imgContentType = null;
    Bitmap imgTemp = null;
    
    WebRequest imgRequest = WebRequest.Create(imgUrl);
    
    WebResponse imgResponse = imgRequest.GetResponse();
    
    Stream imgStream = imgResponse.GetResponseStream();
    imgContentType = imgResponse.ContentType;
    if (imgContentType == "image/png" || imgContentType == "image/gif" || imgContentType == "image/jpeg")
    {
        imgTemp = new Bitmap(imgResponse.GetResponseStream());
        pic1.Image = imgTemp;
        //pic1 is just a panel that I used to temporarily show the image.
    }
    else
    {
        StreamReader imgReader = new StreamReader(imgStream);
        string xmlError = imgReader.ReadToEnd();
        imgReader.Close();
        MessageBox.Show("Content not an image, \r\nType: "+imgContentType + "\r\nError:\r\n" + xmlError); 
    }
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    35
    Async functions will be rather better.
    im from LMoldovaZ

Popular pages Recent additions subscribe to a feed