J-P*_*J-P 3 c# zoom panel winforms
我有一个包含面板的表单,在这个面板中我绘制形状,如矩形和圆形,我需要放大这些形状,我看到几个选项,但大多数使用PictureBox.我应该使用位图创建面板区域作为位图并更改缩放系数?? 如果我想要进行平移并且不能在面板尺寸中绘制图像,这对我的帮助也会更大.
这是我的代码的快照
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);
}
}
Run Code Online (Sandbox Code Playgroud)
可以提供示例代码.非常感谢
J.P
这是一种通过缩放Graphics对象来缩放绘图的方法:
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.ScaleTransform(zoom, zoom);
// some demo drawing:
Rectangle rect = panel1.ClientRectangle;
g.DrawEllipse(Pens.Firebrick, rect);
using (Pen pen = new Pen(Color.DarkBlue, 4f)) g.DrawLine(pen, 22, 22, 88, 88);
}
Run Code Online (Sandbox Code Playgroud)
这里我们存储缩放级别:
float zoom = 1f;
Run Code Online (Sandbox Code Playgroud)
在这里我们设置它并更新Panel:
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();
}
Run Code Online (Sandbox Code Playgroud)
两个示例屏幕截图:
请注意,这也很好地缩放了笔的宽度.打开抗锯齿将是一个好主意..:g.SmoothingMode = SmoothingMode.AntiAlias;
| 归档时间: |
|
| 查看次数: |
9115 次 |
| 最近记录: |