我正在使用 SharpDx 在 WinRt 的 Image 控件中呈现一些形状。如何打印渲染到打印机的内容?
private void RenderGraphics()
{
KSurfaceImageSourceManager _pKSurfaceImageSourceManager = new KSurfaceImageSourceManager();
SurfaceImageSource _pSurfaceImageSource = _pKSurfaceImageSourceManager.NewSurfaceImageSource((int)imageCtrl.Width, (int)imageCtrl.Height);
int _w = 0;
int _h = 0;
_pKSurfaceImageSourceManager.GetSurfaceImageSourceSize(_pSurfaceImageSource, out _w, out _h);
this.imageCtrl.Source = _pSurfaceImageSource;
int _retcode = _pKSurfaceImageSourceManager.ClearSurfaceImageSource(_pSurfaceImageSource, 1, 1, 1, 1);
int _hrenderTarget = 0;
int _offsetx = 0;
int _offsety = 0;
int _surfacewidth = 0;
int _surfaceheight = 0;
try
{
// initiating a Direct2D drawing session: BeginDraw2D
_hrenderTarget = _pKSurfaceImageSourceManager.BeginDraw2D(_pSurfaceImageSource, out _offsetx, out _offsety, out _surfacewidth, out _surfaceheight);
// connecting the _hrenderTarget handle returned by BeginDraw2D to a RenderTarget instance
_pRenderTarget = new RenderTarget(new IntPtr(_hrenderTarget));
// Direct2D drawing session targetting _pRenderTarget
_pRenderTarget.BeginDraw();
SharpDX.Direct2D1.SolidColorBrush _brush = null;
int _left = _offsetx;
int _top = _offsety;
int _right = _surfacewidth;
int _bottom = _surfaceheight;
// border
_brush = new SharpDX.Direct2D1.SolidColorBrush(_pRenderTarget, new SharpDX.Color4(1, 0, 0, 1));
_pRenderTarget.FillRectangle(new SharpDX.RectangleF(_left, _top, _right, _bottom), _brush);
// fill
int _border = 2;
_brush = new SharpDX.Direct2D1.SolidColorBrush(_pRenderTarget, new SharpDX.Color4(1, 1, 1, 1));
_pRenderTarget.FillRectangle(new SharpDX.RectangleF(_left + _border, _top + _border, _right - 2 * _border, _bottom - 2 * _border), _brush);
_pRenderTarget.DrawEllipse(new SharpDX.Direct2D1.Ellipse(new DrawingPointF(50, 80), 50, 50), new SharpDX.Direct2D1.SolidColorBrush(_pRenderTarget, Color.SlateBlue));
}
catch (Exception E)
{
}
finally
{
_pKSurfaceImageSourceManager.EndDraw2D(_pSurfaceImageSource);
}
}
我尝试在不执行这些渲染操作的情况下打印图像控件的内容,它起作用了。当我在调用此方法后尝试相同时,它会打印空白。