1

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

4

3 回答 3

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();
    }
于 2014-08-21T04:46:24.933 回答
0

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

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

于 2009-08-02T20:45:19.617 回答
0

为了绘制更亮或更暗的图片(或以任何方式更改颜色),您可以使用ColorMatrix,如下所示

于 2009-08-03T13:16:28.520 回答