Click here to Skip to main content
15,914,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a simple image upload script I am using that renames the images and saves it in a folder on my server.

But now I need to also create a smaller version of the image, rename it and save it to the same folder.

I'm new to this kind of coding. I have looked at lots of examples but am unable to get any to work. So I was hoping someone could help me with my code.


Here is my existing code:

JavaScript
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>
<script language="C#"  runat="server">
// UPLOAD ORIGINAL IMAGE WITH NEW FILE NAME
void Button1_Click(object Source, EventArgs e){
string id = Request.QueryString["id"];
Response.Write (id);
    if (File1.PostedFile != null){
        try{
            File1.PostedFile.SaveAs(Server.MapPath("uploads//")+"big"+id+".jpg");
            Span1.InnerHtml = "File uploaded successfully: "+"big"+id+".jpg";
        }
        catch (Exception exc){
            Span1.InnerHtml = "Error saving file" + "big"+id+".jpg" + "" + exc.ToString();
        }
		
    }
	
}
</script>
</head>
<body>
<form enctype="multipart/form-data"  runat="server">
  <p>File to Upload: 
    <input id="File1" type="file"  runat="server">
    <br>
    <input type=button id="Button1" value="Upload"  önServerClick="Button1_Click"  runat="server">
</br></p>
  <p><span id=Span1 style="font: 8pt verdana;"  runat="server" /></p>
</form>
</body>
</html>
Posted
Updated 15-Aug-12 2:53am
v2

C#
protected void UploadBtn_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(MapPath("~/Image/" + FileUpload1.FileName));
            System.Drawing.Image img1 = System.Drawing.Image.FromFile(MapPath("~/Image/") + FileUpload1.FileName);
 
            System.Drawing.Image bmp1 = img1.GetThumbnailImage(75, 75, null, IntPtr.Zero);
            bmp1.Save(MapPath("~/Thumbnail/") + FileUpload1.FileName);
            ThumbnailImage.ImageUrl = "~/Thumbnail/" + FileUpload1.FileName;
           
        }
    }



I have used GetThumbnailImage method of the Image class. This method returns the thumbnail image for the given original image.This could be the best picture quality we can derive with thumbnail i guess.
 
Share this answer
 
I tried that but I'm doing something wrong as its not working.

Sorry but i'm really new to this.


Here is my code:

XML
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>
<script language="C#" runat="server">
// UPLOAD ORIGINAL IMAGE WITH NEW FILE NAME
void Button1_Click(object Source, EventArgs e){
string id = Request.QueryString["id"];
Response.Write (id);
    if (File1.PostedFile != null){
        try{
            File1.PostedFile.SaveAs(Server.MapPath("uploads//")+"big"+id+".jpg");
            Span1.InnerHtml = "File uploaded successfully: "+"big"+id+".jpg";

            System.Drawing.Image img1 = System.Drawing.Image.FromFile(Server.MapPath("uploads//")+"big"+id+".jpg");

            System.Drawing.Image bmp1 = img1.GetThumbnailImage(116, 82, null, IntPtr.Zero);
            bmp1.Save(MapPath("uploads//")+"small"+id+".jpg");
            ThumbnailImage.ImageUrl = "uploads//"+"small"+id+".jpg";
        }
        catch (Exception exc){
            Span1.InnerHtml = "Error saving file" + "big"+id+".jpg" + "" + exc.ToString();
        }

    }

}
</script>
</head>
<body>
<form enctype="multipart/form-data" runat="server">
  <p>File to Upload:
    <input id="File1" type="file" runat="server">
    <br>
    <input type=button id="Button1" value="Upload" OnServerClick="Button1_Click" runat="server">
</p>
  <p><span id=Span1 style="font: 8pt verdana;" runat="server" /></p>
</form>
</body>
</html>




Here is the error i'm getting:


Compiler Error Message: CS0103: The name 'ThumbnailImage' does not exist in the current context
 
Share this answer
 
Just removed the offending line and it seems to be working.

Here is my working code incase anyone is interested:

XML
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>
<script language="C#" runat="server">
// UPLOAD ORIGINAL IMAGE WITH NEW FILE NAME
void Button1_Click(object Source, EventArgs e){
string id = Request.QueryString["id"];
Response.Write (id);
    if (File1.PostedFile != null){
        try{
            File1.PostedFile.SaveAs(Server.MapPath("uploads//")+"big"+id+".jpg");
            Span1.InnerHtml = "File uploaded successfully: "+"big"+id+".jpg";

            System.Drawing.Image img1 = System.Drawing.Image.FromFile(Server.MapPath("uploads//")+"big"+id+".jpg");

            System.Drawing.Image bmp1 = img1.GetThumbnailImage(116, 82, null, IntPtr.Zero);
            bmp1.Save(Server.MapPath("uploads//")+"small"+id+".jpg");
            //ThumbnailImage.ImageUrl = "uploads//"+"small"+id+".jpg";
        }
        catch (Exception exc){
            Span1.InnerHtml = "Error saving file" + "big"+id+".jpg" + "" + exc.ToString();
        }

    }

}
</script>
</head>
<body>
<form enctype="multipart/form-data" runat="server">
  <p>File to Upload:
    <input id="File1" type="file" runat="server">
    <br>
    <input type=button id="Button1" value="Upload" OnServerClick="Button1_Click" runat="server">
</p>
  <p><span id=Span1 style="font: 8pt verdana;" runat="server" /></p>
</form>
</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900