C# 面板图形在C中的缩放#
我有一个包含面板的表单,在这个面板中我画了一些形状,比如矩形和圆形,我需要放大这些形状,我看到了一些选项,但大多数都使用PictureBox。我是否应该使用位图创建面板区域作为位图并更改缩放因子??如果我想进行平移,而不是绘制不符合面板尺寸的图像,这是否也会进一步帮助我 这是我的代码的快照C# 面板图形在C中的缩放#,c#,winforms,zooming,panel,C#,Winforms,Zooming,Panel,我有一个包含面板的表单,在这个面板中我画了一些形状,比如矩形和圆形,我需要放大这些形状,我看到了一些选项,但大多数都使用PictureBox。我是否应该使用位图创建面板区域作为位图并更改缩放因子??如果我想进行平移,而不是绘制不符合面板尺寸的图像,这是否也会进一步帮助我 这是我的代码的快照 private void panel1_Paint(object sender, PaintEventArgs e) { Graphics g = panel1.CreateGra
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = panel1.CreateGraphics();
SolidBrush myBrush = new SolidBrush(Color.Black);
Pen p = new Pen(Color.Black);
int RecScale = 1;
foreach (CircuitData.ResistorRow resistorRow in ResistorData.Resistor)
{
RectangleF rec = new RectangleF((float)(resistorRow.CenterX - resistorRow.Length / 2), (float)(resistorRow.CenterY - resistorRow.Width/ 2), (float)resistorRow.Length, (float)resistorRow.Width);
float orientation = 360 - (float)resistorRow.Orientation;
PointF center = new PointF((float)resistorRow.CenterX, (float)resistorRow.CenterY);
PointF[] points = CreatePolygon(rec, center, orientation);
if (!Double.IsNaN(resistorRow.HiX) && !Double.IsNaN(resistorRow.HiY))
{
g.FillEllipse(myBrush, (float)resistorRow.HiX - 5 , (float)resistorRow.HiY - 5, 10, 10);
g.DrawLine(p, new PointF((float)resistorRow.HiX, (float)resistorRow.HiY), center);
}
g.FillPolygon(myBrush, points);
}
}
能否提供示例代码。
非常感谢
Jp由于您是从头开始绘制的,您不能根据缩放因子调整图形大小吗 可以将图形尺寸乘以缩放因子。假设您的缩放因子为:
- 0.5表示50%缩放(这将减小图形大小)
- 100%(实际尺寸)为1.0
- 1.5对于150%(较大尺寸),您可以通过以下方式计算宽度:
以下是按对象缩放图形的方法: 在这里,我们存储缩放级别:
float zoom = 1f;
在这里,我们设置并更新面板:
private void trackBar1_Scroll(object sender, EventArgs e)
{
// for zooming between, say 5% - 500%
// let the value go from 50-50000, and initialize to 100 !
zoom = trackBar1.Value / 100f;
panel1.Invalidate();
}
两个示例屏幕截图:
请注意,这样可以很好地缩放笔的宽度。启用抗锯齿是个好主意:
g.SmoothingMode=SmoothingMode.antialas代码>如果需要,可以缩放图形对象。但是首先在你的绘画活动中修复损坏的代码!!!交换Graphics g=panel1.CreateGraphics()代码>用于图形g=e.图形代码>通过说对象宽度是我代码框中的图形对象g或我正在绘制的每个形状。每个对象。事实上,你需要对每个对象维度都这样做。是否可以放置一个滚动条来查看缩放的内容?@Abhishek,你得到了一个解决方案,可以缩放图片并在面板中显示滚动条(滚动条用于拖动并查看图像的其余部分)更简单的解决方案是将panel1放入启用了autoscroll的panel2中。或者为panel1设置autoscroll和minsize。
float zoom = 1f;
private void trackBar1_Scroll(object sender, EventArgs e)
{
// for zooming between, say 5% - 500%
// let the value go from 50-50000, and initialize to 100 !
zoom = trackBar1.Value / 100f;
panel1.Invalidate();
}