|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
vba Crop 图片等份切割,根据图片缩放高宽 ,每份高宽会有微小的差异,可以忽略
代码参考:
- Sub 图片切割() '''无间隙图片切割
- ActiveSheet.Pictures.Delete '''删图
- myFname = Application.GetOpenFilename(FileFilter:="全部图片(*.jpg;*.png;*.jpeg),*.jpg;*.png;*.jpeg")
- If myFname = "False" Then Exit Sub
- n1 = Val(InputBox("横切几块?", "分图", 5)) '横n1-1刀
- n2 = Val(InputBox("竖切几块?", "分图", 5)) '竖n2-1刀
- Set 原图 = ActiveSheet.Pictures.Insert(myFname)
- With 原图
- .Top = 20: .Left = 20
- w = .Width: h = .Height: .Copy: .Delete '读取原图 W/H 尺寸 ,复制后删除
- End With
- '''判断 宽高会不会整除
- If w / n1 <> w \ n1 Then w = (w \ n1) * n1 + n1: aa = aa + 1
- If h / n2 <> h \ n2 Then h = (h \ n2) * n2 + n2: aa = aa + 1
- If aa > 0 Then
- ''不整除,调整宽高
- With ActiveSheet.Shapes.AddPicture(Filename:=myFname, _
- LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=20, Top:=20, Width:=w, Height:=h)
- .Copy: .Delete '复制后删除
- End With
- End If
- num = 1
- For i = 1 To n1
- For j = 1 To n2
- ActiveSheet.Paste
- Selection.Name = "pic" & num
- With ActiveSheet.Shapes("pic" & num)
- .Top = 20: .Left = 20
- measurements = GetOriginalMeasurements(ActiveSheet.Shapes("pic" & num))
- ScaleWidth = (w / (measurements(0) / 100)) / 100
- ScaleHeight = (h / (measurements(1) / 100)) / 100
- .PictureFormat.CropLeft = w * (j - 1) / n2 / ScaleWidth
- .PictureFormat.CropRight = w * (n2 - j) / n2 / ScaleWidth
- .PictureFormat.CropTop = h * (i - 1) / n1 / ScaleHeight
- .PictureFormat.CropBottom = h * (n1 - i) / n1 / ScaleHeight
- End With
- num = num + 1
- Next
- Next
- End Sub
- Private Function GetOriginalMeasurements(ByRef myShape As Excel.Shape)
- Dim shpCopy As Excel.Shape
- Dim measurements(1) As Single
- Set shpCopy = myShape.Duplicate
- ' Reset original measurements
- shpCopy.ScaleWidth 1, msoTrue
- shpCopy.ScaleHeight 1, msoTrue
- measurements(0) = shpCopy.Width
- measurements(1) = shpCopy.Height
- shpCopy.Delete
- GetOriginalMeasurements = measurements
- End Function
复制代码 实际案例 22 楼:https://club.excelhome.net/forum ... 68&pid=11136201
|
评分
-
3
查看全部评分
-
|