Hi! Below code not worked. Who is you can help me?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using Aspose.Words;
using Aspose.Words.Math;
using Aspose.Words.Saving;
namespace Aspose_png
{
public partial class Form : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string MyDir = @"C:\Sample";
Document doc = new Document(MyDir,"Sample.docx");
NodeCollection equations = doc.GetChildNodes(NodeType.OfficeMath, true);
int imageIndex = 0;
foreach (OfficeMath eq in equations)
{
if (eq.GetAncestor(NodeType.OfficeMath) == null)
{
Image img = RenderNode(eq, new ImageSaveOptions(SaveFormat.Png));
string imageFileName = string.Format("Image{0} Out.png", imageIndex);
img.Save(MyDir + imageFileName);
imageIndex++;
}
}
}
public static Image RenderNode(Node node, ImageSaveOptions imageOptions)
{
if (node == null)
throw new ArgumentException("Node cannot be null");
if (imageOptions == null)
imageOptions = new ImageSaveOptions(SaveFormat.Png);
Color savePaperColor = imageOptions.PaperColor;
imageOptions.PaperColor = Color.Transparent;
Document doc = (Document)node.Document.Clone(true);
node = doc.GetChild(NodeType.Any, node.Document.GetChildNodes(NodeType.Any, true).IndexOf(node), true);
Shape shape = new Shape(doc, ShapeType.TextBox);
Section parentSection = (Section)node.GetAncestor(NodeType.Section);
shape.Width = parentSection.PageSetup.PageWidth;
shape.Height = parentSection.PageSetup.PageHeight;
shape.FillColor = Color.Transparent;
shape.Stroked = false;
Node currentNode = node;
if (currentNode is InlineStory || currentNode is Story)
{
CompositeNode composite = (CompositeNode)currentNode;
foreach (Node childNode in composite.ChildNodes)
{
shape.AppendChild(childNode.Clone(true));
}
}
else
{
while (!(currentNode.ParentNode is InlineStory || currentNode.ParentNode is Story || currentNode.ParentNode is ShapeBase || currentNode.NodeType == NodeType.Paragraph))
{
CompositeNode parent = (CompositeNode)currentNode.ParentNode.Clone(false);
currentNode = currentNode.ParentNode;
parent.AppendChild(node.Clone(true));
node = parent;
}
shape.AppendChild(node.Clone(true));
}
parentSection.Body.FirstParagraph.AppendChild(shape);
MemoryStream stream = new MemoryStream();
shape.GetShapeRenderer().Save(stream, imageOptions);
shape.Remove();
Bitmap croppedImage;
using (Bitmap renderedImage = new Bitmap(stream))
{
Rectangle cropRectangle = FindBoundingBoxAroundNode(renderedImage);
croppedImage = new Bitmap(cropRectangle.Width, cropRectangle.Height);
croppedImage.SetResolution(imageOptions.Resolution, imageOptions.Resolution);
using (Graphics g = Graphics.FromImage(croppedImage))
{
g.Clear(savePaperColor);
g.DrawImage(renderedImage, new Rectangle(0, 0, croppedImage.Width, croppedImage.Height), cropRectangle.X, cropRectangle.Y, cropRectangle.Width, cropRectangle.Height, GraphicsUnit.Pixel);
}
}
return croppedImage;
}
public static Rectangle FindBoundingBoxAroundNode(Bitmap originalBitmap)
{
Point min = new Point(int.MaxValue, int.MaxValue);
Point max = new Point(int.MinValue, int.MinValue);
for (int x = 0; x < originalBitmap.Width; ++x)
{
for (int y = 0; y < originalBitmap.Height; ++y)
{
Color pixelColor = originalBitmap.GetPixel(x, y);
if (pixelColor.ToArgb() != Color.Empty.ToArgb())
{
min.X = Math.Min(x, min.X);
min.Y = Math.Min(y, min.Y);
max.X = Math.Max(x, max.X);
max.Y = Math.Max(y, max.Y);
}
}
}
return new Rectangle(min.X, min.Y, (max.X - min.X) + 1, (max.Y - min.Y) + 1);
}
}
}
Show error: Image - an ambiguous link between System.Web.UI.WebControls.Image in System.Drawing.Image