/// <summary> /// Save to PDF /// </summary> /// <param name="stream">Strema to write to</param> public void SaveToPdf(Stream stream) { //NativeMethods.cairo_select_font_face(_rsvgHandle, "serif", 0, 0); NativeMethods.RsvgDimensionData dim = new NativeMethods.RsvgDimensionData(); NativeMethods.rsvg_handle_get_dimensions(_rsvgHandle, ref dim); CairoWriteFunction cwf = new CairoWriteFunction(WriteToPDF); GCHandle handle1 = GCHandle.Alloc(stream); IntPtr parameter = (IntPtr)handle1; IntPtr surface = NativeMethods.cairo_pdf_surface_create_for_stream(cwf, parameter, dim.width, dim.height); IntPtr cairo = NativeMethods.cairo_create(surface); NativeMethods.rsvg_handle_render_cairo(_rsvgHandle, cairo); int status = NativeMethods.cairo_status(cairo); if (status > 0) { IntPtr s = NativeMethods.cairo_status_to_string(status); string ret = Marshal.PtrToStringAnsi(s); throw new Exception(ret); } NativeMethods.cairo_destroy(cairo); NativeMethods.cairo_surface_destroy(surface); }
/// <summary> /// Creates a C# Bitmap from the SVGImage /// </summary> /// <param name="w">The desired width</param> /// <param name="h">The desired height</param> /// <param name="stretch">If true, stretch the SVG image to fit the width and height exactly</param> /// <returns>A new Bitmap containing the SVG image</returns> public Bitmap Image(int w, int h, bool stretch) { int dw = 0; int dh = 0; if (_rsvgHandle == IntPtr.Zero) { return(null); } NativeMethods.RsvgDimensionData dim = GetDimensions(); double scaleX = w / ((double)dim.width); double scaleY = h / ((double)dim.height); if (stretch) { dw = w; dh = h; } else { double fixedScale = (scaleX < scaleY ? scaleX : scaleY); double fixedWidth = dim.width * fixedScale; double fixedHeight = dim.height * fixedScale; scaleX = fixedScale; scaleY = fixedScale; dw = (int)fixedWidth; dh = (int)fixedHeight; } return(Image(dw, dh, scaleX, scaleY)); }
/// <summary> /// Save to PDF /// </summary> /// <param name="filename">Path to file to save</param> public void SaveToPdf(String filename) { NativeMethods.RsvgDimensionData dim = new NativeMethods.RsvgDimensionData(); NativeMethods.rsvg_handle_get_dimensions(_rsvgHandle, ref dim); IntPtr surface = NativeMethods.cairo_pdf_surface_create(filename, dim.width, dim.height); IntPtr cairo = NativeMethods.cairo_create(surface); NativeMethods.rsvg_handle_render_cairo(_rsvgHandle, cairo); int status = NativeMethods.cairo_status(cairo); if (status > 0) { IntPtr s = NativeMethods.cairo_status_to_string(status); string ret = Marshal.PtrToStringAnsi(s); throw new Exception(ret); } NativeMethods.cairo_destroy(cairo); NativeMethods.cairo_surface_destroy(surface); }
NativeMethods.RsvgDimensionData GetDimensions() { NativeMethods.RsvgDimensionData dim = new NativeMethods.RsvgDimensionData(); NativeMethods.rsvg_handle_get_dimensions(_rsvgHandle, ref dim); return(dim); }
public Bitmap Image(double dpiX, double dpiY) { NativeMethods.RsvgDimensionData dim = GetDimensions(); return(Image(dim.width, dim.height, dpiX, dpiY)); }
public Bitmap Image(double dpi) { NativeMethods.RsvgDimensionData dim = GetDimensions(); return(Image((int)dpi * dim.width, (int)dpi * dim.height, dpi, dpi)); }