Last active
August 31, 2017 12:34
-
-
Save aspose-words/321cf1d7080ec5fa4f5b13ffb6c5c865 to your computer and use it in GitHub Desktop.
This Gist contains code snippets from examples of Aspose.Words for iOS via Xamarin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This Gist contains code snippets from examples of Aspose.Words for iOS via Xamarin. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
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); | |
int shapeDiagonal = (int)System.Math.Sqrt(shapeSizeInPixels.Width * shapeSizeInPixels.Width + shapeSizeInPixels.Height * shapeSizeInPixels.Height); | |
int width = (int)(maxSide * 1.5); | |
int height = (int)(maxSide * 1.5); | |
int bitsPerPixel = 32; | |
int bitsPerComponent = 8; | |
int bytesPerRow = width * bitsPerPixel / bitsPerComponent; | |
int size = bytesPerRow * height; | |
IntPtr bitmapBlock = System.Runtime.InteropServices.Marshal.AllocHGlobal(size); | |
CoreGraphics.CGBitmapContext gr = new CoreGraphics.CGBitmapContext(bitmapBlock, | |
width, height, | |
bitsPerComponent, | |
bytesPerRow, | |
CoreGraphics.CGColorSpace.CreateDeviceRGB(), | |
CoreGraphics.CGBitmapFlags.PremultipliedFirst); | |
// 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 (gr) | |
{ | |
gr.ClearRect(new CoreGraphics.CGRect(0, 0, width, height)); | |
// Clear the shape with the background color of the document. | |
gr.AddRect(new CoreGraphics.CGRect(0, 0, width, height)); | |
gr.ClosePath(); | |
Color c = shape.Document.PageColor; | |
gr.SetFillColor(new CoreGraphics.CGColor(c.R, c.G, c.B, c.A)); | |
gr.DrawPath(CoreGraphics.CGPathDrawingMode.Fill); | |
// Play with transformations. | |
gr.ConcatCTM(CoreGraphics.CGAffineTransform.MakeTranslation((width - shapeDiagonal) / 2, (height) / 2)); | |
gr.ConcatCTM(CoreGraphics.CGAffineTransform.MakeRotation(-(float)(Math.PI / 4))); | |
// Render the shape onto the graphics object. | |
r.RenderToSize(gr, 0, 0, shapeSizeInPixels.Width, shapeSizeInPixels.Height); | |
using (CoreGraphics.CGImage bitmap = gr.ToImage()) | |
{ | |
using (Foundation.NSMutableData imageData = new Foundation.NSMutableData()) | |
{ | |
using (ImageIO.CGImageDestination dest = ImageIO.CGImageDestination.Create(imageData, "public.png", 1)) | |
{ | |
dest.AddImage(bitmap, (Foundation.NSDictionary)null); | |
dest.Close(); | |
} | |
using (var ms = new MemoryStream(imageData.ToArray())) | |
using (FileStream fs = File.Create(dataDir + "/RenderToSize_Out.png")) | |
ms.WriteTo(fs); | |
} | |
} | |
} | |
return "\nShape rendered to graphics successfully.\nFile saved at " + dataDir; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment