1

画像のサイズを変更してトリミングするこの小さな方法がありますが、何時間も作業した後、突然 OutOfMemory Exception が発生し始めました。私は何を間違っていますか?例外がオンになっていると思いますreturn bmp.Clone(cropArea, bmp.PixelFormat);

    private static Bitmap Resize(Bitmap image, int width, int height)
    {
        double scaleH = (double)height / image.Height;
        double scaleW = (double)width / image.Width;
        double scale = 1.0;
        if (image.Width * scaleH >= width)
            scale = scaleH;
        else if (image.Height * scaleW >= height)
            scale = scaleW;

        var scaleWidth = (int)(image.Width * scale);
        var scaleHeight = (int)(image.Height * scale);

        using (var bmp = new Bitmap((int)scaleWidth, (int)scaleHeight))
        {
            using (var graph = Graphics.FromImage(bmp))
            {
                graph.DrawImage(image, new Rectangle(0, 0, scaleWidth, scaleHeight));
            }
            int xStart = (bmp.Width - width) / 2;
            int yStart = (bmp.Height - height) / 2;
            Rectangle cropArea = new Rectangle(xStart, yStart, width, height);
            return bmp.Clone(cropArea, bmp.PixelFormat);
        }
    }

解決策は丸めの問題でした。切り抜きの四角形が画像自体よりも大きかった

    var scaleWidth = (int)Math.Ceiling(image.Width * scale);
    var scaleHeight = (int)Math.Ceiling(image.Height * scale);
4

1 に答える 1