C# 使用“裁剪”旋转图像

C# 使用“裁剪”旋转图像,c#,javascript,jquery,html,css,C#,Javascript,Jquery,Html,Css,我正在使用裁剪图像。在将图像发送到裁剪之前,是否有可能扩展其功能以旋转图像。像这样的 var angle = 0; $('#button').on('click', function() { angle += 90; $("#image").rotate(angle); }); 这是你的电话号码 我可以设法旋转图像,但裁剪后的图像未处于旋转位置。有什么想法吗? 更新: 现在图像正在旋转。我已经设法通过使用裁剪方法的同一处理程序来保持旋转的图像。但旋转位置的裁剪图像仅在旋转图像旋

我正在使用裁剪图像。在将图像发送到裁剪之前,是否有可能扩展其功能以旋转图像。像这样的

var angle = 0;
$('#button').on('click', function() {
    angle += 90;
    $("#image").rotate(angle);
});
这是你的电话号码 我可以设法旋转图像,但裁剪后的图像未处于旋转位置。有什么想法吗? 更新: 现在图像正在旋转。我已经设法通过使用裁剪方法的同一处理程序来保持旋转的图像。但旋转位置的裁剪图像仅在旋转图像旋转180度时才会出现。当它在90/270中时,图像显然不适合边界框,如果你看到过croppic,我得到一个MemoryOfBond异常

代码如下:

public static Bitmap RotateImage(Bitmap bmpSrc, float theta)
{
    Matrix mRotate = new Matrix();
    mRotate.Translate(bmpSrc.Width / -2, bmpSrc.Height / -2, MatrixOrder.Append);
    mRotate.RotateAt(theta, new System.Drawing.Point(0, 0), MatrixOrder.Append);
    using (GraphicsPath gp = new GraphicsPath())
    {  // transform image points by rotation matrix
        gp.AddPolygon(new System.Drawing.Point[] { new System.Drawing.Point(0, 0), new System.Drawing.Point(bmpSrc.Width, 0), new System.Drawing.Point(0, bmpSrc.Height) });
        gp.Transform(mRotate);
        System.Drawing.PointF[] pts = gp.PathPoints;

        // create destination bitmap sized to contain rotated source image
        Rectangle bbox = boundingBox(bmpSrc, mRotate);
        Bitmap bmpDest = new Bitmap(bbox.Width, bbox.Height);

        using (Graphics gDest = Graphics.FromImage(bmpDest))
        {  // draw source into dest
            Matrix mDest = new Matrix();
            mDest.Translate(bmpDest.Width / 2, bmpDest.Height / 2, MatrixOrder.Append);
            gDest.Transform = mDest;
            gDest.DrawImage(bmpSrc, pts);
            return bmpDest;
        }
    }
}

private static Rectangle boundingBox(Image img, Matrix matrix)
{
    GraphicsUnit gu = new GraphicsUnit();
    Rectangle rImg = Rectangle.Round(img.GetBounds(ref gu));

    // Transform the four points of the image, to get the resized bounding box.
    System.Drawing.Point topLeft = new System.Drawing.Point(rImg.Left, rImg.Top);
    System.Drawing.Point topRight = new System.Drawing.Point(rImg.Right, rImg.Top);
    System.Drawing.Point bottomRight = new System.Drawing.Point(rImg.Right, rImg.Bottom);
    System.Drawing.Point bottomLeft = new System.Drawing.Point(rImg.Left, rImg.Bottom);
    System.Drawing.Point[] points = new System.Drawing.Point[] { topLeft, topRight, bottomRight, bottomLeft };
    GraphicsPath gp = new GraphicsPath(points,new byte[] { (byte)PathPointType.Start,
                      (byte)PathPointType.Line, (byte)PathPointType.Line, 
                      (byte)PathPointType.Line });
    gp.Transform(matrix);
    return Rectangle.Round(gp.GetBounds());
}
我认为主要问题在于边界框

using (Bitmap rotate = new Bitmap(RotateImage(bmp, rotateAngle)))
{
    using (Bitmap cloneRotate = (Bitmap)rotate.Clone()) //getting error here
    {
        //some stuffs
        cloneRotate.save(mycroppedimageurl);
    }
}