使用C#从图片裁剪跨矩形 [英] Cropping a cross rectangle from image using c#

查看:201
本文介绍了使用C#从图片裁剪跨矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的基本上是从裁剪图像的矩形什么。然而,应当满足一些特殊情况:



<醇>
  • 我想裁剪图像的倾斜矩形

  • 我不想旋转图像和作物矩形:)

  • 如果超过裁剪图像大小,我不想裁剪一个空的背景颜色。



  • 我想从起点回来,将在起点时结束矩形大小完成裁剪。我知道我无法解释,因此,如果我表现出我想要的视觉:





    蓝点是起点那里,而箭头所示方向裁剪。当裁切超过图像的边界,它将回到起点的背面之多,当矩形的宽度和高度完成的矩形的端部将在起点



    此外,这是以前的问题,我问:





    在这个问题,我无法预测可能发生的有关图像尺寸的问题,所以我没有要求它。但现在有情况三,除案例三,这也正是同样的问题。我该怎么做,有什么建​​议?


    解决方案

    什么需要做的是偏移量添加到矩阵排列。在这种情况下,这是我在矩形的一个额外的长度从各侧(总共9矩形)和抵消每次矩阵



    注意,有必要把偏移 0 (原作物)过去,否则你将得到错误的结果。



    另外请注意,如果您指定一个矩形,它比旋转图片,你仍然会得到空区域大。

     公共静态位图CropRotatedRect(位图源,矩形RECT ,浮角度,布尔高品质)
    {
    INT []的偏移= {1,1,0}; //地方0了!
    位图的结果=新位图(rect.Width,rect.Height);使用(图形G = Graphics.FromImage(结果))
    {
    g.InterpolationMode =高质量
    ? InterpolationMode.HighQualityBicubic:InterpolationMode.Default;
    的foreach(在偏移INT X)
    {
    的foreach(在偏移int y)对
    {使用
    (矩阵垫=新的Matrix())
    {
    //创建适当的填料,根据X,Y
    //导致偏移抵消(-1,-1),( - 1,0),( - 1,1)... (0,0)
    mat.Translate(-rect.Location.X - rect.Width * X,-rect.Location.Y - rect.Height * Y);
    mat.RotateAt(角度,rect.Location);
    g.Transform =垫;
    g.DrawImage(源,新点(0,0));
    }
    }
    }
    }
    返回结果;
    }

    要重新创建你的例子:

     位图源=新位图(C:\\mjexample.jpg); 
    位图DEST = CropRotatedRect(源,新的Rectangle(86,182,87,228),-45,真正的);


    What I want to do is basically cropping a rectangle from an image. However, it should satisfy some special cases:

    1. I want to crop an angled rectangle on image.
    2. I don't want to rotate the image and crop a rectangle :)
    3. If cropping exceeds the image size, I don't want to crop an empty background color.

    I want to crop from back of the starting point, that will end at starting point when rectangle size completed. I know I couldn't explain well so if I show what I want visually:

    The blue dot is the starting point there, and the arrow shows cropping direction. When cropping exceeds image borders, it will go back to the back of the starting point as much as, when the rectangle width and height finished the end of the rectangle will be at starting point.

    Besides this is the previous question I asked:

    In this question, I couldn't predict that a problem can occur about image dimensions so I didn't ask for it. But now there is case 3. Except case three, this is exactly same question. How can I do this, any suggestions?

    解决方案

    What needs to be done is to add offsets to the matrix alignment. In this case I am taking one extra length of the rectangle from each side (total 9 rectangles) and offsetting the matrix each time.

    Notice that it is necessary to place offset 0 (the original crop) last, otherwise you will get the wrong result.

    Also note that if you specify a rectangle that is bigger than the rotated picture you will still get empty areas.

    public static Bitmap CropRotatedRect(Bitmap source, Rectangle rect, float angle, bool HighQuality)
    {
        int[] offsets = { -1, 1, 0 }; //place 0 last!
        Bitmap result = new Bitmap(rect.Width, rect.Height);
        using (Graphics g = Graphics.FromImage(result))
        {
            g.InterpolationMode = HighQuality ? InterpolationMode.HighQualityBicubic : InterpolationMode.Default;
            foreach (int x in offsets)
            {
                foreach (int y in offsets)
                {
                    using (Matrix mat = new Matrix())
                    {
                        //create the appropriate filler offset according to x,y
                        //resulting in offsets (-1,-1), (-1, 0), (-1,1) ... (0,0)
                        mat.Translate(-rect.Location.X - rect.Width * x, -rect.Location.Y - rect.Height * y);
                        mat.RotateAt(angle, rect.Location);
                        g.Transform = mat;
                        g.DrawImage(source, new Point(0, 0));
                    }
                }
            }
        }
        return result;
    }
    

    To recreate your example:

    Bitmap source = new Bitmap("C:\\mjexample.jpg");
    Bitmap dest = CropRotatedRect(source, new Rectangle(86, 182, 87, 228), -45, true);
    

    这篇关于使用C#从图片裁剪跨矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