Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的裁剪逻辑有什么问题?_C#_Graphics - Fatal编程技术网

C# 我的裁剪逻辑有什么问题?

C# 我的裁剪逻辑有什么问题?,c#,graphics,C#,Graphics,我试图通过从中间裁剪将肖像图像裁剪成特定的风景尺寸,但我似乎做错了。现在我有以下代码: // Check the orientation if(original.Height > original.Width) { var bmp = new Bitmap(original); int cropHeightOffset = (desiredHeight - original.Height) / 2; var totalArea = new Rectangle(new

我试图通过从中间裁剪将肖像图像裁剪成特定的风景尺寸,但我似乎做错了。现在我有以下代码:

// Check the orientation
if(original.Height > original.Width) {
    var bmp = new Bitmap(original);
    int cropHeightOffset = (desiredHeight - original.Height) / 2;
    var totalArea = new Rectangle(new Point(0, 0),
                                  new Size(original.Width, original.Height));
    var cropArea = new Rectangle(new Point(0, cropHeightOffset),
                                 new Size(desiredWidth, desiredHeight));

    // Create new blank image of the desired size
    var newBmp = new Bitmap(bmp, new Size(desiredWidth, desiredHeight));

    // Crop image
    var cropper = Graphics.FromImage(newBmp);

    // Draw it
    cropper.DrawImage(bmp, totalArea, cropArea, GraphicsUnit.Pixel);

    // Save
    original.Dispose();
    newBmp.Save(imagePath, ImageFormat.Jpeg);
}
发生这种情况时,它基本上会调整图像的大小(从而使其变形),而不是裁剪图像

如果我在我的
croper.DrawImage
调用中切换
totalArea
cropera
,则它会从底部进行裁剪,但会将图像循环两次(但大小仍然正确)

我完全不知道如何正确地做这件事


编辑:下面是一些我尝试过的例子。有些东西我没有得到,我只是不知道是什么

使用Oliver的代码:

            var targetArea = new Rectangle(new Point(0, 0), new Size(desiredWidth, desiredHeight));
            var cropArea = new Rectangle(new Point(0, cropHeightOffset), new Size(desiredWidth, desiredHeight));

            ...

            // Draw it
            cropper.DrawImage(bmp, targetArea, cropArea, GraphicsUnit.Pixel);
给我

给我


原始图像是:

您必须指定目标区域,而不是总区域:

var newSize = new Size(desiredWidth, desiredHeight);
var targetArea = new Rectangle(new Point(0, 0), newSize);
var cropArea = new Rectangle(new Point(0, cropHeightOffset), newSize);  
...
cropper.DrawImage(bmp, targetArea, cropArea, GraphicsUnit.Pixel); 

我可能错了,但是从MSDN文档来看,我认为
DrawImage
的第二个参数应该是目标位图上的区域,因此它应该具有裁剪区域的高度和宽度。这不是我在上面发布的代码中告诉它的吗?不是;你把原始区域作为第二个参数,目标区域作为第三个参数。只有当我有偏移量为0,0的croparea时,这才有效。当我加入
cropHeightOffset
时,图像会循环。在这两个区域使用cropHeighOffset也不起作用,这只会导致图像的大小调整。我用示例更新了这个问题,说明了我从你的代码中得到了什么。我在减法中失败,我的偏移量是向后计算的(应该是原始的-期望的)
var newSize = new Size(desiredWidth, desiredHeight);
var targetArea = new Rectangle(new Point(0, 0), newSize);
var cropArea = new Rectangle(new Point(0, cropHeightOffset), newSize);  
...
cropper.DrawImage(bmp, targetArea, cropArea, GraphicsUnit.Pixel);