使用FileUpload控件上传服务器前调整图片大小

.NET
567
0
0
2022-03-22
标签   .Net图片

原文链接http://weblogs.asp.net/markmcdonnell/archive/2008/03/09/resize-image-before-uploading-to-server.aspx

这是我在一个国外博客上看到的一篇博文。。意思是将客户端上传的图片在上传前调整大小以节省服务端空间但是图片大小仍然上传。 一篇非常棒的article.. 我重新整理了一下。

1.首先创建一个名为"ImageUpload.aspx" Web窗体.

ImageUpload.aspx 代码

<div> 
    <asp:FileUpload ID="FileUpload" runat="server" /> 
    <br /><br /> 
    <asp:Button ID="btnUpload" runat="server" Text="Upload!" OnClick="UploadFile" /> 
    <br /><br /> 
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
    <br /><br /> 
    <asp:Image ID="Image1" ImageUrl="" runat="server" Visible="false" />
</div>

ImageUpload.aspx.cs 代码

protected void UploadFile(object sender, EventArgs e)
{
    //First we check to see if the user has selected a file 
    if (FileUpload.HasFile)
    {
        //Find the fileUpload control 
        string filename = FileUpload.FileName;
        //Check if the directory we want to the image uploaded to actually exists or not 
        if (!Directory.Exists(Server.MapPath(@"./Upload")))
        {
            //if it doesnt then we just crate it before going any futher
            Directory.CreateDirectory(Server.MapPath(@"./Upload"));
        }
        //Specify the upload directory 
        string directory = Server.MapPath(@"./Upload\");
        //Create a bitmap of the content of the fileUpload control in memory
        Bitmap originalBMP = new Bitmap(FileUpload.FileContent);
        int thumbnailSize = 250;
        int newWidth, newHeight;
        if (originalBMP.Width > originalBMP.Height)
        {
            newWidth = thumbnailSize;
            newHeight = originalBMP.Height * thumbnailSize / originalBMP.Width;
        }
        else
        {
            newWidth = originalBMP.Width * thumbnailSize / originalBMP.Height;
            newHeight = thumbnailSize;
        }
        // Create a new bitmap which will hold the previous resized bitmap
        Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
        // Create a graphic based on the new bitmap
        Graphics oGraphics = Graphics.FromImage(newBMP);
        // Set the properties for the new graphic file
        oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        // Draw the new graphic based on the resized bitmap
        oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
        // Save the new graphic file to the server
        newBMP.Save(directory + filename);
        // Once finished with the bitmap objects, we deallocate them.
        originalBMP.Dispose();
        newBMP.Dispose();
        oGraphics.Dispose();
        // Write a message to inform the user all is OK
        Label1.Text = "File Name: <b style='color: red;'>" + filename + "</b><br>";
        Label1.Text += "Content Type: <b style='color: red;'>" + FileUpload.PostedFile.ContentType + "</b><br>";
        Label1.Text += "File Size: <b style='color: red;'>" + FileUpload.PostedFile.ContentLength.ToString() + "</b>";
        // Display the image to the user
        Image1.Visible = true;
        Image1.ImageUrl = @"./Upload/" + filename;
    }
    else
    {
        Label1.Text = "No file uploaded!";
    }
}

在WIN7 + VS 2008调试通过