Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bo Damgaard Mortensen 712 posts 1189 karma points
    Dec 26, 2010 @ 12:54
    Bo Damgaard Mortensen
    0

    Programatically upload image/media item - what am I missing?

    Hi fellow devs,

    I'm trying to upload an image (System.Drawing.Image) to Umbraco, but when I'm running the code, the only thing that gets uploaded is the thumbnail of the image. I have checked and double-checked the path to the created media item and it should be correct! It feels like I have missed something, somewhere. Any help/hint on this is greatly appreciated!

    My code:

    protected Media UmbracoSaveCroppedImage(System.Drawing.Image croppedImage)
            {         
                string imageName = "crop_" + imgName.Text.ToLower();
                string mediaRootPath = "C:/umbracotest/media/";
                Media m = Media.MakeNew(imageName, MediaType.GetByAlias("image"), User.GetUser(0), 1053);
                string storagePath = mediaRootPath + m.Id.ToString();
                System.IO.Directory.CreateDirectory(storagePath);
                string fullFilePath = storagePath + "\\" + imageName;
    
                string orgExt = ((string)imageName.Substring(imageName.LastIndexOf(".") + 1, imageName.Length - imageName.LastIndexOf(".") - 1));
                orgExt = orgExt.ToLower();
                string ext = orgExt.ToLower();
                try
                {
                    m.getProperty("umbracoExtension").Value = ext;
                }
                catch { }
    
                try
                {
                    int imgBytes;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        croppedImage.Save(ms, croppedImage.RawFormat);
                        imgBytes = ms.Capacity;
                    }
                    m.getProperty("umbracoBytes").Value = imgBytes.ToString();
                }
                catch { }
    
                if (",jpeg,jpg,gif,bmp,png,tiff,tif,".IndexOf("," + ext + ",") > 0)
                {
                    int fileWidth;
                    int fileHeight;
    
                    fileWidth = 125;
                    fileHeight = 100;
    
                    try
                    {
                        m.getProperty("umbracoWidth").Value = fileWidth.ToString();
                        m.getProperty("umbracoHeight").Value = fileHeight.ToString();
                    }
                    catch { }
    
                    string fileNameThumb = fullFilePath.Replace("." + orgExt, "_thumb");
                    generateThumbnail(croppedImage, 100, fileWidth, fileHeight, fullFilePath, ext, fileNameThumb + ".jpg");
    
                    croppedImage.Dispose();
    
                    string mediaPath = "/media/" + m.Id.ToString() + "/" + imageName;
    
                    m.getProperty("umbracoFile").Value = mediaPath;
                    m.XmlGenerate(new XmlDocument());
                }
                return m;
            }

    Thanks a lot in advance!

    Merry xmas!

  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Dec 26, 2010 @ 13:32
    Jan Skovgaard
    0

    Hi Bo

    I'm asuming you're using a form to submit the image. Does it have the "accept" attribute set were you specify the mime-type of the content you want to upload? It's a long shot but I remember having kind of the same problem with some PDF upload using the API a year ago or something.

    /Jan

  • Bo Damgaard Mortensen 712 posts 1189 karma points
    Dec 26, 2010 @ 15:03
    Bo Damgaard Mortensen
    0

    Hi Jan,

    Thanks for your reply!

    What I am trying to do is to make an image cropper to the frontend so the following steps will be like this:

    1. Upload the original image to umbraco using an Upload asp.net control (works just fine, the image gets uploaded etc)
    2. On postback: set an asp:Image to the uploaded picture and envoke the cropping tool (using jCrop for this)
    3. When the cropping area is selected and the Crop button (regular asp:Button) is clicked, upload the cropped image and delete the original one.
    My btnCrop_Click event looks like this:
    protected void btnCrop_Click(object sender, EventArgs e)
    {
        string ImageName = imgUrl.Text;
    
            int w = Convert.ToInt32(W.Value);
            int h = Convert.ToInt32(H.Value);
            int x = Convert.ToInt32(X.Value);
            int y = Convert.ToInt32(Y.Value);
    
            byte[] CropImage = Crop(ImageName, w, h, x, y);
            using (MemoryStream ms = new MemoryStream(CropImage, 0, CropImage.Length))
            {
                ms.Write(CropImage, 0, CropImage.Length);
                    using (System.Drawing.Image CroppedImage = System.Drawing.Image.FromStream(ms, true))
                    {
                        string SaveTo = imgName.Text;
                        CroppedImage.Save(ms, CroppedImage.RawFormat);
                        Media m = UmbracoSaveCroppedImage(CroppedImage);
                        pnlCrop.Visible = false;
                        pnlCropped.Visible = true;
                        imgCropped.ImageUrl = m.getProperty("umbracoFile").Value.ToString();
                    }
            }
    }
    Where I call the method UmbracoSaveCroppedImage(System.Drawing.Image image); I posted in my first post here.
    Is it possible at all the grab a regular System.Drawing.Image from the frontend (my asp:Image control) and upload the image to umbraco, or do I have to use an Upload control for this?
    Thanks again,
    Bo

     

  • Bo Damgaard Mortensen 712 posts 1189 karma points
    Dec 26, 2010 @ 15:19
    Bo Damgaard Mortensen
    0

    Right, it works now ;)

    Sometimes it helps to take a 10 min break away from the screen!

    What I needed was to call the save method on the croppedImage: croppedImage.Save(fullFilePath);

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies