欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 在PPT中绘制形状(shape)

程序员文章站 2023-01-11 18:29:55
概述 本篇文章将介绍C# 在PPT幻灯片中操作形状(shape)的方法。这里主要涉及常规形状,如箭头、矩形、圆形、三角形、多边形、不规则形状等。下面的示例中,可以通过绘制形状,并设置相应格式等。示例包含以下要点: 绘制形状 用图片填充形状 在形状中添加文字 设置形状单色、渐变色填充 设置形状阴影效果 ......

概述

本篇文章将介绍c# 在ppt幻灯片中操作形状(shape)的方法。这里主要涉及常规形状,如箭头、矩形、圆形、三角形、多边形、不规则形状等。下面的示例中,可以通过绘制形状,并设置相应格式等。示例包含以下要点:

  • 绘制形状
  • 用图片填充形状
  • 在形状中添加文字
  • 设置形状单色、渐变色填充
  • 设置形状阴影效果
  • 组合多个形状为一个
  • 设置形状光边效果
  • 将形状保存为图片

 

工具

下载安装后,注意在程序中添加引用spire.presentation.dll到程序,dll文件可在安装路径下的bin文件夹中获取。

C# 在PPT中绘制形状(shape)

示例代码(供参考)

【示例1】绘制形状

步骤1:新建一个幻灯片

//新建一个幻灯片文档,并指定幻灯片大小
presentation ppt = new presentation();
ppt.slidesize.type = slidesizetype.screen16x9;

步骤2:获取第一张幻灯片

islide slide = ppt.slides[0];

步骤3:添加一个云朵形状,并填充渐变色,绘入文字

//添加一个云朵形状,并填充渐变颜色
iautoshape shape1 = slide.shapes.appendshape(shapetype.calloutcloud, new rectanglef(160, 50, 200, 80));
shape1.fill.filltype = fillformattype.gradient;
shape1.fill.gradient.gradientstops.append(0, color.blue);
shape1.fill.gradient.gradientstops.append(1, color.azure);
shape1.line.filltype = fillformattype.none;

//在形状中绘制文本,并设置字体、字号、字体颜色等
shape1.appendtextframe("how??");
textrange textrange = (shape1 as iautoshape).textframe.textrange;
textrange.fontheight = 13;
textrange.latinfont = new textfont("arial");
textrange.fill.filltype = fillformattype.solid;
textrange.fill.solidcolor.color = color.white;

步骤4:添加椭圆形状,并加载图片填充

iautoshape shape2 = slide.shapes.appendshape(shapetype.ellipse, new rectanglef(50, 130, 150, 250));
string picpath = "sk.png"; 
shape2.fill.filltype = fillformattype.picture;
shape2.fill.picturefill.picture.url = picpath;
shape2.fill.picturefill.filltype = picturefilltype.stretch;
shape2.line.filltype = fillformattype.none;

步骤5:添加三角形,并设置边框效果,阴影效果

//添加一个三角形,填充颜色并设置边框样式
iautoshape shape3 = slide.shapes.appendshape(shapetype.triangle, new rectanglef(480, 180, 100, 130));
shape3.fill.filltype = fillformattype.solid;
shape3.fill.solidcolor.color = color.wheat;
shape3.line.width = 3;
shape3.line.dashstyle = linedashstyletype.dash;
shape3.shapestyle.linecolor.color = color.red;

//设置形状阴影效果
presetshadow presetshadow = new presetshadow();
presetshadow.preset = presetshadowvalue.backrightperspective;
presetshadow.colorformat.color = color.lightgray;
shape3.effectdag.presetshadoweffect = presetshadow;

步骤6:添加一个带箭头的直线

iautoshape shape4 = slide.shapes.appendshape(shapetype.line, new rectanglef(660, 200, 100, 100));
shape4.shapestyle.linecolor.color = color.red;
shape4.line.lineendtype = lineendtype.stealtharrow;
shape4.rotation = -90;//设置形状旋转角度

步骤7:绘制一个圆形、五角星,并设置光边效果,将拉个形状组合

//添加一个圆形
iautoshape shape5 = slide.shapes.appendshape(shapetype.ellipse, new rectanglef(289, 166, 120, 120));
shape5.fill.filltype = fillformattype.solid;
shape5.fill.solidcolor.color = color.white;
shape5.line.filltype = fillformattype.solid;
shape5.line.solidfillcolor.color = color.red;

//添加一个五角星形状
iautoshape shape6 = slide.shapes.appendshape(shapetype.fivepointedstar, new rectanglef(300, 170, 100, 100));
shape6.fill.filltype = fillformattype.solid;
shape6.fill.solidcolor.color = color.orange;
shape6.line.filltype = fillformattype.none;
//设置五角星形状的光边效果
gloweffect glow = new gloweffect();
glow.colorformat.color = color.yellow;
glow.radius = 7.0;
shape6.effectdag.gloweffect = glow;

//将shape5和shape6两个形状组合
arraylist list = new arraylist();
list.add(shape5);
list.add(shape6);
ppt.slides[0].groupshapes(list);

步骤8:保存文档

ppt.savetofile("result.pptx", fileformat.pptx2010);

完成代码后,调试运行程序,生成文档,如下图

C# 在PPT中绘制形状(shape)

全部代码:

C# 在PPT中绘制形状(shape)
using spire.presentation;
using spire.presentation.drawing;
using system.collections;
using system.drawing;

