【问题标题】:Cordinates of a shape in PowerPoint (c#)PowerPoint中形状的坐标(c#)
【发布时间】:2014-10-14 12:53:13
【问题描述】:

我想获取 PowerPoint 形状的坐标。

这是我的 c# 实现

Microsoft.Office.Interop.PowerPoint._Application myPPT = Globals.ThisAddIn.Application;
Microsoft.Office.Interop.PowerPoint.Slide curSlide = myPPT.ActiveWindow.View.Slide;

foreach (Shape current in curSlide.Shapes)
{
   Debug.Print(current.Type.ToString());              

   foreach (ShapeNode n in current.Nodes)
   {
      double x = n.Points[1, 1];
      double y = n.Points[1, 2];

      Debug.Print("X: " + x);
      Debug.Print("Y: " + y);
    }
}

这适用于“手绘”形状,但不适用于预装的形状(如矩形、大箭头、星形...),我收到错误 COMException。

有人知道如何实现它并访问形状的点吗?

【问题讨论】:

  • VB.net 代码的哪一部分不起作用?您是否事先将其转换为 C#?展示您生成的 C# 并描述我们可以帮助您调试的具体问题。
  • 这是我的 C# 代码:它在 "double x" 行上有错误。 COMExcepton (HRESULT: 0x800A01A8) foreach (curSlide.Shapes 中的当前形状) { foreach (current.Nodes 中的ShapeNode n) { double x = n.Points(1, 1);双 y = n.Points(1, 2); } }
  • 您的错误代码有很大帮助。看到这个问题stackoverflow.com/questions/7081960/…

标签: c# coordinates powerpoint shape points


【解决方案1】:

我尝试了很多方法,但总是以 HRESULT: 0x800A01A8 错误告终。

解决方法是将形状导出为图像并使用 AForge 确定该图像的边缘。不漂亮,但做了工作。我还没有调整下面的代码来处理转换和偏移中的差异。

    static List<Tuple<float,float>> ShapeToPoints(Microsoft.Office.Interop.PowerPoint.Shape shape)
    {
        string path = @"C:\Temp\bpmimg\Test1.png";
        if (File.Exists(path))
            File.Delete(path);
        List<Tuple<float, float>> points = new List<Tuple<float, float>>();

        try
        {
            //Shape.Export is an internal method, but we access it via reflection this way
            var args = new object[] { path, Microsoft.Office.Interop.PowerPoint.PpShapeFormat.ppShapeFormatPNG, 0, 0, Microsoft.Office.Interop.PowerPoint.PpExportMode.ppRelativeToSlide };
            shape.GetType().InvokeMember("Export", System.Reflection.BindingFlags.InvokeMethod, null, shape, args); // Export to file on disk
            // locating objects
            BlobCounter blobCounter = new BlobCounter();
            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight = 5;
            blobCounter.MinWidth = 5;

            //Important that the dispose is called so that the temp-file cleanup File.Delete doesn't crash due to locked file
            using (System.Drawing.Bitmap image = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(path))
            {
                blobCounter.ProcessImage(image);
            }
            Blob[] blobs = blobCounter.GetObjectsInformation();

            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
            if (blobs.Length != 1)
            {
                //Unexpected number of blobs
                return null;
            }
            var blob = blobs[0];

            List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);

            List<IntPoint> untransformedPoints;
            // use the shape checker to extract the corner points
            if (!shapeChecker.IsConvexPolygon(edgePoints, out untransformedPoints))
            {
                IConvexHullAlgorithm hullFinder = new GrahamConvexHull();
                untransformedPoints = hullFinder.FindHull(edgePoints);
            }
            var untransformedWidth = untransformedPoints.Max(p => p.X);
            var untransformedHeight = untransformedPoints.Max(p => p.Y);
            var widthRatio = untransformedWidth / shape.Width;//How many time bigger is the image-based figure than the shape figure
            var heightRatio = untransformedHeight / shape.Height;//How many time bigger is the image-based figure than the shape figure

            points = untransformedPoints.Select(p => new Tuple<float, float>(shape.Left + p.X / widthRatio, shape.Top + p.Y / heightRatio)).ToList();//Transform
        }
        finally
        {
            if (File.Exists(path))
                File.Delete(path);
        }
        return points;
    }

【讨论】:

    猜你喜欢
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多