Как повернуть изображение и увеличить его, используя только один метод? - PullRequest
0 голосов
/ 26 октября 2018

Вот что я делаю для поворота и масштабирования.Но пока, если вращение работает, масштабирование не так, и наоборот.Так как же объединить вращение и масштабирование в одном методе?Я чувствую, что они не могут сосуществовать, используя мой код.

....................................................................................................................................................................

Вот что у меня сейчас:

Рисунок изображения:

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();
    }

1 Ответ

0 голосов
/ 26 октября 2018

Вы можете добавить другое матричное преобразование для масштабирования: matrix.Scale(2, 2, MatrixOrder.Append);

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);

matrix.Scale(2, 2, MatrixOrder.Append);

using (GraphicsPath graphicsPath = new GraphicsPath())
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...