Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何仅使用一种方法旋转图像并将其放大?_C#_Image Processing - Fatal编程技术网

C# 如何仅使用一种方法旋转图像并将其放大?

C# 如何仅使用一种方法旋转图像并将其放大?,c#,image-processing,C#,Image Processing,下面是我旋转和缩放的步骤。但到目前为止,如果旋转有效,则缩放无效,反之亦然。那么如何在一种方法中结合旋转和缩放呢?我觉得他们不能用我的代码共存 徖 以下是我目前拥有的: 图像绘制: public LayerClass ImageDrawing(LayerClass.Type img, Bitmap bm, Rectangle imgRect, String filepath, int angle, PaintEventArgs e) { bm = ImageClass.G

下面是我旋转和缩放的步骤。但到目前为止,如果旋转有效,则缩放无效,反之亦然。那么如何在一种方法中结合旋转和缩放呢?我觉得他们不能用我的代码共存

以下是我目前拥有的:

图像绘制:

public LayerClass ImageDrawing(LayerClass.Type img, Bitmap bm, Rectangle imgRect, String filepath, int angle, PaintEventArgs e)
    {
        bm = ImageClass.GrayscaleImage(bm);
        bm = MakeTransparentImage(bm);

        e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        bm = RotateImage(bm, angle, imgRect);
        imgRect = new Rectangle((int)(Shape.center.X - (bm.Width / 2)), (int)(Shape.center.Y - (bm.Height / 2)), (int)bm.Width, (int)bm.Height);
        e.Graphics.DrawImage(bm, imgRect);

        this.imageBitmap = bm;
        this.filePath = filePath;
        this.rotationAngle = angle;
        this.location = location;
        this.imageRect = imgRect;
        return new LayerClass(LayerClass.Type.Image, this, filePath, imgRect);
    }
 public static Bitmap RotateImage(Bitmap bitmap, float angle, Rectangle rect)
    {
        Matrix matrix = new Matrix();
        matrix.Translate(bitmap.Width / -2, bitmap.Height / -2, MatrixOrder.Append);
        matrix.RotateAt(angle, new System.Drawing.Point(0, 0), MatrixOrder.Append);
        using (GraphicsPath graphicsPath = new GraphicsPath())
        {
            graphicsPath.AddPolygon(new System.Drawing.Point[] { new System.Drawing.Point(0, 0), new System.Drawing.Point(bitmap.Width, 0), new System.Drawing.Point(0, bitmap.Height) });
            graphicsPath.Transform(matrix);
            System.Drawing.PointF[] points = graphicsPath.PathPoints;

            rect = boundingBox(bitmap, matrix);
            Bitmap resultBitmap = new Bitmap(rect.Width, rect.Height);

            using (Graphics g = Graphics.FromImage(resultBitmap))
            {
                Matrix matrix2 = new Matrix();
                matrix2.Translate(resultBitmap.Width / 2, resultBitmap.Height / 2, MatrixOrder.Append);
                g.Transform = matrix2;
                g.DrawImage(bitmap, points);
                return resultBitmap; 
            }
        }
    }
private void trackBar_ScaleImg_Scroll(object sender, EventArgs e)
    {
        if(rb_BothImage.Checked)
        {
            if (imgRect.Width > imgRect.Height)
            {
                imgRect.Width = trackBar_ScaleImg.Value;
                imgRect.Height = (int)(trackBar_ScaleImg.Value / aspect);

            ImageBitmap = new Bitmap(ImageBitmap, new Size(imgRect.Width, imgRect.Height));
            }
            else if (imgRect.Height > imgRect.Width)
            {
                imgRect.Height = trackBar_ScaleImg.Value; //64mm
                imgRect.Width = (int)(trackBar_ScaleImg.Value / aspect);

            ImageBitmap = new Bitmap(ImageBitmap, new Size(imgRect.Width, imgRect.Height));
            }
            else if (imgRect.Width == imgRect.Height)
            {
                imgRect.Width = trackBar_ScaleImg.Value;
                imgRect.Height = trackBar_ScaleImg.Value;

            }
            imgRect.X = (int)(Shape.center.X - (imgRect.Width / 2));
            imgRect.Y = (int)(Shape.center.Y - (imgRect.Height / 2));

            ImageBitmap = new Bitmap(ImageBitmap, new Size(imgRect.Width, imgRect.Height));
        }
        pictureBox_Canvass.Invalidate();
    }
旋转:

public LayerClass ImageDrawing(LayerClass.Type img, Bitmap bm, Rectangle imgRect, String filepath, int angle, PaintEventArgs e)
    {
        bm = ImageClass.GrayscaleImage(bm);
        bm = MakeTransparentImage(bm);

        e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        bm = RotateImage(bm, angle, imgRect);
        imgRect = new Rectangle((int)(Shape.center.X - (bm.Width / 2)), (int)(Shape.center.Y - (bm.Height / 2)), (int)bm.Width, (int)bm.Height);
        e.Graphics.DrawImage(bm, imgRect);

        this.imageBitmap = bm;
        this.filePath = filePath;
        this.rotationAngle = angle;
        this.location = location;
        this.imageRect = imgRect;
        return new LayerClass(LayerClass.Type.Image, this, filePath, imgRect);
    }
 public static Bitmap RotateImage(Bitmap bitmap, float angle, Rectangle rect)
    {
        Matrix matrix = new Matrix();
        matrix.Translate(bitmap.Width / -2, bitmap.Height / -2, MatrixOrder.Append);
        matrix.RotateAt(angle, new System.Drawing.Point(0, 0), MatrixOrder.Append);
        using (GraphicsPath graphicsPath = new GraphicsPath())
        {
            graphicsPath.AddPolygon(new System.Drawing.Point[] { new System.Drawing.Point(0, 0), new System.Drawing.Point(bitmap.Width, 0), new System.Drawing.Point(0, bitmap.Height) });
            graphicsPath.Transform(matrix);
            System.Drawing.PointF[] points = graphicsPath.PathPoints;

            rect = boundingBox(bitmap, matrix);
            Bitmap resultBitmap = new Bitmap(rect.Width, rect.Height);

            using (Graphics g = Graphics.FromImage(resultBitmap))
            {
                Matrix matrix2 = new Matrix();
                matrix2.Translate(resultBitmap.Width / 2, resultBitmap.Height / 2, MatrixOrder.Append);
                g.Transform = matrix2;
                g.DrawImage(bitmap, points);
                return resultBitmap; 
            }
        }
    }
