/// <summary> /// 将指定的页码显示出现 /// </summary> /// <param name="page">要呈现的页码</param> /// <param name="graphics"></param> /// <param name="dpiX"></param> /// <param name="dpiY"></param> /// <param name="bounds"></param> public override void Render(int page, Graphics graphics, float dpiX, float dpiY, Rectangle bounds) { if (graphics == null) { throw new ArgumentNullException("graphics"); } if (_disposed) { throw new ObjectDisposedException(GetType().Name); } float graphicsDpiX = graphics.DpiX; float graphicsDpiY = graphics.DpiY; var dc = graphics.GetHdc(); try { if ((int)graphicsDpiX != (int)dpiX || (int)graphicsDpiY != (int)dpiY) { var transform = new NativeMethods.XFORM { eM11 = graphicsDpiX / dpiX, eM22 = graphicsDpiY / dpiY }; NativeMethods.SetGraphicsMode(dc, NativeMethods.GM_ADVANCED); NativeMethods.ModifyWorldTransform(dc, ref transform, NativeMethods.MWT_LEFTMULTIPLY); } bool success = _file.RenderPDFPageToDC( page, dc, (int)dpiX, (int)dpiY, bounds.X, bounds.Y, bounds.Width, bounds.Height, true /* fitToBounds */, true /* stretchToBounds */, true /* keepAspectRatio */, true /* centerInBounds */, true /* autoRotate */ ); if (!success) { throw new Win32Exception(); } } finally { graphics.ReleaseHdc(dc); } }
internal static extern int AddStroke([In] ContextSafeHandle hRecContext, [In] ref PACKET_DESCRIPTION packetDesc, [In] uint cbPackets, [In] IntPtr pByte, [In, MarshalAs(UnmanagedType.LPStruct)] NativeMethods.XFORM xForm);
/// <summary> /// Retrieve the packet description, packets data and XFORM which is the information the native recognizer needs. /// The method is called from AddStrokes. /// </summary> private void GetPacketData ( Stroke stroke, out MS.Win32.Recognizer.PACKET_DESCRIPTION packetDescription, out int countOfBytes, out IntPtr packets, out NativeMethods.XFORM xForm ) { int i; countOfBytes = 0; packets = IntPtr.Zero; packetDescription = new MS.Win32.Recognizer.PACKET_DESCRIPTION(); Matrix matrix = Matrix.Identity; xForm = new NativeMethods.XFORM((float)(matrix.M11), (float)(matrix.M12), (float)(matrix.M21), (float)(matrix.M22), (float)(matrix.OffsetX), (float)(matrix.OffsetY)); StylusPointCollection stylusPoints = stroke.StylusPoints; if (stylusPoints.Count == 0) { return; //we'll fail when the calling routine sees that packets is IntPtr.Zer } if (stylusPoints.Description.PropertyCount > StylusPointDescription.RequiredCountOfProperties) { // // reformat to X, Y, P // StylusPointDescription reformatDescription = new StylusPointDescription( new StylusPointPropertyInfo[] { new StylusPointPropertyInfo(StylusPointProperties.X), new StylusPointPropertyInfo(StylusPointProperties.Y), stylusPoints.Description.GetPropertyInfo(StylusPointProperties.NormalPressure) }); stylusPoints = stylusPoints.Reformat(reformatDescription); } // // now make sure we only take a finite amount of data for the stroke // if (stylusPoints.Count > MaxStylusPoints) { stylusPoints = stylusPoints.Clone(MaxStylusPoints); } Guid[] propertyGuids = new Guid[] { StylusPointPropertyIds.X, //required index for SPD StylusPointPropertyIds.Y, //required index for SPD StylusPointPropertyIds.NormalPressure }; //required index for SPD Debug.Assert(stylusPoints != null); Debug.Assert(propertyGuids.Length == StylusPointDescription.RequiredCountOfProperties); // Get the packet description packetDescription.cbPacketSize = (uint)(propertyGuids.Length * Marshal.SizeOf(typeof(Int32))); packetDescription.cPacketProperties = (uint)propertyGuids.Length; // // use X, Y defaults for metrics, sometimes mouse metrics can be bogus // always use NormalPressure metrics, though. // StylusPointPropertyInfo[] infosToUse = new StylusPointPropertyInfo[StylusPointDescription.RequiredCountOfProperties]; infosToUse[StylusPointDescription.RequiredXIndex] = StylusPointPropertyInfoDefaults.X; infosToUse[StylusPointDescription.RequiredYIndex] = StylusPointPropertyInfoDefaults.Y; infosToUse[StylusPointDescription.RequiredPressureIndex] = stylusPoints.Description.GetPropertyInfo(StylusPointProperties.NormalPressure); MS.Win32.Recognizer.PACKET_PROPERTY[] packetProperties = new MS.Win32.Recognizer.PACKET_PROPERTY[packetDescription.cPacketProperties]; StylusPointPropertyInfo propertyInfo; for (i = 0; i < packetDescription.cPacketProperties; i++) { packetProperties[i].guid = propertyGuids[i]; propertyInfo = infosToUse[i]; MS.Win32.Recognizer.PROPERTY_METRICS propertyMetrics = new MS.Win32.Recognizer.PROPERTY_METRICS( ); propertyMetrics.nLogicalMin = propertyInfo.Minimum; propertyMetrics.nLogicalMax = propertyInfo.Maximum; propertyMetrics.Units = (int)(propertyInfo.Unit); propertyMetrics.fResolution = propertyInfo.Resolution; packetProperties[i].PropertyMetrics = propertyMetrics; } unsafe { int allocationSize = (int)(Marshal.SizeOf(typeof(MS.Win32.Recognizer.PACKET_PROPERTY)) * packetDescription.cPacketProperties); packetDescription.pPacketProperties = Marshal.AllocCoTaskMem(allocationSize); MS.Win32.Recognizer.PACKET_PROPERTY *pPacketProperty = (MS.Win32.Recognizer.PACKET_PROPERTY *)(packetDescription.pPacketProperties.ToPointer()); MS.Win32.Recognizer.PACKET_PROPERTY *pElement = pPacketProperty; for (i = 0; i < packetDescription.cPacketProperties; i++) { Marshal.StructureToPtr(packetProperties[i], new IntPtr(pElement), false); pElement++; } } // Get packet data int[] rawPackets = stylusPoints.ToHiMetricArray(); int packetCount = rawPackets.Length; if (packetCount != 0) { countOfBytes = packetCount * Marshal.SizeOf(typeof(Int32)); packets = Marshal.AllocCoTaskMem(countOfBytes); Marshal.Copy(rawPackets, 0, packets, packetCount); } }
internal Hdc(Graphics graphics) { this.graphics = graphics; NativeMethods.XFORM xform = new NativeMethods.XFORM(this.graphics.Transform); this.hdc = new HandleRef(this, this.graphics.GetHdc()); //If the function fails, the return value is zero this.oldGraphicsMode = NativeMethods.SetGraphicsMode(this.hdc, NativeMethods.GM_ADVANCED); if (this.oldGraphicsMode == 0) throw new Win32Exception(Marshal.GetLastWin32Error()); //If the function fails, the return value is zero. int result = NativeMethods.SetWorldTransform(this.hdc, xform); if (result == 0) throw new Win32Exception(Marshal.GetLastWin32Error()); IntPtr handle = NativeMethods.GetCurrentObject(this.hdc, NativeMethods.OBJ_PEN); if (handle == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error()); this.oldPen = new HandleRef(this, handle); handle = NativeMethods.GetCurrentObject(this.hdc, NativeMethods.OBJ_EXTPEN); if (handle == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error()); this.oldPenEx = new HandleRef(this, handle); handle = NativeMethods.GetCurrentObject(this.hdc, NativeMethods.OBJ_BRUSH); if (handle == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error()); this.oldBrush = new HandleRef(this, handle); }
/// <summary> /// Renders a page of the PDF document to the provided graphics instance. /// </summary> /// <param name="page">Number of the page to render.</param> /// <param name="graphics">Graphics instance to render the page on.</param> /// <param name="dpiX">Horizontal DPI.</param> /// <param name="dpiY">Vertical DPI.</param> /// <param name="bounds">Bounds to render the page in.</param> /// <param name="flags">Flags used to influence the rendering.</param> public void Render(int page, Graphics graphics, float dpiX, float dpiY, Rectangle bounds, PdfRenderFlags flags) { if (graphics == null) throw new ArgumentNullException("graphics"); if (_disposed) throw new ObjectDisposedException(GetType().Name); float graphicsDpiX = graphics.DpiX; float graphicsDpiY = graphics.DpiY; var dc = graphics.GetHdc(); try { if ((int)graphicsDpiX != (int)dpiX || (int)graphicsDpiY != (int)dpiY) { var transform = new NativeMethods.XFORM { eM11 = graphicsDpiX / dpiX, eM22 = graphicsDpiY / dpiY }; NativeMethods.SetGraphicsMode(dc, NativeMethods.GM_ADVANCED); NativeMethods.ModifyWorldTransform(dc, ref transform, NativeMethods.MWT_LEFTMULTIPLY); } var point = new NativeMethods.POINT(); NativeMethods.SetViewportOrgEx(dc, bounds.X, bounds.Y, out point); bool success = _file.RenderPDFPageToDC( page, dc, (int)dpiX, (int)dpiY, 0, 0, bounds.Width, bounds.Height, FlagsToFPDFFlags(flags) ); NativeMethods.SetViewportOrgEx(dc, point.X, point.Y, out point); if (!success) throw new Win32Exception(); } finally { graphics.ReleaseHdc(dc); } }