// ExStart:RenderShapeToGraphics public static void RenderShapeToGraphics(string dataDir, Shape shape) { ShapeRenderer r = shape.GetShapeRenderer(); // Find the size that the shape will be rendered to at the specified scale and resolution. Size shapeSizeInPixels = r.GetSizeInPixels(1.0f, 96.0f); // Rotating the shape may result in clipping as the image canvas is too small. Find the longest side // And make sure that the graphics canvas is large enough to compensate for this. int maxSide = System.Math.Max(shapeSizeInPixels.Width, shapeSizeInPixels.Height); using (Bitmap image = new Bitmap((int)(maxSide * 1.25), (int)(maxSide * 1.25))) { // Rendering to a graphics object means we can specify settings and transformations to be applied to // The shape that is rendered. In our case we will rotate the rendered shape. using (Graphics gr = Graphics.FromImage(image)) { // Clear the shape with the background color of the document. gr.Clear(shape.Document.PageColor); // Center the rotation using translation method below gr.TranslateTransform((float)image.Width / 8, (float)image.Height / 2); // Rotate the image by 45 degrees. gr.RotateTransform(45); // Undo the translation. gr.TranslateTransform(-(float)image.Width / 8, -(float)image.Height / 2); // Render the shape onto the graphics object. r.RenderToSize(gr, 0, 0, shapeSizeInPixels.Width, shapeSizeInPixels.Height); } dataDir = dataDir + "TestFile.RenderToGraphics_out.png"; image.Save(dataDir, ImageFormat.Png); Console.WriteLine("\nShape rendered to graphics successfully.\nFile saved at " + dataDir); } }
public static void RenderShapeToGraphics(string dataDir, Shape shape) { //ExStart //ExFor:ShapeRenderer //ExFor:ShapeBase.GetShapeRenderer //ExFor:ShapeRenderer.GetSizeInPixels //ExFor:ShapeRenderer.RenderToSize //ExId:RenderShapeToGraphics //ExSummary:Shows how to render a shape independent of the document to a .NET Graphics object and apply rotation to the rendered image. // The shape renderer is retrieved using this method. This is made into a separate object from the shape as it internally // caches the rendered shape. ShapeRenderer r = shape.GetShapeRenderer(); // Find the size that the shape will be rendered to at the specified scale and resolution. Size shapeSizeInPixels = r.GetSizeInPixels(1.0f, 96.0f); // Rotating the shape may result in clipping as the image canvas is too small. Find the longest side // and make sure that the graphics canvas is large enough to compensate for this. int maxSide = Math.Max(shapeSizeInPixels.Width, shapeSizeInPixels.Height); using (Bitmap image = new Bitmap((int)(maxSide * 1.25), (int)(maxSide * 1.25))) { // Rendering to a graphics object means we can specify settings and transformations to be applied to // the shape that is rendered. In our case we will rotate the rendered shape. using (Graphics gr = Graphics.FromImage(image)) { // Clear the shape with the background color of the document. gr.Clear(Color.White); // Center the rotation using translation method below gr.TranslateTransform((float)image.Width / 8, (float)image.Height / 2); // Rotate the image by 45 degrees. gr.RotateTransform(45); // Undo the translation. gr.TranslateTransform(-(float)image.Width / 8, -(float)image.Height / 2); // Render the shape onto the graphics object. r.RenderToSize(gr, 0, 0, shapeSizeInPixels.Width, shapeSizeInPixels.Height); } image.Save(dataDir + "TestFile.RenderToGraphics.png", ImageFormat.Png); } //ExEnd }
// ExStart:RenderShapeToGraphics public static string RenderShapeToGraphics(string dataDir, Shape shape) { ShapeRenderer r = shape.GetShapeRenderer(); // Find the size that the shape will be rendered to at the specified scale and resolution. Size shapeSizeInPixels = r.GetSizeInPixels(1.0f, 96.0f); // Rotating the shape may result in clipping as the image canvas is too small. Find the longest side // And make sure that the graphics canvas is large enough to compensate for this. int maxSide = System.Math.Max(shapeSizeInPixels.Width, shapeSizeInPixels.Height); using (Android.Graphics.Bitmap bitmap = Android.Graphics.Bitmap.CreateBitmap((int)(maxSide * 1.25), (int)(maxSide * 1.25), Android.Graphics.Bitmap.Config.Argb8888)) { // Rendering to a graphics object means we can specify settings and transformations to be applied to // The shape that is rendered. In our case we will rotate the rendered shape. using (Android.Graphics.Canvas gr = new Android.Graphics.Canvas(bitmap)) { // Clear the shape with the background color of the document. gr.DrawColor(new Android.Graphics.Color(shape.Document.PageColor.ToArgb())); // Center the rotation using translation method below gr.Translate((float)bitmap.Width / 8, (float)bitmap.Height / 2); // Rotate the image by 45 degrees. gr.Rotate(45); // Undo the translation. gr.Translate(-(float)bitmap.Width / 8, -(float)bitmap.Height / 2); // Render the shape onto the graphics object. r.RenderToSize(gr, 0, 0, shapeSizeInPixels.Width, shapeSizeInPixels.Height); } // Save output to file. using (System.IO.FileStream fs = System.IO.File.Create(dataDir + "/RenderToSize_Out.png")) { bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, fs); } } return("\nShape rendered to graphics successfully.\nFile saved at " + dataDir); }
//ExStart:RenderShapeToGraphics public void RenderShapeToGraphics() { Document doc = new Document(MyDir + "Rendering.docx"); Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true); ShapeRenderer render = shape.GetShapeRenderer(); // Find the size that the shape will be rendered to at the specified scale and resolution. Size shapeSizeInPixels = render.GetSizeInPixels(1.0f, 96.0f); // Rotating the shape may result in clipping as the image canvas is too small. Find the longest side // and make sure that the graphics canvas is large enough to compensate for this. int maxSide = System.Math.Max(shapeSizeInPixels.Width, shapeSizeInPixels.Height); using (Bitmap image = new Bitmap((int)(maxSide * 1.25), (int)(maxSide * 1.25))) { // Rendering to a graphics object means we can specify settings and transformations to be applied to the rendered shape. // In our case we will rotate the rendered shape. using (Graphics graphics = Graphics.FromImage(image)) { // Clear the shape with the background color of the document. graphics.Clear(shape.Document.PageColor); // Center the rotation using the translation method below. graphics.TranslateTransform((float)image.Width / 8, (float)image.Height / 2); // Rotate the image by 45 degrees. graphics.RotateTransform(45); // Undo the translation. graphics.TranslateTransform(-(float)image.Width / 8, -(float)image.Height / 2); // Render the shape onto the graphics object. render.RenderToSize(graphics, 0, 0, shapeSizeInPixels.Width, shapeSizeInPixels.Height); } image.Save(ArtifactsDir + "RenderShape.RenderShapeToGraphics.png", ImageFormat.Png); } }