如何在VBA 2010上导出图像之前裁剪图像

我有一个子程序工作得很好,导出从Excel中的范围内拍摄的图像,但我面临一个问题…即使当我设法使图表对象透明,没有边界…导出的图像有一个很多未使用的区域,我希望在出口之前裁剪。

Sub BtnSaveFile_Click() Dim RgExp As Range Dim ImageToExport As Excel.ChartObject Const sSlash$ = "/" Const sPicType$ = ".png" Dim sChartName$ Dim sPath$ Dim sBook$ Set RgExp = Range("G4:N28") RgExp.CopyPicture xlScreen, xlPicture Set ImageToExport = ActiveSheet.ChartObjects.Add(Left:=RgExp.Left - 80, Top:=RgExp.Top - 80, Width:=RgExp.Width - 80, Height:=RgExp.Height - 80) With ImageToExport.Chart.ChartArea.Format.Fill .Visible = msoFalse End With With ImageToExport.Chart.ChartArea.Format.Line .Visible = msoCFalse End With ImageToExport.Chart.Paste Start: sChartName = Application.InputBox("Enter A Name Of Your Choice" & vbCr & _ "There Is No Default Name Available" & vbCr & _ "The File Will Be Saved At C:\SECTIONIZER\SAVED SECTION\", "PROVIDE A NAME FOR THE VIEW", "") If sChartName = Empty Then MsgBox "Please Enter A File Name", , "Invalid Entry" GoTo Start End If If sChartName = "False" Then ImageToExport.Delete Exit Sub End If sBook = "C:\SECTIONIZER\SAVED SECTION" sPath = sBook & sSlash & sChartName & sPicType ImageToExport.Chart.Export Filename:=sPath, FilterName:="PNG" ImageToExport.Delete ExitProc: Application.ScreenUpdating = True Set ImageToExport = Nothing Set RgExp = Nothing End Sub 

我有这个想法,通过寻找在图像的每一边(左,上,右,下)的第一个黑色像素,所以我可以设置坐标来挖空空像素,但我还没有find代码这样做。

编辑:从OP的提供的链接添加图像

由此:

在这里输入图像说明

对此:

在这里输入图像说明

您将需要启动macros录制器,然后将图片裁剪到您喜欢的区域,然后使用子例程中logging的坐标。 以下是你将得到的一个样本

 Selection.ShapeRange.PictureFormat.Crop.PictureWidth = 196 Selection.ShapeRange.PictureFormat.Crop.PictureHeight = 196 Selection.ShapeRange.PictureFormat.Crop.PictureOffsetX = 0 Selection.ShapeRange.PictureFormat.Crop.PictureOffsetY = -8 

我设法解决它。 首先,我将excel范围内的所有graphics进行分组,选中该组,然后将选中的W和H设置为要添加的图表的宽度和高度,然后在添加的图表上粘贴复制select…这是最后的结果:

 Sub BtnSaveFile_Click() Dim ImageToExport As Excel.ChartObject Dim Shp As Shape Dim RangeToTest As Range Dim CC As Range Dim DD As Range Const sSlash$ = "/" Const sPicType$ = ".png" Dim sChartName$ Dim sPath$ Dim sBook$ 'The images at the range are selected and grouped Set RangeToTest = Range("G4:N28") For Each CC In RangeToTest Set ShpList = Sheets("SECTIONIZER").Shapes For Each Shp In ShpList If CC.Address = Shp.TopLeftCell.Address Then Shp.Select Replace:=False End If Next Shp Next CC Selection.ShapeRange.Group.Select 'W and H are established with the above selected group Width and Height W = Selection.Width H = Selection.Height 'Selected group is copied as picture Selection.CopyPicture xlScreen, xlPicture 'Chart Object is Added with the W and H values Set ImageToExport = ActiveSheet.ChartObjects.Add(0, 0, W , H) With ImageToExport.Chart.ChartArea.Format.Fill .Visible = msoFalse End With With ImageToExport.Chart.ChartArea.Format.Line .Visible = msoCFalse End With 'Group Selected is then Pasted into the above added Chart ImageToExport.Chart.Paste Start: ' Pop Up Window For User To Enter File Name sChartName = Application.InputBox("Enter A Name Of Your Choice" & vbCr & _ "There Is No Default Name Available" & vbCr & _ "The File Will Be Saved At C:\SECTIONIZER\SAVED SECTION\", "PROVIDE A NAME FOR THE VIEW", "") ' User presses "OK" without entering a name If sChartName = Empty Then MsgBox "Please Enter A File Name", , "Invalid Entry" GoTo Start End If ' If Cancel Button Is Pressed If sChartName = "False" Then ImageToExport.Delete Exit Sub End If ' If A Name Was Given, View Is Exported As A *.PNG Image ' At C:\SECTIONIZER\SAVED SECTION sBook = "C:\SECTIONIZER\SAVED SECTION" sPath = sBook & sSlash & sChartName & sPicType ImageToExport.Chart.Export Filename:=sPath, FilterName:="PNG" ImageToExport.Delete ExitProc: Application.ScreenUpdating = True Set ImageToExport = Nothing Set RgExp = Nothing End Sub