C#:GDI+;图像裁剪

C#:GDI+;图像裁剪,c#,gdi+,C#,Gdi+,我有一个图像。我想从图像的左边裁剪10像素,从右边裁剪10像素。我使用了下面的代码 string oldImagePath="D:\\RD\\dotnet\\Images\\photo1.jpg"; Bitmap myOriginalImage = (Bitmap)Bitmap.FromFile(oldImagePath); int newWidth = myOriginalImage.Width; int newHeight = myOriginalImage.

我有一个图像。我想从图像的左边裁剪10像素,从右边裁剪10像素。我使用了下面的代码

    string oldImagePath="D:\\RD\\dotnet\\Images\\photo1.jpg";
    Bitmap myOriginalImage = (Bitmap)Bitmap.FromFile(oldImagePath);
    int newWidth = myOriginalImage.Width;
    int newHeight = myOriginalImage.Height;
    Rectangle cropArea = new Rectangle(10,0, newWidth-10, newHeight);

    Bitmap target = new Bitmap(cropArea.Width, cropArea.Height);
    using (Graphics g = Graphics.FromImage(target))
    {
        g.DrawImage(myOriginalImage,cropArea);
    }

    target.Save("D:\\RD\\dotnet\\Images\\test.jpg");

但这并没有给我预期的结果。这将输出一个从右侧裁剪10像素的图像和一个调整大小的图像。我认为它不是在调整宽度,而是在调整宽度。因此图像被缩小(按宽度)。有人能纠正我吗?提前谢谢

好的,我完全无法解释这一点,但请稍候:

DrawImage函数需要图像的位置及其位置。你需要第二个位置来裁剪旧的和新的,而不是相反

这是完全不可理解的,但这里是代码

g.DrawImage(myOriginalImage, -cropArea.X, -cropArea.Y);
我希望这能比我解释得更多。

我猜是这句话

Rectangle cropArea = new Rectangle(10,0, newWidth-10, newHeight);
应该是

Rectangle cropArea = new Rectangle(10,0, newWidth-20, newHeight);
将新矩形的宽度设置为每边比原来的-10小20


一些指示会给你带来什么样的结果,这将有助于你确认这一点。

你的新宽度应该减少两倍的裁剪边距,因为你将从两边切掉这个数量

接下来,在将图像绘制到新图像中时,以负偏移量绘制图像。这会导致您不感兴趣的区域被剪掉

int cropX = 10;
Bitmap target = new Bitmap(myOriginalImage.Width - 2*cropX, myOriginalImage.Height);
using (Graphics g = Graphics.FromImage(target))
{
    g.DrawImage(myOriginalImage, -cropX, 0);
}

科里·罗斯是对的。或者,可以沿负x轴平移并以0.0、0.0进行渲染。应该产生相同的结果

using (Graphics g = Graphics.FromImage(target))
{
    g.TranslateTransform(-cropX, 0.0f);
    g.DrawImage(myOriginalImage, 0.0f, 0.0f);
}

您需要使用重载来指定目标矩形和源矩形

下面是一个交互式表单,使用表单上的图片框。它允许您拖动图像。我建议将图片框设为100 x 100,并具有更大的图像,例如您使用alt-prtscr捕获的全屏窗口

class Form1 : Form
{
    // ...
    Image i = new Bitmap(@"C:\Users\jasond\Pictures\foo.bmp");
    Point lastLocation = Point.Empty;
    Size delta = Size.Empty;
    Point drawLocation = Point.Empty;
    bool dragging = false;
    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            if (!dragging)
            {
                lastLocation = e.Location;
                dragging = true;
            }
            delta = new Size(lastLocation.X - e.Location.X, lastLocation.Y - e.Location.Y);
            lastLocation = e.Location;
            if (!delta.IsEmpty)
            {
                drawLocation.X += delta.Width;
                drawLocation.Y += delta.Height;
                pictureBox1.Invalidate();
            }
        }
        else
        {
            dragging = false;
        }
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Rectangle source = new Rectangle(drawLocation,pictureBox1.ClientRectangle.Size);
        e.Graphics.DrawImage(i,pictureBox1.ClientRectangle,source, GraphicsUnit.Pixel);
    }
    //...

它在哪方面没有给你预期的结果?你看到了什么?我正在调整大小的图像(缩小了宽度)是的,但是结果是出乎意料的,因为它只在左手边减少了10px?如果是这样的话,那么对我所指示的行的更改应该可以解决这个问题。我使用的重载具有签名DrawImage(图像img、矩形目标、矩形源、图形单位),因为您要绘制固定大小的备用图像,所以使用(0,0)-(宽度、高度)作为目标矩形。源矩形将是(10,10)-(目的地。宽度,目的地。高度)。呃,我误读了你的原始帖子,请不要在源位置将Y偏移10。