Copied to clipboard

Flag this post as spam?

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


  • Moran 285 posts 934 karma points
    Jun 10, 2013 @ 12:17
    Moran
    0

    creating a new media item

    I am using a file upload control to upload an image to the server, how can I save this image in the site backoffice in the appropriate media folder?

    this is the file upload code behind:

    if (ImageFileUpload.HasFile)
    try
    {
      ImageFileUpload.SaveAs(MapPath("~/media/") +
                             ImageFileUpload.FileName);
                        MessageLabel.Text = "File name: " +
                             ImageFileUpload.PostedFile.FileName + "<br>" +
                             ImageFileUpload.PostedFile.ContentLength + " kb<br>" +
                             "Content type: " +
                             ImageFileUpload.PostedFile.ContentType;
                        uploadSucess = true;
                    }
  • Moran 285 posts 934 karma points
    Jun 11, 2013 @ 10:34
    Moran
    0

    anybody can help with this? I am missing just the part of integrating the image in to my backoffice

  • Moran 285 posts 934 karma points
    Jun 12, 2013 @ 08:45
    Moran
    100

    Found a hint in another post this is my code:

    string savePath = MapPath("~/media/" + fileName);
    string newFolder = MapPath("~/media/" + newMedia.Id);
    string newFile = newFolder + "/" + fileName;
    string extension = fileName.Substring(fileName.LastIndexOf('.') + 1).ToUpper();

    Directory.CreateDirectory(newFolder);
    File.Move(savePath, newFile);


    //Save filename and path to Umrbaco File
    newMedia.getProperty("umbracoFile").Value = "/media/" + newMedia.Id + "/" + fileName;
    //Save type
    newMedia.getProperty("umbracoExtension").Value = extension;
    //Save size FileInfo f = new FileInfo(newFile);
    newMedia.getProperty("umbracoBytes").Value = f.Length;
  • Moran 285 posts 934 karma points
    Jun 12, 2013 @ 08:48
    Moran
    0

    Does anyone knows how to create a thumbnail image? I didn't understand the instructions from the post.

  • Moran 285 posts 934 karma points
    Jun 13, 2013 @ 14:00
    Moran
    0

    Found the solution, it's a bit long but it does the work for me :)


    //Save thumb image
    string fileNameThumb = newFile.Replace("." + extension, "_thumb");
    GenerateThumbnail(image, 100, fileWidth, fileHeight, fileName, extension, fileNameThumb + ".jpg");

    /// <summary>
    /// Generate the thumb image
    /// </summary>
    /// <param name="image">the image created</param>
    /// <param name="maxWidthHeight">image height limit</
    /// <param name="fileWidth">the original image width</param>
    /// <param name="fileHeight">the original image height</param>
    /// <param name="fullFilePath">the image path</param>
    /// <param name="ext">the image extention</param>
    /// <param name="thumbnailFileName">thumbnail file name</param>
    protected void GenerateThumbnail(System.Drawing.Image image, int maxWidthHeight, int fileWidth, int fileHeight, string fullFilePath, string ext, string thumbnailFileName)
    {
    // Generate thumbnail
    float fx = (float)fileWidth / (float)maxWidthHeight;
    float fy = (float)fileHeight / (float)maxWidthHeight;
    // must fit in thumbnail size
    float f = Math.Max(fx, fy); //if (f < 1) f = 1;
    int widthTh = (int)Math.Round((float)fileWidth / f); int heightTh = (int)Math.Round((float)fileHeight / f);

    // fixes for empty width or height
    if (widthTh == 0)
    widthTh = 1;
    if (heightTh == 0)
    heightTh = 1;

    // Create new image with best quality settings
    Bitmap bp = new Bitmap(widthTh, heightTh);
    Graphics g = Graphics.FromImage(bp);
    g.SmoothingMode = SmoothingMode.HighQuality;
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.PixelOffsetMode = PixelOffsetMode.HighQuality;

    // Copy the old image to the new and resized
    Rectangle rect = new Rectangle(0, 0, widthTh, heightTh);
    g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);

    // Copy metadata
    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
    ImageCodecInfo codec = null;
    for (int i = 0; i < codecs.Length; i++)
    {
    if (codecs[i].MimeType.Equals("image/jpeg"))
    codec = codecs[i];
    }

    // Set compresion ratio to 90%
    EncoderParameters ep = new EncoderParameters();
    ep.Param[0] = new EncoderParameter(Encoder.Quality, 90L);

    // Save the new image
    bp.Save(thumbnailFileName, codec, ep);
    bp.Dispose();
    g.Dispose();
    }
  • Sean Mooney 131 posts 158 karma points c-trib
    Jun 13, 2013 @ 14:35
    Sean Mooney
    0

    or you could let Umbraco do everything for you:

    Dim name As String = "ImageName"
    Dim u As New Umbraco.BusinessLogic.User(0) Dim m = Umbraco.cms.businesslogic.media.Media.MakeNew(name, Umbraco.cms.businesslogic.media.MediaType.GetByAlias("Alias"), u, ParentID)
    '--Save the file to disk Dim pf As New Umbraco.cms.businesslogic.media.PostedMediaFile() pf.FileName = name pf.ContentLength = str.Length pf.DisplayName = name pf.InputStream = str pf.ReplaceExisting = True Dim factory As New Umbraco.cms.businesslogic.media.UmbracoImageMediaFactory() factory.DoHandleMedia(m, pf, u)
    '--Add other properties m.getProperty("originalUrl").Value = img.Url m.getProperty("source").Value = img.Source m.Save()
    str is the inputstream from the fileupload control
  • 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