Fetching Image from SharePoint 2007 picture library using object model

I thought this is an easy requirement to fetch Image form SharePoint 2007 picture library and bind to a System.Drwaing.Image object. But when I started doing that, I had to give my 3 to 4hrs. So here is the code snippet to fetch images from SharePoint 2007 Picture library.

Namespace Required


clip_image002


Function to return Image as MemoryStream

This function will return Image from Picture Library of SharePoint 2007 using WSS Object model. This function is taking siteUrl and filename to be fetched from the picture library. This function is returning MemoryStream



public static MemoryStream GetImageforCharts(string siteUrl, string fileName)
        {
            Byte[] fileContentsArray=null; 
            MemoryStream imageStream = null; 

            try
            {
                using (SPSite site = new SPSite(siteUrl))
                // using (SPSite site = SPContext.Current.Site)
                {
                    using (SPWeb web = site.OpenWeb())
                    {

                     SPPictureLibrary chartPictureLibrary = 
                                      (SPPictureLibrary)web.Lists["UrPictureLibraryName"];
                        SPQuery query = new SPQuery();
                query.Query = @"<Where><Eq>
                                 <FieldRef Name ='Title'/>
                               <Value Type='Text'>" + fileName + "</Value></Eq></Where>";
                    SPListItemCollection lstImages = chartPictureLibrary.GetItems(query);
                        foreach (SPListItem r in lstImages)
                        {
                            SPFile file = r.File;
                            using (Stream fileContents = file.OpenBinaryStream())
                            {
                                long  length = fileContents.Length ;
                                fileContentsArray = new Byte[length]; 
                          fileContents.Read(fileContentsArray, 0,Convert.ToInt32(length));
                            }
                            
                        }

                        

                    }

                }

                imageStream = new MemoryStream(fileContentsArray);
                return imageStream; 
              

            }
            catch (Exception ex)
            {
                return null; 
            }



Using the Function

Now we can call the function as below, and save the Image in System.Drawing.Image object.

clip_image004

Published by

Dhananjay Kumar

Dhananjay Kumar is Developer Evangelist for Infragistics. He is a 8 times Microsoft MVP and well respected Developer Advocate in India.He is the author of 900+ Blog Posts, and can often be found speaking around India at conferences and hosting free workshops for programmers across the country. So far, he has hosted 60 free workshops on various topics like JavaScript, Angular, WCF, ASP.NET MVC, C#, Azure etc. Follow him on twitter @debug_mode for all the updates about his blog posts and workshops. You can send him email at debugmode [at] outlook [dot] com

2 thoughts on “Fetching Image from SharePoint 2007 picture library using object model”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s