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


原文链接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调试通过



转载于:https://www.cnblogs.com/loveasm/articles/1552474.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值