[RESOLVED] Reading an Image into memory stream

Hi Friends,
Please help me with this code. This is what I am trying to do.
Read an image into memory stream and write the Binary data to a database
url = "https://graph.facebook.com/me/albums/?access_token=" + oAuth.Token;
                    json = oAuth.WebRequest(oAuthFacebook.Method.GET, url, String.Empty);
                    Albums albms = js.Deserialize<Albums>(json);
                    foreach (Album alb in albms.data)
                    {
                        alb.picture = "https://graph.facebook.com/" + alb.id + "/picture/?access_token=" + oAuth.Token;
url = "https://graph.facebook.com/me/albums/?access_token=" + oAuth.Token;
                    json = oAuth.WebRequest(oAuthFacebook.Method.GET, url, String.Empty);
                    Albums albms = js.Deserialize<Albums>(json);
                    foreach (Album alb in albms.data)
                    {
                        alb.picture = "https://graph.facebook.com/" + alb.id + "/picture/?access_token=" + oAuth.Token;

                        MemoryStream ms = new MemoryStream(1000);
                            string sfn = alb.picture;
                        System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(alb.picture));
                            m_lImageFileLength = alb.picture.Length;
                            m_barrImg = new Byte[Convert.ToInt32(m_lImageFileLength)];
                            
                            ms.Read(m_barrImg, 0, (int)m_lImageFileLength);
                            //Basically I want to read this alb.picture into a memory stream, instead of storing it on the local harddisk
and write the binary data into DB.
                         insert_Facebook_Photos(Convert.ToInt64(ui.id), m_barrImg, alb.created_time.ToString(), alb.updated_time.ToString());
                         fs.Close();



 

private Image GetRemoteImage(string filePath)
{
 WebClient webClient = new WebClient();
byte[] imageBytes = webClient .DownloadData(filePath);
    MemoryStream mem = new MemoryStream(imageBytes);
   return Image.FromStream(mem);
}

To download a remote image you need to use HttpWebRequest of WebClient .Both classes are in System.Net namespace

Try below code

 

@Shakti Singh: Why should I use Web Client

I have my code like this

MemoryStream ms = new MemoryStream(1000);
                            string sfn = alb.picture;
                           
                            //FileInfo filImage = new FileInfo(sfn);
                            m_lImageFileLength = alb.picture.Length;
                            m_barrImg = new Byte[Convert.ToInt32(m_lImageFileLength)];
                            
                            ms.Read(m_barrImg, 0, (int)m_lImageFileLength);
                            
                            objBLL.insert_FacebookPhots(Convert.ToInt64(ui.id), m_barrImg, alb.created_time.ToString(), alb.updated_time.ToString());
                            
But here it is just inserting s 0x000000000000000000000000000. and not any image

Thanks

Smitha

Your memorystream contain no data..Since file is present on remote location) as url suggests you need to download it and save that in memory stream then from memory stream you can save it back to db

 

alb.picture = "https://graph.facebook.com/" + alb.id + "/picture/?access_token=" + oAuth.Token; // This is just a URL
 
                        MemoryStream ms = new MemoryStream(1000);
                            string sfn = alb.picture;
                  //      System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(alb.picture)); //This way you were retriving image only then you will have image bytes


 ms.Read(m_barrImg, 0, (int)m_lImageFileLength); //This is nothing but an empty byte array

thoughts on "[RESOLVED] Reading an Image into memory stream"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>