Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I created a event receiver but work ok for single file but no work for multiple files,

HttpFileCollection collection = _context.Request.Files;
            for (int i = 0; i < collection.Count; i++)
            {
                HttpPostedFile postedFile = collection[i];

                Stream fileStream = postedFile.InputStream;
                fileStream.Position = 0;
                byte[] fileContents = new byte[postedFile.ContentLength];
                fileStream.Read(fileContents, 0, postedFile.ContentLength);

                System.Drawing.Image image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(fileContents));

                int imageHeight = image.Height;
                int imageWidth = image.Width;

when multiple file upload it asks,

object reference no set...

at first line, what to do sir

share|improve this question
    
below answer doesn't work at all sir –  Customized Name May 21 '13 at 7:56
add comment

2 Answers

up vote 2 down vote accepted

Picture Library Schema contains fields ImageWidth and ImageHeight. You could get it in your receiver. For example:

public override void ItemAdded(SPItemEventProperties properties)
{
    if (properties.ListItem["ImageWidth"].ToString() == "300")
    {
       //your code here
    } 
}

Fields ImageWidth and ImageHeight are filled only in ItemAdded/ItemUpdated events.


If you want to get image sizes in ItemAdding you could get file from HttpContext and wrap it into System.Drawing.Image.

Here is sample event receiver:

public class PicsReceiver : SPItemEventReceiver
{

    private HttpContext _context;

    public PicsReceiver()
    {
        _context = HttpContext.Current;
    }

    public override void ItemAdding(SPItemEventProperties properties)
    {
        base.ItemAdding(properties);

        string searchForFileName = Path.GetFileName(properties.BeforeUrl);
        HttpFileCollection collection = _context.Request.Files;
        for (int i = 0; i < collection.Count; i++)
        {
            HttpPostedFile postedFile = collection[i];
            if (searchForFileName.Equals(
                Path.GetFileName(postedFile.FileName), StringComparison.OrdinalIgnoreCase))
            {
                Stream fileStream = postedFile.InputStream;
                Image img = Image.FromStream(fileStream);
                //use img.Width and img.Height to get file sizes
            }
        }
    }
}

I found that method in William van Strien's blog.

NOTE: You need to add references to System.Web.dll and System.Drawing.dll.

share|improve this answer
    
is this internal names of field sir ? –  Customized Name May 20 '13 at 9:14
    
yes, it is internal names –  Andrew May 20 '13 at 9:48
    
sorry sir but it;s no working, width is coming null –  Customized Name May 20 '13 at 11:51
    
yes, my fault. You can get this properties only in ItemAdded/ItemUpdated events via properties.ListItem["Image Width"]. Then to get width/height in ItemAdding you could try to get image from properties.BeforeUrl and get it's size. I can provide sample for you if you want. –  Andrew May 20 '13 at 12:45
    
yes provide sample plesae –  Customized Name May 20 '13 at 12:46
show 4 more comments

I created the same thing a few years back. Here's the code if it helps u

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
using Microsoft.SharePoint.Utilities;
using System.Drawing;
namespace SharepointImageResizerProject
{
    class ImageResizerEventHandler : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            try
            {
                //Check user is site admin , remove it if you dont need it
                if (properties.OpenWeb().CurrentUser.IsSiteAdmin)
                {
                    SPFile file = properties.ListItem.File;
                    if (null != file)
                    {
                        properties.OpenWeb().AllowUnsafeUpdates = true;
                        try
                        {
                            MemoryStream msOut = new MemoryStream();
                            MemoryStream msIn = new MemoryStream(file.OpenBinary(), true);
                            int width = 100; //set to desired value 
                            int height = 200; //set to desired value 
                            Image img = Image.FromStream(msIn).GetThumbnailImage(width, height, null, IntPtr.Zero);
                            img.Save(msOut, System.Drawing.Imaging.ImageFormat.Jpeg);
                            file.SaveBinary(msOut, false);
                            file.CheckOut();
                            file.Update();
                            file.CheckIn(string.Empty);
                        }
                        finally
                        {
                            properties.OpenWeb().AllowUnsafeUpdates = false;
                        }
                    }
                }
            }
            catch
            {
                //Do some exception handling 
                throw;
            }
        }
        public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);
        }
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);
        }
        public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);
        }
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.