【问题标题】:Crop a picture using a rectangle使用矩形裁剪图片
【发布时间】:2009-08-02 20:22:55
【问题描述】:

我想在 C# 中裁剪图像。与大多数照片编辑软件一样,我想使用可以通过鼠标调整大小和重新定位的矩形框。另外,我想知道如何突出显示裁剪区域,如this photo所示。

【问题讨论】:

  • 你在这里找什么?从更大的图像中裁剪一部分的实际方法?或者像您链接的图片一样显示您的裁剪选择的方式?
  • 我已经对裁剪有了一些想法。我想帮助如何显示图片中的裁剪选择。我还移动了裁剪选择。

标签: c# .net image gdi+ desktop-application


【解决方案1】:

您的图片链接不再可用。

因此,假设在面板中您的图片框带有要裁剪的图像。

首先,您需要为鼠标操作创建事件处理程序,以便能够绘制您希望裁剪的矩形区域:

private void picBox_MouseDown(object sender, MouseEventArgs e)
    {
        Cursor = Cursors.Default;
        if (Makeselection)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    Cursor = Cursors.Cross;
                    cropX = e.X;
                    cropY = e.Y;

                    cropPen = new Pen(Color.Crimson, 1);
                    cropPen.DashStyle = DashStyle.Solid;
                }
                picBox.Refresh();
            }
            catch (Exception ex)
            {
            }
        }
    }

    private void picBox_MouseUp(object sender, MouseEventArgs e)
    {
        if (Makeselection)
        {
            Cursor = Cursors.Default;
        }
    }

    private void picBox_MouseMove(object sender, MouseEventArgs e)
    {
        Cursor = Cursors.Default;
        if (Makeselection)
        {
            picBox.Cursor = Cursors.Cross;
            try
            {
                if (picBox.Image == null)
                    return;


                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    picBox.Refresh();
                    cropWidth = e.X - cropX;
                    cropHeight = e.Y - cropY;
                    picBox.CreateGraphics().DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight);
                }
            }
            catch (Exception ex)
            {
            }
        }
    }

    private void picBox_MouseLeave(object sender, EventArgs e)
    {
        tabControl.Focus();
    }

    private void picBox_MouseEnter(object sender, EventArgs e)
    {
        picBox.Focus();
    }

现在,来了裁剪图片的按钮点击功能:

 private void btnCrop_Click_1(object sender, EventArgs e)
    {
        Cursor = Cursors.Default;

        try
        {
            if (cropWidth < 1)
            {
                return;
            }
            Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
            //First we define a rectangle with the help of already calculated points
            Bitmap OriginalImage = new Bitmap(picBoxScreenshot.Image, picBoxScreenshot.Width, picBoxScreenshot.Height);
            //Original image
            Bitmap _img = new Bitmap(cropWidth, cropHeight);
            // for cropinfo image
            Graphics g = Graphics.FromImage(_img);
            // create graphics
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            //set image attributes
            g.DrawImage(OriginalImage, 0, 0, rect, GraphicsUnit.Pixel);

            picBox.Image = _img;
            picBox.Width = _img.Width;
            picBox.Height = _img.Height;
            PictureBoxLocation();
            cropWidth = 0;
        }
        catch (Exception ex){}
    }

 private void PictureBoxLocation()
    {
        int _x = 0;
        int _y = 0;
        if (panel1.Width > picBox.Width)
        {
            _x = (panel1.Width - picBox.Width) / 2;
        }
        if (panel1.Height > picBox.Height)
        {
            _y = (panel1.Height - picBox.Height) / 2;
        }
        picBox.Location = new Point(_x, _y);
        picBox.Refresh();
    }

【讨论】:

    【解决方案2】:

    为了使图片更亮或更暗(或以任何方式改变颜色),您可以使用 ColorMatrix,例如 this

    【讨论】:

    • 我知道颜色矩阵的使用,但我知道只在所​​有图像上使用颜色矩阵。我们如何在图像的部分使用它?
    • 将其绘制为五个单独的矩形(五次调用 .DrawImage)。您可以将图片分成四个矩形,覆盖所选内容之外的所有内容,一个矩形本身就是所选内容。
    【解决方案3】:

    选择框的外部似乎覆盖了一个黑色图像,其 alpha 约为 30%。为此,您只需将每个像素移出内容区域并在其顶部绘制一个带有 30% alpha 的黑色像素。这将产生所需的变暗效果。

    至于如何在 C# 中动态获取矩形。

    【讨论】:

    • 感谢尼克贝拉迪的帮助。问题是,如果我逐个处理一个像素,则需要很长时间。有没有像颜色矩阵这样的快速方法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多