This is a migrated thread and some comments may be shown as answers.

Resize uploaded image

9 Answers 257 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Andy Green
Top achievements
Rank 2
Andy Green asked on 06 Dec 2012, 11:16 AM
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

9 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 06 Dec 2012, 11:46 AM
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.
0
Andy Green
Top achievements
Rank 2
answered on 06 Dec 2012, 02:39 PM
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
0
Andy Green
Top achievements
Rank 2
answered on 10 Dec 2012, 08:22 AM
In addition, I have removed the targetFolder tag from the controls markup, and the file is still removed before the function is called.

Andy
0
Plamen
Telerik team
answered on 10 Dec 2012, 01:07 PM
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.
0
Andy Green
Top achievements
Rank 2
answered on 10 Dec 2012, 02:38 PM
Thanks Plamen

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

Andy
0
Andy Green
Top achievements
Rank 2
answered on 10 Dec 2012, 02:40 PM
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");

}















0
Andy Green
Top achievements
Rank 2
answered on 10 Dec 2012, 04:30 PM
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
0
Plamen
Telerik team
answered on 13 Dec 2012, 01:02 PM
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.
0
Fabio Cirillo
Top achievements
Rank 1
answered on 15 Mar 2013, 05:35 PM

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

Tags
Upload (Obsolete)
Asked by
Andy Green
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Andy Green
Top achievements
Rank 2
Plamen
Telerik team
Fabio Cirillo
Top achievements
Rank 1
Share this question
or