【问题标题】:System.OutOfMemoryException when cropping bitmap in C#在 C# 中裁剪位图时出现 System.OutOfMemoryException
【发布时间】:2017-06-12 08:12:12
【问题描述】:

我正在尝试使用 ASP 文件上传控件上传图像,但将其从中间裁剪为正方形。这是我的代码,

 public void CropImage(string imagePath)
    {
        //string imagePath = @"C:\Users\Admin\Desktop\test.jpg";
        Bitmap croppedImage;
        int x, y;
        // Here we capture the resource - image file.
        using (var originalImage = new Bitmap(imagePath))
        {
        if (originalImage.Width > originalImage.Height) {
            x = 0;
            y = (originalImage.Width / 2) - (originalImage.Height / 2);
        }
        else if (originalImage.Width < originalImage.Height)
        {
            y = 0;
            x = (originalImage.Height / 2) - (originalImage.Width / 2);
        }
        else { 
            y=x=0;
        }
            int sqrWidth = (originalImage.Width >= originalImage.Height) ? originalImage.Height : originalImage.Width;
            Rectangle crop = new Rectangle(x, y, sqrWidth, sqrWidth);

            // Here we capture another resource.
            croppedImage = originalImage.Clone(crop, originalImage.PixelFormat);

        } // Here we release the original resource - bitmap in memory and file on disk.

        // At this point the file on disk already free - you can record to the same path.
        croppedImage.Save(imagePath, ImageFormat.Jpeg);

        // It is desirable release this resource too.
        croppedImage.Dispose();
    }
    //Updates an agent in the database.
    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        AgentController agentController = new AgentController();
        AgentContactController agentContactController = new AgentContactController();
        CityController cityController = new CityController();
        DistrictController districtController = new DistrictController();
        ProvinceController provinceController = new ProvinceController();
        CountryController countryController = new CountryController();
        InsurancePortalContext context = new InsurancePortalContext();



        string folderPath = Server.MapPath("~/AdvisersImages/");

        //Check whether Directory (Folder) exists.
        if (!Directory.Exists(folderPath))
        {
            //If Directory (Folder) does not exists. Create it.
            Directory.CreateDirectory(folderPath);
        }

        //Save the File to the Directory (Folder).
        string FilePath = folderPath + Path.GetFileName(ImageUploadUpdate.PostedFile.FileName);

        if (!File.Exists(FilePath))
        {
            ImageUploadUpdate.SaveAs(FilePath);
            CropImage(FilePath);
        }

但是我遇到了内存不足的异常,

croppedImage = originalImage.Clone(crop, originalImage.PixelFormat);

如果我不使用 CropImage 函数中的 x,y 值设置条件块和/或仅按原样保存上传的图像,则不会出现此异常。

提前致谢

【问题讨论】:

标签: c# asp.net bitmap


【解决方案1】:

正如我在 cmets 中提到的。 .Clone() 如果您的裁剪矩形超出图像范围,则抛出 OutOfMemoryException。你可以在这里阅读它:
https://msdn.microsoft.com/de-de/library/ms141944(v=vs.110).aspx

考虑您的代码具有以下大小的输入图像:
宽度:5px
高度:10px

这是你的代码。

if(originalImage.Width > originalImage.Height)
{
   x = 0;
   y = (originalImage.Width / 2) - (originalImage.Height / 2);
}
else if(originalImage.Width < originalImage.Height)
{
   y = 0;
   x = (originalImage.Height / 2) - (originalImage.Width / 2);
}
else
{
   y = x = 0;
}

int sqrWidth = (originalImage.Width >= originalImage.Height) ? originalImage.Height : originalImage.Width;
Rectangle crop = new Rectangle(x, y, sqrWidth, sqrWidth);

5x10 图像将属于 else if 案例。
y 设置为 0,x 设置为 ((10 / 2) = 5) - (5 / 2) = 2) = 3
然后sqrWidth 设置为 10

接下来您尝试克隆以下矩形:
x = 3,y = 0,w = 10,h = 10

瞧,您的矩形超出了图像的范围。你需要修正你的裁剪逻辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 2016-08-28
    • 2011-01-01
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多