【问题标题】:ASP.NET error when uploading, renaming and resizing an image上传、重命名和调整图像大小时出现 ASP.NET 错误
【发布时间】:2013-05-30 22:57:57
【问题描述】:

下午,我正在尝试使用 asp.net FileUpload 控件上传文件。在保存图像之前,我需要使用用户 GUID 重命名文件,然后将图像大小调整为 128 像素。

但是,当它进行保存时,我收到错误 GDI+ 中发生一般错误。 我不知道如何对其进行排序。

有人可以查看我的代码并提供一些指导

protected void btnUpload_Click(object sender, EventArgs e)
    {
        Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey; 

        string directory = Server.MapPath("imgs/users");
        string fileExt = Path.GetExtension(fuSample.PostedFile.FileName);
        string fileName = userGuid + fileExt;

        //Check File ext, make sure its an image!
        if (fileExt == ".jpeg" || fileExt == ".jpg" || fileExt == ".png" || fileExt == ".gif")
        {
            //Now we check to make sure its less than 600kb
            if (fuSample.PostedFile.ContentLength < 614400)
            {
                //Delete an existing file.
                if (File.Exists(directory + fileName))
                    File.Delete(directory + fileName);

                // Create a bitmap of the content of the fileUpload control in memory
                Bitmap originalBMP = new Bitmap(fuSample.FileContent);

                // Calculate the new image dimensions
                int origWidth = originalBMP.Width;
                int origHeight = originalBMP.Height;
                int sngRatio = origWidth / origHeight;
                int newWidth = 128;
                int newHeight = newWidth / sngRatio;

                // 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();


                //Success! 

            }
            else
            {
                //error here, img too big
            }
        }
        else
        {
            notifybar.Attributes.Add("style", "display:block;");
            notifybar.Attributes.Add("class", "failed");
            notifyText.Text = "Valid Image files only please! (.png, .jpg, .jpeg, or .gif)";
        }
    }

【问题讨论】:

    标签: asp.net file-upload image-resizing file-rename


    【解决方案1】:

    这可能像文件覆盖冲突一样简单。然而,这段代码中有许多危险的错误,而且它并没有非常有效地使用 RAM。

    我会建议一种不同的方法(使用ImageResizer NuGet 包):

    var j = new ImageJob(fuSample,"~/imgs/users/<guid>.<ext>", 
      new ResizeSettings("maxwidth=128;maxheight=128"));
    j.Build();
    string imageUrl = PathUtils.GuessVirtualPath(j.FinalPath)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      • 2015-05-27
      • 2016-05-03
      • 1970-01-01
      • 2010-09-20
      • 2014-06-26
      • 1970-01-01
      相关资源
      最近更新 更多