Commit a0d26577 LN

旋转问题修改

1 个父辈 94ce7964
......@@ -19,6 +19,9 @@ namespace TSA_V
private string fileName = "";
private bool update = false;
public Image lastImage = null;
private int LastWidth = 0;
private int LastHeight = 0;
private float targetAngle = 0;
/// <summary>
/// 选择图片的方式0=新增电路板。1=元器件图片
......@@ -40,7 +43,10 @@ namespace TSA_V
lastImage = myImage;
this.picViewer.Image = myImage;
lastImage =(Image) myImage.Clone();
LastWidth = lastImage.Width;
LastHeight= lastImage.Height;
targetAngle = 0;
img.Dispose();
btnOK.Focus();
}
......@@ -108,40 +114,23 @@ namespace TSA_V
// 旋转图片90度
RotateImage(v);
}
//private void RotateImage(float angle)
//{
// update = true;
// Image originalImage = (Image)picViewer.Image.Clone();
// // 创建一个新的 Bitmap 用于存储旋转后的图片
// Bitmap bmp = new Bitmap(originalImage.Width, originalImage.Height);
// // 设置绘图的旋转角度和中心点
// Graphics g = Graphics.FromImage(bmp);
// g.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
// g.RotateTransform(angle);
// g.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);
// // 绘制旋转后的图片
// g.DrawImage(originalImage, new Point(0, 0));
// g.Dispose();
// // 更新 PictureBox 中的图片
// picViewer.Image = bmp;
// // 更新旋转后的图片
// //rotatedImage = bmp;
//}
private void RotateImage(float angle)
{
targetAngle += angle;
update = true;
Image originalImage = (Image)picViewer.Image.Clone();
//Image originalImage = (Image)picViewer.Image.Clone();
Image originalImage = (Image)lastImage.Clone();
// 计算旋转后包含整个图像的最小矩形
RectangleF rotatedRect = new RectangleF(0, 0, originalImage.Width, originalImage.Height);
PointF[] points = new PointF[] { new PointF(rotatedRect.Left, rotatedRect.Top), new PointF(rotatedRect.Right, rotatedRect.Top), new PointF(rotatedRect.Left, rotatedRect.Bottom) };
RectangleF rotatedRect = new RectangleF(0, 0,LastWidth, LastHeight);
//RectangleF rotatedRect = new RectangleF(0, 0, originalImage.Width, originalImage.Height);
PointF[] points = new PointF[] { new PointF(rotatedRect.Left, rotatedRect.Top), new PointF(rotatedRect.Right, rotatedRect.Top) ,
new PointF(rotatedRect.Left, rotatedRect.Bottom), new PointF(rotatedRect.Right, rotatedRect.Bottom) };
Matrix matrix = new Matrix();
matrix.RotateAt(angle, new PointF(rotatedRect.Width / 2, rotatedRect.Height / 2));
float pointA = targetAngle % 360;
matrix.RotateAt(pointA, new PointF(rotatedRect.Width / 2, rotatedRect.Height / 2));
matrix.TransformPoints(points);
rotatedRect = RectangleF.FromLTRB(points.Min(p => p.X), points.Min(p => p.Y), points.Max(p => p.X), points.Max(p => p.Y));
......@@ -151,7 +140,7 @@ namespace TSA_V
// 设置绘图的旋转角度和中心点
Graphics g = Graphics.FromImage(bmp);
g.TranslateTransform(rotatedRect.Width / 2, rotatedRect.Height / 2);
g.RotateTransform(angle);
g.RotateTransform(pointA);
g.TranslateTransform(-originalImage.Width / 2, -originalImage.Height / 2);
// 绘制旋转后的图片
......@@ -167,12 +156,6 @@ namespace TSA_V
}
/// <summary>
/// 获取单通道的掩模图片
/// </summary>
/// <param name="img"></param>
/// <param name="path"></param>
/// <returns></returns>
public Image GetRoiMask(Image img, GraphicsPath path)
{
if (path != null)
......@@ -244,6 +227,11 @@ namespace TSA_V
Image cutImage = CutImage(originalImage, path, true);
picViewer.Image = cutImage;
picViewer.SelectionRegion = null;
lastImage = (Image)cutImage.Clone();
LastWidth = lastImage.Width;
LastHeight = lastImage.Height;
targetAngle = 0;
}
}
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!