for*_*idt 7 c++ mfc drawing gdi+ line
从我的MFC应用程序中截取的屏幕截图显示了应该平行的第2行.

线条是使用Gdiplus::Graphics::Drawline().
一个小问题是虚线不是直线而是断裂.你注意到了,但这不是什么大问题......
真正的问题是它不一致,而且绘制的线条Gdiplus::DashStyleSolid非常直.
这是GDI +中的错误吗?有解决方法吗?
我尝试制作一个最小的例子,但我正在努力重现它......它可能与非常大的坐标值有关,它们的坐标之间差别不大.这些是一些示例线坐标:
Line 1: PointF(2.21866e+006, 1.40198e+006), PointF(2.21732e+006, 1.40111e+006)
Line 2: PointF(2.21866e+006, 1.40198e+006), PointF(2.21732e+006, 1.40112e+006)
Run Code Online (Sandbox Code Playgroud)
I noticed that the start points are identical but this may be caused by rounding in the debug output...
I could reproduce it now: The sample application was generated as SDI project and the view class inherits CScrollView (code and screenshot below). The "broken line" effect also appears to be related to the pen's thickness. The line runs a bit different with varying thickness but I did not find a value that results in parallel lines all the time.
Vincent Porvik suggests in a comment that this might be the result of rounding errors, since Gdiplus::REAL is defined as float.
I believe this is the case as well...
Do I have any options other than somehow using smaller coordinate values here?
These functions were added/modified (in addition to the GDI+ initialization code):
void CgdiplusLinesView::OnDraw(CDC* pDC)
{
CgdiplusLinesDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
pDC->SetBkMode(TRANSPARENT);
using namespace Gdiplus;
Graphics g(pDC->m_hDC);
g.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
Pen solidPen(Color(0,0,0), 2.f);
Pen dashedPen(Color(0,0,0), 2.f);
dashedPen.SetDashStyle(DashStyleDash);
g.DrawLine(&dashedPen, PointF(4438749.500000, 2805806.500000), PointF(4434280.500000, 2802106.500000));
g.DrawLine(&solidPen, PointF(4438746.500000, 2805809.500000), PointF(4434277.500000, 2802109.500000));
}
void CgdiplusLinesView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
pDC->SetMapMode(MM_ISOTROPIC);
const int mapSizePixels = 8388608;
pDC->SetWindowExt(mapSizePixels, mapSizePixels);
pDC->SetViewportExt(mapSizePixels, mapSizePixels);
pDC->SetWindowOrg(GetScrollPos(SB_HORZ), GetScrollPos(SB_VERT));
//__super::OnPrepareDC(pDC, pInfo);
}
void CgdiplusLinesView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
SetScrollSizes(MM_TEXT, CSize(8388608, 8388608), CSize(1361, 1054));
SetScrollPos(SB_HORZ, 4434897.5);
SetScrollPos(SB_VERT, 2802645.);
}
Run Code Online (Sandbox Code Playgroud)

我不认为这与虚线与实线有关,而是与浮动精度有关。您的坐标似乎是地理坐标。通常的良好做法是将所有坐标存储为双精度,当您需要使用较低精度的库时,请在将所有坐标传递给图形函数之前自行偏移所有坐标。
正确的方法是选择一个接近数据集中的点的参考点(找到的第一个坐标即可),然后将其坐标减去所有其他点。这使数值保持在 0.0 附近,即浮点具有更高的分辨率密度。
| 归档时间: |
|
| 查看次数: |
1263 次 |
| 最近记录: |