Hola a todos, estoy tratando de rotar una imagen en un Graphics. Si miren el codigo siguiente:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace rotarImagen
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap bm = new Bitmap(200, 200);
Bitmap figura = new Bitmap(100, 100);
PointF[] p = { new PointF(0,0), new PointF(100, 0), new PointF(0, 100) };
private void Form1_Load(object sender, EventArgs e)
{
figura.SetResolution(300, 300);
colorear(figura, Color.Red);
bm.SetResolution(300, 300);
colorear(bm, Color.White);
p = new PointF[] {
new PointF(200/2-100/2, 200 / 2 - 100 / 2),
new PointF(200 / 2 - 100 / 2 + 100, 200 / 2 - 100 / 2),
new PointF(200 / 2 - 100 / 2, 200 / 2 - 100 / 2+100) };
dibujar();
}
void colorear(Bitmap imagen, Color color)
{
for (int x = 0; x < imagen.PhysicalDimension.Width; x++)
{
for (int y = 0; y < imagen.PhysicalDimension.Height; y++)
{
imagen.SetPixel(x, y, color);
}
}
}
void dibujar()
{
Image im = new Bitmap(bm);
Graphics g = Graphics.FromImage(im);
//g.TranslateTransform(-(200 / 2), -(200 / 2));
g.RotateTransform(trackBar1.Value);
g.TranslateTransform(-100, -100);
g.DrawImage(new Bitmap(figura), p, new RectangleF(new PointF(0, 0), figura.PhysicalDimension), GraphicsUnit.Pixel);
g.Dispose();
pictureBox1.Image = im;
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
if (figura != null)
{
dibujar();
}
}
//void rotar()
//{
// double R = Math.Sqrt(Math.Pow(100,2)+Math.Pow(100,2))/2;
// PointF Pul = p[0];
// PointF Pur = p[1];
// PointF Pll = p[2];
// Pul = new PointF((float)(R * Math.Cos(0 + Math.Acos(Pul.X / R))), (float)(R * Math.Sin(0 + Math.Asin(Pul.Y / R))));
// Pur = new PointF((float)(R * Math.Cos(0 + Math.Acos(Pur.X / R))), (float)(R * Math.Sin(0 + Math.Asin(Pur.Y / R))));
// Pll = new PointF((float)(R * Math.Cos(0 + Math.Acos(Pll.X / R))), (float)(R * Math.Sin(0 + Math.Asin(Pll.Y / R))));
// p = new PointF[] { Pul, Pur, Pll };
// dibujar();
//}
//void rotar2()
//{
// double R = Math.Sqrt(Math.Pow(100, 2) + Math.Pow(100, 2)) / 2;
// PointF Pul = p[0];
// PointF Pur = p[1];
// PointF Pll = p[2];
// //cuando rotamos PulX = 1/PurX y PulY = 1/PurY; PulX = 1/Pll y Pul = Pll en sentido de proporcionalidad
// Pul.X = (float)(R * Math.Cos(0 + Math.Acos(Pul.X / R)));
// Pul.Y = (float)(R * Math.Sin(0 + Math.Asin(Pul.Y / R)));
// Pur.X = (float)(R * Math.Cos(0 + Math.Acos(Pur.X / R)));
// Pur.Y = (float)(R * Math.Sin(0 + Math.Asin(Pur.Y / R)));
// Pll.X = (float)(R * Math.Cos(0 + Math.Acos(Pll.X / R)));
// Pll.Y = (float)(R * Math.Sin(0 + Math.Asin(Pll.Y / R)));
// //Pul = new PointF((float)(R * Math.Cos(0 + Math.Acos(Pul.X / R))), (float)(R * Math.Sin(0 + Math.Asin(Pul.Y / R))));
// //Pur = new PointF((float)(R * Math.Cos(0 + Math.Acos(Pur.X / R))), (float)(R * Math.Sin(0 + Math.Asin(Pur.Y / R))));
// //Pll = new PointF((float)(R * Math.Cos(0 + Math.Acos(Pll.X / R))), (float)(R * Math.Sin(0 + Math.Asin(Pll.Y / R))));
// p = new PointF[] { Pul, Pur, Pll };
// dibujar();
//}
}
}
explico:
tenemos un PictureBox para visualizar con zoom en centrado y un TrakBar para rotar con minimo -270 y maximo 270 y su valor inicial en 0.
tengo dos bitmap uno que es el fondo bm y la imagen a rotar figura.
el bm es de color blanco y la figura es de color rojo.
en el procedimiento dibujar() esta mi problema miren el siguiente video donde muestro con mas detalles lo que sucede con g.RotateTransform(trackBar1.Value) y que sucede cuando comentamos g.TranslateTransform(-100, -100)... cuando yo solo quiero que rote la
figura en el mismo lugar pero solo lo hace descomentando g.TranslateTransform(-100, -100)???
video
gracias de antemano.