예제 #1
0
        // Return a bitmap rotated around its center.
        public static Bitmap RotateBitmap(Bitmap bm, float angle)
        {
            // Make a Matrix to represent rotation
            // by this angle.
            System.Drawing.Drawing2D.Matrix rotate_at_origin = new System.Drawing.Drawing2D.Matrix();
            rotate_at_origin.Rotate(angle);

            // Rotate the image's corners to see how big
            // it will be after rotation.
            PointF[] points =
            {
                new PointF(0,                0),
                new PointF(bm.Width,         0),
                new PointF(bm.Width, bm.Height),
                new PointF(0,        bm.Height),
            };
            rotate_at_origin.TransformPoints(points);
            float xmin, xmax, ymin, ymax;

            GetPointBounds(points, out xmin, out xmax,
                           out ymin, out ymax);

            // Make a bitmap to hold the rotated result.
            int    wid    = (int)Math.Round(xmax - xmin);
            int    hgt    = (int)Math.Round(ymax - ymin);
            Bitmap result = new Bitmap(wid, hgt);

            // Create the real rotation transformation.
            Matrix rotate_at_center = new Matrix();

            rotate_at_center.RotateAt(angle,
                                      new PointF(wid / 2f, hgt / 2f));

            // Draw the image onto the new bitmap rotated.
            using (Graphics gr = Graphics.FromImage(result))
            {
                // Use smooth image interpolation.
                gr.InterpolationMode = InterpolationMode.High;

                // Clear with the color in the image's upper left corner.
                gr.Clear(bm.GetPixel(0, 0));

                //// For debugging. (It's easier to see the background.)
                //gr.Clear(Color.LightBlue);

                // Set up the transformation to rotate.
                gr.Transform = rotate_at_center;

                // Draw the image centered on the bitmap.
                int x = (wid - bm.Width) / 2;
                int y = (hgt - bm.Height) / 2;
                gr.DrawImage(bm, x, y);
            }

            // Return the result bitmap.
            return(result);
        }
예제 #2
0
        private static void DrawFillPattern(Graphics g, FillPattern fillPattern)
        {
            Stopwatch sw = Stopwatch.StartNew();

            if (fillPattern == null)
            {
                return;
            }

            float matrixScale = 1000;

            try
            {
                var width = 30;

                var height = 30;

                var rect = new System.Drawing.Rectangle(0, 0, (int)width, (int)height);

                var rect_border = new System.Drawing.Rectangle(0, 0, (int)width - 1, (int)height - 1);
                Pen myPen       = new Pen(System.Drawing.Color.FromArgb(0, 255, 220), 1);

                g.DrawRectangle(myPen, rect_border);

                var centerX = (rect.Left + rect.Left + rect.Width) / 2;

                var centerY = (rect.Top + rect.Top + rect.Height) / 2;

                g.TranslateTransform(centerX, centerY);

                var rectF = new System.Drawing.Rectangle(-1, -1, 2, 2);

                g.FillRectangle(System.Drawing.Brushes.Transparent, rectF);

                g.ResetTransform();

                var fillGrids = fillPattern.GetFillGrids();

                foreach (var fillGrid in fillGrids)
                {
                    var degreeAngle = (float)fillGrid.Angle * 180 / Math.PI;

                    var pen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(0, 255, 220))
                    {
                        Width = 1f / matrixScale
                    };

                    float dashLength = 1;

                    var segments = fillGrid.GetSegments();

                    if (segments.Count > 0)
                    {
                        pen.DashPattern = segments
                                          .Select(s => Math.Max(float.Epsilon, Convert.ToSingle(s)))
                                          .ToArray();

                        dashLength = pen.DashPattern.Sum();
                    }

                    g.ResetTransform();

                    var rotateMatrix = new System.Drawing.Drawing2D.Matrix();
                    rotateMatrix.Rotate((float)degreeAngle);

                    var matrix = new System.Drawing.Drawing2D.Matrix(1, 0, 0, -1,
                                                                     centerX, centerY);

                    matrix.Scale(matrixScale, matrixScale);

                    matrix.Translate((float)fillGrid.Origin.U,
                                     (float)fillGrid.Origin.V);

                    var backMatrix = matrix.Clone();
                    backMatrix.Multiply(rotateMatrix);
                    matrix.Multiply(rotateMatrix);

                    var offset = (-10) * dashLength;
                    matrix.Translate(offset, 0);
                    backMatrix.Translate(offset, 0);

                    bool   moving_forward = true;
                    bool   moving_back    = true;
                    int    safety         = 500;
                    double alternator     = 0;
                    while (moving_forward || moving_back) // draw segments shifting and offsetting each time
                    {
                        var rectF1 = new RectangleF(-2 / matrixScale, -2 / matrixScale, 4 / matrixScale, 4 / matrixScale);

                        if (moving_forward && LineIntersectsRect(matrix, rect))
                        {
                            g.Transform = matrix;
                            g.DrawLine(pen, new PointF(0, 0), new PointF(100, 0));
                        }
                        else
                        {
                            moving_forward = false;
                        }

                        if (moving_back && LineIntersectsRect(backMatrix, rect))
                        {
                            g.Transform = backMatrix;
                            g.DrawLine(pen, new PointF(0, 0), new PointF(100, 0));
                        }
                        else
                        {
                            moving_back = false;
                        }

                        if (safety == 0)
                        {
                            break;
                        }
                        else
                        {
                            --safety;
                        }

                        matrix.Translate((float)fillGrid.Shift, (float)fillGrid.Offset);
                        backMatrix.Translate(-(float)fillGrid.Shift, -(float)fillGrid.Offset);

                        alternator += fillGrid.Shift;
                        if (Math.Abs(alternator) > Math.Abs(offset))
                        {
                            matrix.Translate(offset, 0);
                            backMatrix.Translate(offset, 0);
                            alternator = 0d;
                        }
                    }
                }

                sw.Stop();
                g.ResetTransform();

                Pen p = new Pen(System.Drawing.Color.Teal);
                p.Width = 1f / matrixScale;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }