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 have a standard list and I have to add a special field:

It should be an upload field or a link to a window including an upload field where the user can upload a picture and select an area to crop, so he is uploading maybe a 1000x800 pic and select just a 300x400 area to use.

After selecting the area the cropped picture is then stored into a picture library and that item is linked to item the user is editing in the standard list (via lookup field maybe).

Is there a solution out there for that or do I have to code that by myself? Is that even possible?

share|improve this question
add comment

2 Answers 2

jCrop is your answer:

public class ImageCrop : LayoutsPageBase
private const string WIDTH = "450";
private const string HEIGHT = "200";
protected Image imgJCrop;
protected Button btnCropImage;
protected Button btnCropImageTop;
string strImageURL;
protected Label lblErr;
protected TextBox txtW;
protected TextBox txtH;
protected Button btnApplyDimensions;

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    this.EnsureChildControls();

    //load jCrop script
    ClientScriptManager sm = Page.ClientScript;
    sm.RegisterHiddenField("jCropX", "0");
    sm.RegisterHiddenField("jCropY", "0");
    sm.RegisterHiddenField("jCropW", WIDTH);
    sm.RegisterHiddenField("jCropH", HEIGHT);

    string strScript = string.Format("<script type='text/javascript'>" +
                                    "var globalX=0;" +
                                    "var globalY=0;" +
                                    "jQuery(document).ready(function() {{" +
                                    "var jcropAPI = $.Jcrop('#{0}',{{onChange: storeCoords}});" +
                                    "jcropAPI.setSelect([0,0,{1},{2}]);" +

                                        "jQuery('#{11}').click(function(e) {{" +
                                        "jcropAPI.setSelect([globalX,globalY,jQuery('#{9}').val(),jQuery('#{10}').val()]); }});" +

                                    "}});" +

                                    "function nothing(e) {{" +
                                    "e.stopPropagation(); e.preventDefault(); return false;" +
                                    "}};" +

                                    "function storeCoords(c) {{" +
                                    "jQuery('#{3}').val(c.x); " +
                                    "jQuery('#{4}').val(c.y);" +
                                    "jQuery('#{5}').val(c.w);" +
                                    "jQuery('#{6}').val(c.h);" +
                                    "jQuery('#{7}').val(c.w);" +
                                    "jQuery('#{8}').val(c.h);" +
        //"globalX = c.x;" + 
        //"globalY=c.y;" + 
                                    "}};" +
                                    "</script>",
                                    imgJCrop.ClientID,
                                    WIDTH,
                                    HEIGHT,
                                    "jCropX",
                                    "jCropY",
                                    "jCropW",
                                    "jCropH",
                                    txtW.ClientID,
                                    txtH.ClientID,
                                    txtW.ClientID,
                                    txtH.ClientID,
                                    btnApplyDimensions.ClientID);
    this.RegisterStartupScript("jCropJS", strScript);

    //diable server side on button click
    btnApplyDimensions.OnClientClick = "return false;";

    //load image
    //SPSite sitecoll = this.Site;
    if (Request.QueryString["ItemUrl"] != null)
    {
        if (this.Web.ServerRelativeUrl != "/")
            {
                strImageURL = this.Web.Url.TrimEnd('/') + Request.QueryString["ItemUrl"].ToString().Replace(this.Web.ServerRelativeUrl, "");
            }
            else
            {
                strImageURL = this.Web.Url.TrimEnd('/') + Request.QueryString["ItemUrl"].ToString();
            }

            imgJCrop.ImageUrl = strImageURL;
    }

}


protected void btnCropImage_Click(object sender, EventArgs e)
{
    int x = Convert.ToInt32(Page.Request["jCropX"]);
    int y = Convert.ToInt32(Page.Request["jCropY"]);
    int w = Convert.ToInt32(Page.Request["jCropW"]);
    int h = Convert.ToInt32(Page.Request["jCropH"]);

    string strListID = Request.QueryString["ListId"];
    //string strWebURL = Request.QueryString["SiteUrl"];
    string strItemID = Request.QueryString["ItemId"];

    lblErr.Style.Add(HtmlTextWriterStyle.Color, "Red");
    lblErr.Style.Add(HtmlTextWriterStyle.FontSize, "11px");
    lblErr.Style.Add(HtmlTextWriterStyle.FontWeight, "bold");
    lblErr.Visible = true;

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        try
        {
            using (SPSite site = new SPSite(this.Site.Url))
            {
                using (SPWeb web = site.OpenWeb(this.Web.ServerRelativeUrl))
                {

                    try
                    {
                        web.AllowUnsafeUpdates = true;

                        SPList list = web.Lists[new Guid(strListID)];
                        SPListItem item = list.Items.GetItemById(Convert.ToInt32(strItemID));
                        SPFile file = item.File;

                        if (list.WorkflowAssociations.Count > 0)
                        {
                            //dont crop as it can effect workflow
                            lblErr.Text = "The document library this image belongs to has workflow enabled. Please disable workflows on this document library to enable cropping";

                            btnCropImage.Enabled = false;
                            btnCropImageTop.Enabled = false;
                            return;
                        }
                        else
                        {
                            lblErr.Visible = false;
                        }

                        if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None)
                        {
                            file.CheckIn("");
                        }

                        file.CheckOut();

                        //use WebRequest to create stream to the document library file
                        WebRequest reqImg = WebRequest.Create(strImageURL);
                        reqImg.UseDefaultCredentials = true;
                        WebResponse imgResponse = reqImg.GetResponse();
                        StreamReader reader = new StreamReader(imgResponse.GetResponseStream());

                        using (System.Drawing.Image imgOriginal = System.Drawing.Image.FromStream(reader.BaseStream, true))
                        {
                            using (System.Drawing.Image imgCropped = new System.Drawing.Bitmap(w, h))
                            {
                                using (System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(imgCropped))
                                {
                                    graphic.SmoothingMode = SmoothingMode.AntiAlias;
                                    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                    graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;

                                    System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, w, h);
                                    graphic.DrawImage(imgOriginal, rect, x, y, w, h, System.Drawing.GraphicsUnit.Pixel);

                                    MemoryStream mstream = new MemoryStream();
                                    imgCropped.Save(mstream, imgOriginal.RawFormat);

                                    MemoryStream msNew = null;
                                    using (msNew = new MemoryStream(mstream.GetBuffer(), 0, mstream.GetBuffer().Length))
                                    {
                                        msNew.Write(mstream.GetBuffer(), 0, mstream.GetBuffer().Length);
                                        list.RootFolder.Files.Add(file.Name, msNew.ToArray(), true);
                                        file.Item.SystemUpdate(false);
                                        file.CheckIn("");
                                    }
                                }
                            }
                        }

                        web.AllowUnsafeUpdates = false;
                    }
                    catch (Exception ex)
                    {
                        lblErr.Visible = true;
                        lblErr.Text = ex.Message;
                        if (web.AllowUnsafeUpdates != false)
                        {
                            web.AllowUnsafeUpdates = false;
                        }
                    }

                }
            }
        }
        catch (Exception ex)
        {
            lblErr.Visible = true;
            lblErr.Text = ex.Message;
        }

    });
}

}

Visit this page for full description and code.

share|improve this answer
add comment

maybe you could use a publishing image field combined with http://spimagefieldupload.codeplex.com

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.