C#-将pictureBox中的坐标平移到中心\绘制坐标系\坐标轴

 在pictureBox中绘制图像,将坐标轴平移到中心(默认在pictureBox的左上角),并绘制坐标轴

--该段代码可以放在pictureBox的Paint事件里

Graphics myGraohics = e.Graphics;
//坐标系变换-先平移后缩放
myGraphics.TranslateTransform(pictureBox1.Width / 2, pictureBox1.Height / 2, MatrixOrder.Prepend);  //平移变换
myGraphics.ScaleTransform(1, -1, MatrixOrder.Prepend);  //继续进行缩放变换

//绘制平移、缩放后的坐标系,画笔采用蓝色-此时坐标原点已经到中心
Pen myPen = new Pen(Color.Black,3);

myPen.EndCap = LineCap.ArrowAnchor;  //设置线段终点的样式
myGraphics.DrawLine(myPen, -pictureBox1.Width / 2, 0, pictureBox1.Width / 2, 0); //绘制X轴
myGraphics.DrawLine(myPen, 0, -pictureBox1.Height / 2, 0, pictureBox1.Height / 2); //绘制Y轴

#region 绘刻度线
myPen.EndCap = LineCap.NoAnchor;
int k = pictureBox1.Width / 50;
for (int i = 0; i <= k; i++) //X轴刻度线
            {
                myGraphics.DrawLine(myPen, 50 * i, 0, 50 * i, 15);
                myGraphics.DrawLine(myPen, -50 * i, 0, -50 * i, 15);
            }
k = pictureBox1.Height / 100;
for (int i = 0; i <= k; i++) //Y轴刻度线
            {
                myGraphics.DrawLine(myPen, 0, 50 * i, 10, 50 * i);
                myGraphics.DrawLine(myPen, 0, -50 * i, 10, -50 * i);
            }
#endregion

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值