Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

ASP.net C# Saving an Image to disk using an Image URL

Options
  • 27-08-2008 1:32pm
    #1
    Registered Users Posts: 2,791 ✭✭✭


    Hi,

    I have an image control on a page which is loaded with a URL pointing to an image handler (.ashx).

    I need to save this image to disk but I am having trouble getting it to work.

    I have two functions, one for reading in an image from the URL and returning it in a Byte[] array.

    The second function should write this byte[] to disk as an image, but it throws an exception "Parameter is not valid" when assigning newImage with the FromStream value.

    The byte array is populated with data but I cannot verifiy that this is valid, and my gut feeling is that it's contents are what's causing the problem. I say this because I tried a different method which did write a new image but it was blank.

    I suspect the image handler is the cause of my problems.

    Would appreciate any comments or suggestions...
    public byte[] GetBytesFromUrl(string sUrl)
            {
    
                //Saves an image into bytes from a given URL
                byte[] b;
                HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(sUrl);
                WebResponse myResp = myReq.GetResponse();
    
                Stream stream = myResp.GetResponseStream();
    
                using (BinaryReader br = new BinaryReader(stream))
                {
                    b = br.ReadBytes(500000);
                    br.Close();
                }
                myResp.Close();
                return b;
    
            }
    
            public void WriteBytesToFile(string sFileName, byte[] content)
            {
                System.Drawing.Image newImage;
    
                using (MemoryStream ms = new MemoryStream(content, 0, content.Length))
                {
                    ms.Write(content, 0, content.Length);
    
                    newImage = System.Drawing.Image.FromStream(ms,true);
                    ms.Close();
                    newImage.Save(sFileName);
                   
                }
    
            }
    


Comments

  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    You should (I think) be able to read the image into a System.Drawing.Image object, use a System.Net.Webclient object to read to a stream then use the Image.Fromstream() static method to create your System.Drawing.Image object.

    Similar to what you are doing but saves you having to go through the byte array.


  • Registered Users Posts: 2,931 ✭✭✭Ginger




  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Ginger wrote: »

    Thanks but the Image Handler which is implemented is customised with unique specifications, so I can't replace it.

    I have discovered that the reason the image is blank is because of an authentication issue. If I open it with notepad then it contains the login.aspx script.

    I don't know how to get around this issue... :(


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Ok, your request to the website must contain authentication as well..

    Now if its forms, you can create a generic user context and use that to create the request..

    If its a windows auth, create a windows impersonation context with the correct credentials and use that context to create the request to download your file.

    namespace is System.Security.Principal and System.Security.Permissions

    Sample idea is http://www.codeproject.com/KB/cs/cpimpersonation1.aspx


Advertisement