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

  • Thread starter Thread starter Kokoda144
  • Start date Start date
K

Kokoda144

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



1378102.jpg<sub></sub><sup></sup><strike></strike>

IMG(1)

1378103.jpg

IMG(2)

Continue reading...
 
Back
Top