วันศุกร์ที่ 20 กุมภาพันธ์ พ.ศ. 2552

Get Image from stream use local path.

have only one sample function here

        private Image GetImageFromLocal( string localPath )
        {
            using (FileStream fs = new FileStream( localPath, FileMode.Open, FileAccess.Read ))
            {
                Image img = Image.FromStream( fs );
                return img;
            }            
        }

Don't sure about performance from local or url and if found bad data.
I will try test later.

Get image from stream url.

have 2 sample function for this (c#)

1.
            private Image GetImage(string sURL)
            {
                Stream str = null;
                HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(sURL);
                HttpWebResponse wRes = (HttpWebResponse)(wReq).GetResponse();
                str = wRes.GetResponseStream();

               return Image.FromStream(str);
            }

2.
       private Image GetImageFromURL( string strURL )
        {
            //Initialize the return value 
            Image retVal = null;

            //Any errror while connecting to the server or streaming the data into an image can result in an exception! dont forget to use a try{}catch{} block! 
            try
            {
                //We use the HttpWebRequest object to connect to a web site: 
                HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create( strURL );

                //Be sure to populate the time out values in the request :) 
                request.Timeout = 10000; // 10 seconds in milliseconds
                request.ReadWriteTimeout = 40000; // allow up to 40 seconds to elapse

                //Retrieving the stream of data from the web server is very easy: 
                // execute the request
                HttpWebResponse response = (HttpWebResponse)
                request.GetResponse();

                //Now we convert the downloaded stream into an Image! 
                retVal = Image.FromStream( response.GetResponseStream() );
            }

            //Perform cleanup on an exception:
            catch (Exception)
            {
                retVal = null;
            }

            //Thats it! now we return the image :) 
            return retVal;
        }
sorry I can't remember for this reference i wrote this because i found problem about memory full by get Image use Image.GetFromFile so use stream can help alot.