Resize uploaded image

Thread is closed for posting
10 posts, 0 answers
  1. Andy Green
    Andy Green avatar
    342 posts
    Member since:
    Aug 2008

    Posted 06 Dec 2012 Link to this post

    Hi

    Simple one this (I hope) but can't figure it out.

    I want to resize the uploaded image and overwrite the uploaded file (in the target folder) with the resized one.

    Is there any code available on how to do this?

    Andy
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 06 Dec 2012 Link to this post

    Hi,

    Try the following code snippet to resize uploaded images.

    ASPX:
    <telerik:RadUpload ID="RadUpload1" runat="server" ></telerik:RadUpload>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

    C#:
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (UploadedFile file in RadUpload1.UploadedFiles)
        {
            string target = Server.MapPath("~/upload");
            if (!Directory.Exists(target))
            {
                Directory.CreateDirectory(target);
            }
            file.SaveAs(Path.Combine(target, file.GetName()));
            using (Bitmap originalImage = new Bitmap(file.InputStream))
            {
                int width = newwidth;
                int height = (originalImage.Height * newwidth) / originalImage.Width;
                Bitmap thumbnail = new Bitmap(width, height);
                using (Graphics g = Graphics.FromImage((System.Drawing.Image)thumbnail))
                    g.DrawImage(originalImage, 0, 0, width, height);
                string thumbnailFileName = Path.Combine(target,string.Format("{0}_bthumb{1}", file.GetNameWithoutExtension(), file.GetExtension()));
                thumbnail.Save(thumbnailFileName);
            }
        }
    }

    Please take a look into this for more information.

    Regards,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Andy Green
    Andy Green avatar
    342 posts
    Member since:
    Aug 2008

    Posted 06 Dec 2012 Link to this post

    Thanks Shinu

    The file.saveas is failing. I had this with code I put together from the link you supplied.

    Could not find file 'C:\InTouch\Activity App\into-activity-v6.2\Web\App_Data\RadUploadTemp\qryh2plj.ija'.

    The file is in the folder following upload, but on click, it disappears, so by the time the file.saveas is fired the flie is gone.

    Andy
  5. Andy Green
    Andy Green avatar
    342 posts
    Member since:
    Aug 2008

    Posted 10 Dec 2012 Link to this post

    In addition, I have removed the targetFolder tag from the controls markup, and the file is still removed before the function is called.

    Andy
  6. Plamen
    Admin
    Plamen avatar
    2729 posts

    Posted 10 Dec 2012 Link to this post

    Hello Andy,

     
    Here is a sample web page where this functionality is implemented and is working properly. Please review it and let me know if you have further questions.

    Regards,
    Plamen
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  7. Andy Green
    Andy Green avatar
    342 posts
    Member since:
    Aug 2008

    Posted 10 Dec 2012 Link to this post

    Thanks Plamen

    Its exacly the same, I have also tried removing the AJAX manager and the client side scripts.

    Andy
  8. Andy Green
    Andy Green avatar
    342 posts
    Member since:
    Aug 2008

    Posted 10 Dec 2012 Link to this post

    This is my code:

    <telerik:RadAsyncUpload
                       ID="ruKioskAreaImage"
                       runat="server"
                       AutoAddFileInputs="true"
                       MaxFileInputsCount="1"
                       InitialFileInputsCount="1"
                       MultipleFileSelection="Disabled"
                       AllowedFileExtensions="jpg,jpeg"
                       OnClientValidationFailed="validationFailed"
                       OnClientFileUploadRemoved="fileRemoved"
                       InputSize="24"
                       ControlObjectsVisibility="None"
                       OnFileSelected="ruKioskAreaImage_FileSelected" 
                       Width="230px"
                       EnableEmbeddedSkins="False" Skin="Activity"
                       >
                       </telerik:RadAsyncUpload>

    function validationFailed(sender, eventArgs) {

    $find(

    "<%= AjaxManager.ClientID %>").ajaxRequest("SizeValidation");

    }

     

    function fileRemoved(sender, eventArgs) {

    $find(

    "<%= AjaxManager.ClientID %>").ajaxRequest("FileRemoved");

    }















  9. Andy Green
    Andy Green avatar
    342 posts
    Member since:
    Aug 2008

    Posted 10 Dec 2012 Link to this post

    I think I have this working OK, but would appreciate some clrification:

    In the sample code, I had to remove the first file.SaveAs(Path.Combine(target, file.GetName())).
    As this was causing the file to be moved.

    thumbnail.Save(thumbnailFileName);
    this is then used to same the file

    Is this correct?

    Andy
  10. Plamen
    Admin
    Plamen avatar
    2729 posts

    Posted 13 Dec 2012 Link to this post

    Hi Andy,

    Unfortunately I could not quite understand what is was the code that worked for you from the explanation provided but if this is what you were finally trying to achieve I suppose it is correct.

    Regards,
    Plamen
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  11. Fabio Cirillo
    Fabio Cirillo avatar
    180 posts
    Member since:
    Jan 2010

    Posted 15 Mar 2013 Link to this post

    hi,
    sorry if I intrude in his speech, there is also a way to compress the image resized without losing quality? Perchp I noticed that the code you posted Andy works perfectly except that it has a large image 180x180 jpeg size on my hard = 5.80 kb once resized to 128x128 its size on disk is 16.1 kb, why?

    hello

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017