0

I have an event receiver that should download images from an imagelibrary, and do some stuff with that copy. There will be several images with the same customid that i want to grab, so I loop over all images and select images with correct id.

How can manage to get the specific item, but how do I grab the file in that item and pass it off as for example a bitmap?

So far I've got this:

//tempid is the value Im looking for in the library
tempid = 1000;
SPListItemCollection Images = pictureLib.Items;
for(int i = 0;i<pictureLib.ItemCount;i++)
{
    //get a specific lookupid,the one im im looking for.
    SPFielLookupValue value = new SPFieldLookupValue(itemid,ObjectId);
    int id = Convert.ToInt32(value);
    if(tempid == id)
    { 
        //Here is where i want the bitmap.
        System.Drawing.Bitmap img = Images[i].File;
        ...
     }
 }

So within the "if" I want to get a bitmap from the SPListItemCollection, but how do i get that, is it possible? Is there a method on SPListItemCollection I can use, either "File" or "Attachment", or do I have to convert the file to bitmap first?

Much appreciated any help or ideas here. I'm stumped.

1

You can use a MemoryStream to create an Image object from the file:

var imageFile = Images[i].File;
var imageStream = imageFile.OpenBinaryStream();
Image image = Image.FromStream(imageStream, true, true);
imageStream.Dispose();

Depending on what you need to do, this can be done from the Image, like resizing

if (originalImage.Width > MAX_WIDTH)// || originalImage.Height > MAX_HEIGHT)
                {
                    var ratioX = (double)MAX_WIDTH / originalImage.Width;
                    //var ratioY = (double)MAX_HEIGHT / originalImage.Height;
                    var ratio = ratioX;// Math.Min(ratioX, ratioY);

                    var newWidth = (int)(originalImage.Width * ratio);
                    var newHeight = (int)(originalImage.Height * ratio);

                    var newImage = new Bitmap(newWidth, newHeight);
                    Graphics.FromImage(newImage).DrawImage(originalImage, 0, 0, newWidth, newHeight);
                    byte[] smallImage = ImageToByteArray(newImage, originalImage.RawFormat);
                }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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