private void trackBar_ScaleImg_Scroll(object sender, EventArgs e)
    {
        if(rb_BothImage.Checked)
        {
            if (imgRect.Width > imgRect.Height)
            {
                imgRect.Width = trackBar_ScaleImg.Value;
                imgRect.Height = (int)(trackBar_ScaleImg.Value / aspect);

            ImageBitmap = new Bitmap(ImageBitmap, new Size(imgRect.Width, imgRect.Height));
            }
            else if (imgRect.Height > imgRect.Width)
            {
                imgRect.Height = trackBar_ScaleImg.Value; //64mm
                imgRect.Width = (int)(trackBar_ScaleImg.Value / aspect);

            ImageBitmap = new Bitmap(ImageBitmap, new Size(imgRect.Width, imgRect.Height));
            }
            else if (imgRect.Width == imgRect.Height)
            {
                imgRect.Width = trackBar_ScaleImg.Value;
                imgRect.Height = trackBar_ScaleImg.Value;

            }
            imgRect.X = (int)(Shape.center.X - (imgRect.Width / 2));
            imgRect.Y = (int)(Shape.center.Y - (imgRect.Height / 2));

            ImageBitmap = new Bitmap(ImageBitmap, new Size(imgRect.Width, imgRect.Height));
        }
        pictureBox_Canvass.Invalidate();
    }
缩放:

public LayerClass ImageDrawing(LayerClass.Type img, Bitmap bm, Rectangle imgRect, String filepath, int angle, PaintEventArgs e)
    {
        bm = ImageClass.GrayscaleImage(bm);
        bm = MakeTransparentImage(bm);

        e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        bm = RotateImage(bm, angle, imgRect);
        imgRect = new Rectangle((int)(Shape.center.X - (bm.Width / 2)), (int)(Shape.center.Y - (bm.Height / 2)), (int)bm.Width, (int)bm.Height);
        e.Graphics.DrawImage(bm, imgRect);

        this.imageBitmap = bm;
        this.filePath = filePath;
        this.rotationAngle = angle;
        this.location = location;
        this.imageRect = imgRect;
        return new LayerClass(LayerClass.Type.Image, this, filePath, imgRect);
    }
 public static Bitmap RotateImage(Bitmap bitmap, float angle, Rectangle rect)
    {
        Matrix matrix = new Matrix();
        matrix.Translate(bitmap.Width / -2, bitmap.Height / -2, MatrixOrder.Append);
        matrix.RotateAt(angle, new System.Drawing.Point(0, 0), MatrixOrder.Append);
        using (GraphicsPath graphicsPath = new GraphicsPath())
        {
            graphicsPath.AddPolygon(new System.Drawing.Point[] { new System.Drawing.Point(0, 0), new System.Drawing.Point(bitmap.Width, 0), new System.Drawing.Point(0, bitmap.Height) });
            graphicsPath.Transform(matrix);
            System.Drawing.PointF[] points = graphicsPath.PathPoints;

            rect = boundingBox(bitmap, matrix);
            Bitmap resultBitmap = new Bitmap(rect.Width, rect.Height);

            using (Graphics g = Graphics.FromImage(resultBitmap))
            {
                Matrix matrix2 = new Matrix();
                matrix2.Translate(resultBitmap.Width / 2, resultBitmap.Height / 2, MatrixOrder.Append);
                g.Transform = matrix2;
                g.DrawImage(bitmap, points);
                return resultBitmap; 
            }
        }
    }
private void trackBar_ScaleImg_Scroll(object sender, EventArgs e)
    {
        if(rb_BothImage.Checked)
        {
            if (imgRect.Width > imgRect.Height)
            {
                imgRect.Width = trackBar_ScaleImg.Value;
                imgRect.Height = (int)(trackBar_ScaleImg.Value / aspect);

            ImageBitmap = new Bitmap(ImageBitmap, new Size(imgRect.Width, imgRect.Height));
            }
            else if (imgRect.Height > imgRect.Width)
            {
                imgRect.Height = trackBar_ScaleImg.Value; //64mm
                imgRect.Width = (int)(trackBar_ScaleImg.Value / aspect);

            ImageBitmap = new Bitmap(ImageBitmap, new Size(imgRect.Width, imgRect.Height));
            }
            else if (imgRect.Width == imgRect.Height)
            {
                imgRect.Width = trackBar_ScaleImg.Value;
                imgRect.Height = trackBar_ScaleImg.Value;

            }
            imgRect.X = (int)(Shape.center.X - (imgRect.Width / 2));
            imgRect.Y = (int)(Shape.center.Y - (imgRect.Height / 2));

            ImageBitmap = new Bitmap(ImageBitmap, new Size(imgRect.Width, imgRect.Height));
        }
        pictureBox_Canvass.Invalidate();
    }

您可以添加另一个用于缩放的矩阵变换:
matrix.Scale(2,2,MatrixOrder.Append)


我使用trackbar进行缩放,如何实现这一点?