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

时间:2018-10-26 09:27:35

标签: c# image-processing

这就是我要做的旋转和缩放。但是到目前为止,如果旋转有效,则缩放比例无效,反之亦然。那么,如何在一种方法中结合使用旋转和缩放?我觉得他们无法使用我的代码共存。

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

这是我现在拥有的:

图像绘制:

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 :(得分:1)

您可以添加另一个矩阵缩放比例:Databse rabbits_enc.sqlite3 already exists Databse rabbits.sqlite3 already exists 0: Start! Db: rabbits_enc.sqlite3, Encripted, key len=16 0: Connect. Elapsed: 0.00016079703345894814 sec 0: Provide Key. Elapsed: 0.00215048500103876 sec 0: Read 1 records from table 'version'. Elapsed: 0.4296091449796222 sec 0: Read 100 records from table 'account'. Elapsed: 0.0009567929664626718 sec 0: done. 1: Start! Db: rabbits_enc.sqlite3, Encripted, key len=16 1: Connect. Elapsed: 7.332500535994768e-05 sec 1: Provide Key. Elapsed: 0.00037083501229062676 sec 1: Read 100 records from table 'account'. Elapsed: 0.4182819949928671 sec 1: Read 1 records from table 'version'. Elapsed: 0.0005165199982002378 sec 1: done. 2: Start! Db: rabbits_enc.sqlite3, Encripted, key len=16 2: Connect. Elapsed: 9.809300536289811e-05 sec 2: Provide Key. Elapsed: 0.0019192049512639642 sec 2: Read 100 records from table 'account'. Elapsed: 0.4121257350197993 sec 2: Read 100 records from table 'account'. Elapsed: 0.0008492250344716012 sec 2: done. 3: Start! Db: rabbits.sqlite3, Not encripted 3: Connect. Elapsed: 7.215503137558699e-05 sec 3: Skip Provide Key. Elapsed: 0.0002521659480407834 sec 3: Read 1 records from table 'version'. Elapsed: 0.0035479930229485035 sec 3: Read 100 records from table 'account'. Elapsed: 0.000983492995146662 sec 3: done. 4: Start! Db: rabbits.sqlite3, Not encripted 4: Connect. Elapsed: 7.175595965236425e-05 sec 4: Skip Provide Key. Elapsed: 0.004018213017843664 sec 4: Read 100 records from table 'account'. Elapsed: 0.0010135580087080598 sec 4: Read 1 records from table 'version'. Elapsed: 0.0014616100233979523 sec 4: done. 5: Start! Db: rabbits.sqlite3, Not encripted 5: Connect. Elapsed: 7.912697037681937e-05 sec 5: Skip Provide Key. Elapsed: 0.0003501430037431419 sec 5: Read 100 records from table 'account'. Elapsed: 0.0007411669939756393 sec 5: Read 100 records from table 'account'. Elapsed: 0.000722763012163341 sec 5: done.

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