SIGN UP MEMBER LOGIN:    
ARTICLE

Fetching Image from SharePoint 2007 picture library using object model

Posted by Dhananjay Kumar Articles | SharePoint February 08, 2011
Fetch an image from a SharePoint 2007 picture library and bind to a System.Drwaing.Image object
Reader Level:


I thought it would be an easy requirement to fetch an image from a SharePoint 2007 picture library and bind to a System.Drwaing.Image object. But when I started doing that, I had to give myself 3 to 4 hours. So here is the code snippet to fetch images from a SharePoint 2007 picture library.

Namespace Required

Share1.gif

Function to return Image as MemoryStream

This function will return an image from a SharePoint 2007 picture library using WSS Object model. This function is taking siteUrl and filename to be fetched from the picture library. This function is returning a 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 a System.Drawing.Image object.

Share2.gif
 

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor