请版主能加个精华,盖个精华章,要求不高吧,呵呵。也请大侠看能否把程序简化。 在ppt、excel板块都实现了图片的切割,但是,那些方法大部分从vb来的dll文件,无法查看源文件或代码。无意中,我找到了方法,很高兴与大家一同分享。 PictureFormat.CropLeft=20 ‘表示指定图片或 OLE 对象从左侧裁剪了20 磅,只剩下右侧。 PictureFormat.CropRight=30 ‘表示指定图片或 OLE 对象从右侧裁剪了30 磅,只剩下左侧。 PictureFormat.CropTop=40 ‘表示指定图片或 OLE 对象从顶侧裁剪了40 磅,只剩下底侧。 PictureFormat.CropTop =50 ‘表示指定图片或 OLE 对象从底侧裁剪了50 磅,只剩下顶侧。 如果把这四个属性的数值改为图片的高度(height)或宽度(width),那不就实现了图片的切割了吗? 比如: PictureFormat.CropLeft= ActiveDocument.Shapes("picture 2").Width / 2 ‘浮动式图片 PictureFormat.CropLeft= ActiveDocument.InlineShapes(i).Height / 2 ‘嵌入式图片 但是图片切割一次后,原有的图片就消失了,只剩下切割后的图片。怎么办?我想到了继续添加图片,或复制粘贴原有的图片,这样,切割就能多次进行。 简单举个例子,比如要把一个图片切割成四等份,就是横一刀,竖一刀,分成左上、右上、左下、右下四部分。 1、怎么保留左上呢?切掉右侧CropRight,切掉底侧CropBottom。 2、怎么保留右上呢?切掉左侧CropLeft,切掉底侧CropBottom。 3、怎么保留左下呢?切掉右侧.CropRight,切掉上侧CropTop。 4、怎么保留右上呢?切掉左侧CropLeft,切掉上侧CropTop。 因为不断添加图片,图片的名称就变化了,所以我在引用新的图片名称时就重新定义了。而在嵌入式图片时使用了复制粘贴的方法。 如果在你的电脑运行错误,建议你在空白文档中运行,或修改括号()中的数值。
- Sub 嵌入式图片切割1()
- ‘插入一个嵌入式图片,只有一个哦
- '横一刀
- Dim s As InlineShape
- Dim i As Integer
- Set FileName = ActiveDocument.InlineShapes(1)
- i = ActiveDocument.InlineShapes.Count
- Set s = ActiveDocument.InlineShapes(1)
- s.PictureFormat.CropBottom = ActiveDocument.InlineShapes(i).Height / 2
- End Sub
复制代码- Sub 嵌入式图片切割2()
- ‘插入一个嵌入式图片,只有一个哦
- '竖一刀
- Dim s As InlineShape
- Dim i As Integer
- Set FileName = ActiveDocument.InlineShapes(1)
- i = ActiveDocument.InlineShapes.Count
- Set s = ActiveDocument.InlineShapes(1)
- s.PictureFormat.CropRight = ActiveDocument.InlineShapes(i).Width / 2
- End Sub
复制代码- Sub 嵌入式图片切割3()
- ‘插入一个嵌入式图片,只有一个哦
- '横一刀竖一刀
- Dim s As InlineShape, t As InlineShape, m As InlineShape, n As InlineShape
- Dim i As Integer
- Set FileName = ActiveDocument.InlineShapes(1)
- i = ActiveDocument.InlineShapes.Count
- ActiveDocument.InlineShapes(1).Select
- Selection.Copy
- Set s = ActiveDocument.InlineShapes(1)
- s.PictureFormat.CropRight = ActiveDocument.InlineShapes(1).Width / 2 '左上
- s.PictureFormat.CropBottom = ActiveDocument.InlineShapes(1).Height / 2
- Selection.MoveRight Unit:=wdCharacter, Count:=1
- Selection.Paste
- ActiveDocument.InlineShapes(2).Select
- Set t = ActiveDocument.InlineShapes(2)
- t.PictureFormat.CropLeft = ActiveDocument.InlineShapes(2).Width / 2 '右上
- t.PictureFormat.CropBottom = ActiveDocument.InlineShapes(2).Height / 2
- Selection.MoveRight Unit:=wdCharacter, Count:=1
- Selection.Paste
- ActiveDocument.InlineShapes(3).Select
- Set m = ActiveDocument.InlineShapes(3)
- m.PictureFormat.CropRight = ActiveDocument.InlineShapes(3).Width / 2 '左下
- m.PictureFormat.CropTop = ActiveDocument.InlineShapes(3).Height / 2
- Selection.MoveRight Unit:=wdCharacter, Count:=1
- Selection.Paste
- ActiveDocument.InlineShapes(4).Select
- Set n = ActiveDocument.InlineShapes(4)
- n.PictureFormat.CropLeft = ActiveDocument.InlineShapes(4).Width / 2 '右下
- n.PictureFormat.CropTop = ActiveDocument.InlineShapes(4).Height / 2
- End Sub
复制代码 浮动式图片切割原理和嵌入式图片切割原理相似。
- Sub 浮动式图片切割1()
- ‘插入嵌入式图片,改为浮动式图片后。
- '横一刀竖一刀
- Dim s As Shape, t As Shape, m As Shape, n As Shape
- Dim i As Integer
- Selection.ShapeRange.Name = "picture 2"
- 'i = ActiveDocument.Shapes.Count
- Set s = ActiveDocument.Shapes("picture 2")
- ActiveDocument.Shapes("picture 2").Select
- Selection.Copy
- s.PictureFormat.CropRight = ActiveDocument.Shapes("picture 2").Width / 2’左上
- s.PictureFormat.CropBottom = ActiveDocument.Shapes("picture 2").Height / 2
- Selection.MoveRight Unit:=wdCharacter, Count:=1
- Selection.Paste
- Selection.ShapeRange.Name = "picture 3"
- Set t = ActiveDocument.Shapes("picture 3")
- t.PictureFormat.CropLeft = ActiveDocument.Shapes("picture 3").Width / 2’右上
- t.PictureFormat.CropBottom = ActiveDocument.Shapes("picture 3").Height / 2
- Selection.MoveRight Unit:=wdCharacter, Count:=1
- Selection.Paste
- Selection.ShapeRange.Name = "picture 4"
- Set m = ActiveDocument.Shapes("picture 4")
- m.PictureFormat.CropRight = ActiveDocument.Shapes("picture 4").Width / 2’左下
- m.PictureFormat.CropTop = ActiveDocument.Shapes("picture 4").Height / 2
- Selection.MoveRight Unit:=wdCharacter, Count:=1
- Selection.Paste
- Selection.ShapeRange.Name = "picture 5"
- Set n = ActiveDocument.Shapes("picture 5")
- n.PictureFormat.CropLeft = ActiveDocument.Shapes("picture 5").Width / 2’右下
- n.PictureFormat.CropTop = ActiveDocument.Shapes("picture 5").Height / 2
- End Sub
复制代码 下面几个程序,文件名自己可以修改,其余的不动。文档中是空白的,利用导入文件的方法。每次切割后,原有图片消失,只剩下切割后的图片,所以我的程序里不断添加这个文件,不断切割,达到效果。
- Sub 图片切割四等份()
- '横一刀竖一刀
- Dim s As Shape
- Dim i As Integer
- filename = "C:\Documents and Settings\Administrator\桌面\111.jpg"
- i = ActiveDocument.Shapes.Count
- Set s = ActiveDocument.Shapes.AddPicture(filename)
- s.PictureFormat.CropRight = ActiveDocument.Shapes(i + 1).Width / 2
- s.PictureFormat.CropBottom = ActiveDocument.Shapes(i + 1).Height / 2
- Set s = ActiveDocument.Shapes.AddPicture(filename)
- s.PictureFormat.CropLeft = ActiveDocument.Shapes(i + 2).Width / 2
- s.PictureFormat.CropBottom = ActiveDocument.Shapes(i + 2).Height / 2
- Set s = ActiveDocument.Shapes.AddPicture(filename)
- s.PictureFormat.CropRight = ActiveDocument.Shapes(i + 3).Width / 2
- s.PictureFormat.CropTop = ActiveDocument.Shapes(i + 3).Height / 2
- Set s = ActiveDocument.Shapes.AddPicture(filename)
- s.PictureFormat.CropLeft = ActiveDocument.Shapes(i + 4).Width / 2
- s.PictureFormat.CropTop = ActiveDocument.Shapes(i + 4).Height / 2
- End Sub
复制代码- Sub 图片切割六等份()
- '横一刀竖两刀
- Dim s As Shape
- Dim i As Integer
- filename = "C:\Documents and Settings\Administrator\桌面\111.jpg"
- i = ActiveDocument.Shapes.Count
- Set s = ActiveDocument.Shapes.AddPicture(filename)
- s.PictureFormat.CropRight = ActiveDocument.Shapes(i + 1).Width * 2 / 3 '左上块
- s.PictureFormat.CropBottom = ActiveDocument.Shapes(i + 1).Height / 2
- Set s = ActiveDocument.Shapes.AddPicture(filename)
- s.PictureFormat.CropLeft = ActiveDocument.Shapes(i + 2).Width / 3 '中上块
- s.PictureFormat.CropRight = ActiveDocument.Shapes(i + 2).Width / 2
- s.PictureFormat.CropBottom = ActiveDocument.Shapes(i + 2).Height / 2
- Set s = ActiveDocument.Shapes.AddPicture(filename)
- s.PictureFormat.CropLeft = ActiveDocument.Shapes(i + 3).Width * 2 / 3 '右上块
- s.PictureFormat.CropBottom = ActiveDocument.Shapes(i + 3).Height / 2
- Set s = ActiveDocument.Shapes.AddPicture(filename)
- s.PictureFormat.CropRight = ActiveDocument.Shapes(i + 4).Width * 2 / 3 '左下块
- s.PictureFormat.CropTop = ActiveDocument.Shapes(i + 4).Height / 2
- Set s = ActiveDocument.Shapes.AddPicture(filename)
- s.PictureFormat.CropLeft = ActiveDocument.Shapes(i + 5).Width / 3 '中下块
- s.PictureFormat.CropRight = ActiveDocument.Shapes(i + 5).Width / 2
- s.PictureFormat.CropTop = ActiveDocument.Shapes(i + 5).Height / 2
- Set s = ActiveDocument.Shapes.AddPicture(filename)
- s.PictureFormat.CropLeft = ActiveDocument.Shapes(i + 6).Width * 2 / 3 '右下块
- s.PictureFormat.CropTop = ActiveDocument.Shapes(i + 6).Height / 2 '
- End Sub
复制代码
- Sub 图片切割六等份2()
- '横两刀竖一刀
- Dim s As Shape
- Dim i As Integer
- filename = "C:\Documents and Settings\Administrator\桌面\111.jpg"
- i = ActiveDocument.Shapes.Count
- Set s = ActiveDocument.Shapes.AddPicture(filename)
- s.PictureFormat.CropRight = ActiveDocument.Shapes(i + 1).Width / 2 '左上块
- s.PictureFormat.CropBottom = ActiveDocument.Shapes(i + 1).Height * 2 / 3
- Set s = ActiveDocument.Shapes.AddPicture(filename)
- s.PictureFormat.CropLeft = ActiveDocument.Shapes(i + 2).Width / 2 '右上块
- s.PictureFormat.CropBottom = ActiveDocument.Shapes(i + 2).Height * 2 / 3
- Set s = ActiveDocument.Shapes.AddPicture(filename)
- s.PictureFormat.CropTop = ActiveDocument.Shapes(i + 3).Height / 3 '左中块
- s.PictureFormat.CropBottom = ActiveDocument.Shapes(i + 3).Height / 2
- s.PictureFormat.CropRight = ActiveDocument.Shapes(i + 3).Width / 2
- Set s = ActiveDocument.Shapes.AddPicture(filename)
- s.PictureFormat.CropTop = ActiveDocument.Shapes(i + 4).Height / 3 '右中块
- s.PictureFormat.CropBottom = ActiveDocument.Shapes(i + 4).Height / 2
- s.PictureFormat.CropLeft = ActiveDocument.Shapes(i + 4).Width / 2
- Set s = ActiveDocument.Shapes.AddPicture(filename)
- s.PictureFormat.CropRight = ActiveDocument.Shapes(i + 5).Width / 2 '左下块
- s.PictureFormat.CropTop = ActiveDocument.Shapes(i + 5).Height * 2 / 3
- Set s = ActiveDocument.Shapes.AddPicture(filename)
- s.PictureFormat.CropLeft = ActiveDocument.Shapes(i + 6).Width / 2 '右下块
- s.PictureFormat.CropTop = ActiveDocument.Shapes(i + 6).Height * 2 / 3 '
- End Sub
复制代码 如果程序有错误,请告知,谢谢。
|