6

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

1
  • below answer doesn't work at all sir Commented May 21, 2013 at 7:56

2 Answers 2

2

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.

9
  • is this internal names of field sir ? Commented May 20, 2013 at 9:14
  • yes, it is internal names
    – Andrew
    Commented May 20, 2013 at 9:48
  • sorry sir but it;s no working, width is coming null Commented May 20, 2013 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
    Commented May 20, 2013 at 12:45
  • yes provide sample plesae Commented May 20, 2013 at 12:46
0

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);
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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