wpf 标尺 刻度尺 适用于图像缩放

xaml部分主要代码:

<Canvas x:Name="cvRuler" Margin="0" Background="WhiteSmoke" TextBlock.Foreground="Black"/>

后台主要代码

复制代码
 1         //画标尺
 2         private void DrawRule()
 3         {
 4 
 5             if (cvRuler.Children != null)
 6             {
 7                 cvRuler.Children.Clear();
 8             }
 9 
10             System.Windows.Shapes.Line _line;
11             TextBlock _textBlock;
12             
13             var xScale =你的缩放倍数;
14 
15             int _lineIndex = 0;
16             double _width = cvRuler.ActualWidth;//展示部分总pixel
17             string _unit = "(cm)";
18             float _inch = (float)5195/600;//dpi为600时,对应总英寸
19             float _cm = (float)(_inch*2.54);//英寸换算成cm
20             double _pixelPerline = _width/_cm/10*xScale;//刻度尺每cm对应多少pixel。分成十份,再乘以缩放倍数
21             for (double i = 0; i < _width; i += _pixelPerline)
22             {
23                 _line = new System.Windows.Shapes.Line();
24                 _line.X1 = i;
25                 _line.X2 = i;
26                 _line.Y1 = 0;
27                 _line.StrokeThickness = 1;
28                 if (_lineIndex % 10 == 0)//十份,最长的刻度线
29                 {
30                     _line.Stroke = Brushes.DimGray;
31                     _line.Y2 = 25;
32 
33                     _textBlock = new TextBlock();
34                     _textBlock.Text = (_lineIndex/10).ToString()+(_lineIndex==0 ? _unit : "");
35                     _textBlock.FontSize = 8;
36                     _textBlock.HorizontalAlignment = HorizontalAlignment.Left;
37                     _textBlock.VerticalAlignment = VerticalAlignment.Top;
38                     _textBlock.Margin = new Thickness(i+2, 20, 0, 0);
39                     cvRuler.Children.Add(_textBlock);
40                 }
41                 else if (_lineIndex % 5 == 0)//中间的刻度线
42                 {
43                     _line.Stroke = Brushes.Gray;
44                     _line.Y2 = 20;
45                 }
46                 else
47                 {
48                     _line.Stroke = Brushes.Gray;
49                     _line.Y2 = 15;
50                 }
51                 cvRuler.Children.Add(_line);
52 
53                 _lineIndex++;
54             }
55         }
复制代码

 

posted @   dyfisgod  阅读(79)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示