public SVGImage(string file) { int error = 0; dimension = new RsvgDimensionData(); try { handle = rsvg_handle_new_from_file(file, out error); if (handle != IntPtr.Zero) { rsvg_handle_get_dimensions(handle, ref dimension); } } catch (Exception e) { Console.WriteLine("SVGImage. SVGImage (file). Exception: {0}", e); } finally { if (handle == IntPtr.Zero) { throw new System.IO.IOException("SVGImage. SVGImage (file). Could not load file: " + file); } } }
public SVGImage(string file) { int error = 0; dimension = new RsvgDimensionData (); try { handle = rsvg_handle_new_from_file (file, out error); if (handle != IntPtr.Zero) rsvg_handle_get_dimensions (handle, ref dimension); } finally { if (handle == IntPtr.Zero) throw new System.IO.IOException ("File not found: " + file); } }
public SVGImage(string file) { int error = 0; dimension = new RsvgDimensionData (); try { handle = rsvg_handle_new_from_file (file, out error); if (handle != IntPtr.Zero) rsvg_handle_get_dimensions (handle, ref dimension); } catch (Exception e) { Console.WriteLine ("SVGImage. SVGImage (file). Exception: {0}", e); } finally { if (handle == IntPtr.Zero) throw new System.IO.IOException ("SVGImage. SVGImage (file). Could not load file: " + file); } }
public SVGImage(string file) { int error = 0; dimension = new RsvgDimensionData(); try { handle = rsvg_handle_new_from_file(file, out error); if (handle != IntPtr.Zero) { rsvg_handle_get_dimensions(handle, ref dimension); } } finally { if (handle == IntPtr.Zero) { throw new System.IO.IOException("File not found: " + file); } } }
static extern void rsvg_handle_get_dimensions(IntPtr handle, ref RsvgDimensionData dimension);
internal static extern void rsvg_handle_get_dimensions(IntPtr handle, ref RsvgDimensionData dimension_data);
/// <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) { RsvgDimensionData dim = new RsvgDimensionData(); int dw = 0; int dh = 0; if (_rsvgHandle == IntPtr.Zero) { return null; } rsvg_handle_get_dimensions(_rsvgHandle, ref dim); 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; } //// Initialize the gdk_pixbuf IntPtr pixbuf = gdk_pixbuf_new(ColorSpace.Rgb, true, 8, dw, dh); int stride = gdk_pixbuf_get_rowstride(pixbuf); int width = dw; int height = dh; IntPtr pixels = gdk_pixbuf_get_pixels(pixbuf); byte[] src = new byte[stride * height]; Marshal.Copy(src, 0, pixels, src.Length); //// Create the cairo surface IntPtr surface = cairo_image_surface_create_for_data(gdk_pixbuf_get_pixels(pixbuf), 0, width, height, stride); IntPtr cairo = cairo_create(surface); //// Set the scale and render the image cairo_scale(cairo, scaleX, scaleY); rsvg_handle_render_cairo(_rsvgHandle, cairo); //// Destroy the cairo surface cairo_destroy(cairo); cairo_surface_destroy(surface); //// Copy the gdk_pixbuf into a bitmap Bitmap bitmap = new Bitmap(width, height); byte[] temp = new byte[stride * height]; Marshal.Copy(pixels, temp, 0, temp.Length); BitmapData bd = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); if (bd.Stride == stride) { Marshal.Copy(temp, 0, bd.Scan0, temp.Length); } else { for (int y = 0; y < height; ++y) { Marshal.Copy(temp, y * stride, new IntPtr(bd.Scan0.ToInt64() + y * bd.Stride), width * 4); } } bitmap.UnlockBits(bd); //// Release the gdk_pixbuf g_object_unref(pixbuf); return bitmap; }
/// <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) { RsvgDimensionData dim = new RsvgDimensionData(); int dw = 0; int dh = 0; if (_rsvgHandle == IntPtr.Zero) { return(null); } rsvg_handle_get_dimensions(_rsvgHandle, ref dim); 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; } //// Initialize the gdk_pixbuf IntPtr pixbuf = gdk_pixbuf_new(ColorSpace.Rgb, true, 8, dw, dh); int stride = gdk_pixbuf_get_rowstride(pixbuf); int width = dw; int height = dh; IntPtr pixels = gdk_pixbuf_get_pixels(pixbuf); byte[] src = new byte[stride * height]; Marshal.Copy(src, 0, pixels, src.Length); //// Create the cairo surface IntPtr surface = cairo_image_surface_create_for_data(gdk_pixbuf_get_pixels(pixbuf), 0, width, height, stride); IntPtr cairo = cairo_create(surface); //// Set the scale and render the image cairo_scale(cairo, scaleX, scaleY); rsvg_handle_render_cairo(_rsvgHandle, cairo); //// Destroy the cairo surface cairo_destroy(cairo); cairo_surface_destroy(surface); //// Copy the gdk_pixbuf into a bitmap Bitmap bitmap = new Bitmap(width, height); byte[] temp = new byte[stride * height]; Marshal.Copy(pixels, temp, 0, temp.Length); BitmapData bd = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); if (bd.Stride == stride) { Marshal.Copy(temp, 0, bd.Scan0, temp.Length); } else { for (int y = 0; y < height; ++y) { Marshal.Copy(temp, y * stride, new IntPtr(bd.Scan0.ToInt64() + y * bd.Stride), width * 4); } } bitmap.UnlockBits(bd); //// Release the gdk_pixbuf g_object_unref(pixbuf); return(bitmap); }