namespace drawshape_ppt
{
    class program
    {
        static void main(string[] args)
        {
            //新建一个幻灯片文档,并指定幻灯片大小
            presentation ppt = new presentation();
            ppt.slidesize.type = slidesizetype.screen16x9;

            //获取第一张幻灯片
            islide slide = ppt.slides[0];

            //添加一个云朵形状,并填充渐变颜色
            iautoshape shape1 = slide.shapes.appendshape(shapetype.calloutcloud, new rectanglef(160, 50, 200, 80));
            shape1.fill.filltype = fillformattype.gradient;
            shape1.fill.gradient.gradientstops.append(0, color.blue);
            shape1.fill.gradient.gradientstops.append(1, color.azure);
            shape1.line.filltype = fillformattype.none;

            //在形状中绘制文本,并设置字体、字号、字体颜色等
            shape1.appendtextframe("how??");
            textrange textrange = (shape1 as iautoshape).textframe.textrange;
            textrange.fontheight = 13;
            textrange.latinfont = new textfont("arial");
            textrange.fill.filltype = fillformattype.solid;
            textrange.fill.solidcolor.color = color.white;

            //添加一个椭圆,并用图片填充形状
            iautoshape shape2 = slide.shapes.appendshape(shapetype.ellipse, new rectanglef(50, 130, 150, 250));
            string picpath = "sk.png"; 
            shape2.fill.filltype = fillformattype.picture;
            shape2.fill.picturefill.picture.url = picpath;
            shape2.fill.picturefill.filltype = picturefilltype.stretch;
            shape2.line.filltype = fillformattype.none;

            //添加一个三角形,填充颜色并设置形状边框样式
            iautoshape shape3 = slide.shapes.appendshape(shapetype.triangle, new rectanglef(480, 180, 100, 130));
            shape3.fill.filltype = fillformattype.solid;
            shape3.fill.solidcolor.color = color.wheat;
            shape3.line.width = 3;
            shape3.line.dashstyle = linedashstyletype.dash;
            shape3.shapestyle.linecolor.color = color.red;

            //设置形状阴影效果
            presetshadow presetshadow = new presetshadow();
            presetshadow.preset = presetshadowvalue.backrightperspective;
            presetshadow.colorformat.color = color.lightgray;
            shape3.effectdag.presetshadoweffect = presetshadow;
         
            //添加一个带箭头的直线
            iautoshape shape4 = slide.shapes.appendshape(shapetype.line, new rectanglef(660, 200, 100, 100));
            shape4.shapestyle.linecolor.color = color.red;
            shape4.line.lineendtype = lineendtype.stealtharrow;
            shape4.rotation = -90;//设置形状旋转角度

            //添加一个圆形
            iautoshape shape5 = slide.shapes.appendshape(shapetype.ellipse, new rectanglef(289, 166, 120, 120));
            shape5.fill.filltype = fillformattype.solid;
            shape5.fill.solidcolor.color = color.white;
            shape5.line.filltype = fillformattype.solid;
            shape5.line.solidfillcolor.color = color.red;

            //添加一个五角星形状
            iautoshape shape6 = slide.shapes.appendshape(shapetype.fivepointedstar, new rectanglef(300, 170, 100, 100));
            shape6.fill.filltype = fillformattype.solid;
            shape6.fill.solidcolor.color = color.orange;
            shape6.line.filltype = fillformattype.none;
            //设置五角星形状的光边效果
            gloweffect glow = new gloweffect();
            glow.colorformat.color = color.yellow;
            glow.radius = 7.0;
            shape6.effectdag.gloweffect = glow;
            
            //将shape5和shape6两个形状组合
            arraylist list = new arraylist();
            list.add(shape5);
            list.add(shape6);
            ppt.slides[0].groupshapes(list);

            //保存文档
            ppt.savetofile("result.pptx", fileformat.pptx2010);
            system.diagnostics.process.start("result.pptx");
        }
    }
}
view code

 

【示例2】将形状保存为图片

步骤1:加载测试文档

presentation ppt = new presentation();
ppt.loadfromfile("test.pptx");

步骤2:将形状保存为图片

//遍历第一张幻灯片中的所有图形
 for (int i = 0; i < ppt.slides[0].shapes.count; i++)
 {
     //获取幻灯片中的图形,并保存为.png格式的图片
     image image = ppt.slides[0].shapes.saveasimage(i);
     image.save(string.format("picture-{0}.png", i), system.drawing.imaging.imageformat.png);
 }

C# 在PPT中绘制形状(shape)

全部代码:

C# 在PPT中绘制形状(shape)
using spire.presentation;
using system;
using system.drawing;

namespace saveshapesasimgs_ppt
{
    class program
    {
        static void main(string[] args)
        {
            //实例化presentation类的对象,并加载测试文档
            presentation ppt = new presentation();
            ppt.loadfromfile("test.pptx");

            //遍历第一张幻灯片中的所有图形
            for (int i = 0; i < ppt.slides[0].shapes.count; i++)
            {
                //获取幻灯片中的图形,并保存为.png格式的图片
                image image = ppt.slides[0].shapes.saveasimage(i);
                image.save(string.format("picture-{0}.png", i), system.drawing.imaging.imageformat.png);
            }

        }
    }
}
view code

 

(本文完)

转载请注明出处。