none
Rotating an image but original image still there and corners are cut off RRS feed

  • Question

  • I am trying to rotate an image (1) onto a new image(2).

    image(1) has (w1,h1) and image (2) has (w1,w1). I understand the corners will be cut along the sides but the image is being cut at the top and bottom.

    Image(1) is still in the background unrotated (I don't want) and the rotated one is on top. In image(2) the output shows (w1,w1) in the properties.

    I am unsure where it is going wrong.

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Windows.Forms;
    
    
    namespace rotate_test
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                List<float> degreesToRotate = Enumerable.Range(1, 20).ToList().ConvertAll(x => (float)x);
                foreach (float deg in degreesToRotate)
                {
    
                    Bitmap bmp = new Bitmap(@"C:\ELEC Project\docwindow.bmp");//, PixelFormat.Format32bppArgb);
                    using (bmp)
                    {
                        // Create a graphics object from the bitmap
    
                        using (Graphics gfx = Graphics.FromImage(bmp))
                        {
                            Bitmap newImg = new Bitmap(width: bmp.Width, height: bmp.Width);
    
                            using (Graphics graph = Graphics.FromImage(newImg))
                            {
                                Rectangle ImageSize = new Rectangle(0, 0, bmp.Width, bmp.Width);
                                graph.FillRectangle(Brushes.Black, ImageSize);
                                graph.DrawImage(newImg, 0, 0);
    
                                //gfx.DrawImage(newImg, 0, 0);
                                //newImg.Save(@"C:\ELEC Project\templatetest.bmp");//, ImageFormat.Bmp
                            }
    
                            
                            //move graphics over to rotate on center axis then move back
                            var centX = bmp.Width / 2;
                            var centY = bmp.Height / 2;
                            gfx.TranslateTransform(centX,centY );
                            gfx.RotateTransform(angle: deg);
                            gfx.TranslateTransform(-centX, -centY);
                            //gfx.ResetTransform();
    
                            //construc source bitmap
                            Point srcPoint = new Point(0, 0);
                            Size srcSize = new Size(bmp.Width, bmp.Height);
                            Rectangle srcRec = new Rectangle(location: srcPoint, size: srcSize);
                            gfx.DrawImage(bmp,srcPoint);
    
                            //distination map
                            PointF[] destPoint =
                                {new PointF(0, (newImg.Height / 2) + (bmp.Height / 2)),//UL
                                new PointF(bmp.Width,(newImg.Height / 2) + (bmp.Height / 2)),//UR
                                new PointF(0, (newImg.Height / 2) - (bmp.Height / 2)) };//LL
                                //new Point(bmp.Width,(newImg.Height / 2) - (bmp.Height / 2))};
    
                            //Point wda = new Point(0, (newImg.Height / 2) + (bmp.Height / 2));
                            //Size destSize = new Size(bmp.Width, bmp.Height);
                            //Rectangle destRec = new Rectangle(location: wda, size: destSize);
    
                            using (Graphics grD = Graphics.FromImage(newImg))
                            {
                                grD.DrawImage(bmp, destPoint, srcRec, GraphicsUnit.Pixel);
    
                                // Save the screenshot to the specified location
    
                                //newImg.Save(@"C:\ELEC Project\docwindow" + deg + ".bmp");//, ImageFormat.Bmp
                            }
    
                            newImg.Save(@"C:\ELEC Project\docwindow" + deg + ".bmp");//, ImageFormat.Bmp
                            newImg.Dispose();
                            bmp.Dispose();
                        }
                    }
                }
            }
        }
    }
    

    img(1)<sub></sub><sup></sup><strike></strike>

    IMG(1)

    IMG(2)

    IMG(2)

    Tuesday, January 8, 2019 2:26 AM

Answers

  •  

    Hi Kokoda144,

     

    Thank you for posting here.

     

    According to your issue, you could try this code below.

    private void button1_Click(object sender, EventArgs e)
            {
                List<float> degreesToRotate = Enumerable.Range(1, 20).ToList().ConvertAll(x => (float)x);
                foreach (float deg in degreesToRotate)
                {
    
                    Bitmap bmp = new Bitmap(@"D:\example.bmp");
                    using (bmp)
                    {
                        using (Graphics gfx = Graphics.FromImage(bmp))
                        {
                            double radian = deg* Math.PI/ 180.0;
                            double cos = Math.Cos(radian);
                            double sin = Math.Sin(radian);
                            int w = bmp.Width;
                            int h = bmp.Height;
                            int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
                            int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
                            Bitmap newImg = new Bitmap(W,H);
                            using (Graphics graph = Graphics.FromImage(newImg))
                            {
                                graph.InterpolationMode = InterpolationMode.Bilinear;
                                graph.SmoothingMode = SmoothingMode.HighQuality;
                                Point Offset = new Point((W - w) / 2, (H - h) / 2);
                                Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
                                Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
                                graph.TranslateTransform(center.X, center.Y);
                                graph.RotateTransform(deg);
                                graph.TranslateTransform(-center.X, -center.Y);
                                graph.DrawImage(bmp, rect);
                                graph.ResetTransform();
                            }
                            newImg.RotateFlip(RotateFlipType.Rotate180FlipX);
                            newImg.Save(@"D:\pic\" + deg + ".bmp");
                            newImg.Dispose();
                            bmp.Dispose();
                           
                        }
                    }
                }
                MessageBox.Show("success");
            }

    Test Result: 

    Hope my suggestion could be helpful.

     

    Best regards,

    Jack




    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Wednesday, January 9, 2019 6:56 AM
    Moderator