如何旋转、裁剪、缩放、翻转图像?
How to rotate, crop, scale, flip an image?
我正在开始一个大型项目,该项目需要 VBA 多种形式的 图像处理功能 以及 Access 和 Excel。
第一个是jpeg images. Existing answers involve workarounds or 的简单旋转。
如何以编程方式裁剪、重新缩放、翻转或旋转图像?
解决方法和第 3 方插件无法满足此项目的需求。
Windows Image Acquisition API (WIA) is an ideal solution in this case. It provides all of the functionality used by Microsoft Paint, Windows Fax & Scan,以及以前的 Windows 相机和扫描仪向导...WIA 非常直观。
这是四个最常见的操作 裁剪、翻转、旋转和调整大小(更多内容即将推出)我将从代码开始,让您无需滚动浏览我的对话。
这些程序都相当 self-explanatory 并准备好 copy/paste as-is(不需要引用。)这些子程序在 Access & Excel 2016 年 Windows 7.
旋转
Sub imgRotate(inFile As String, outFile As String, degreesRotate As Long) '90, 180, 270
Dim Img As Object, IP As Object
Set Img = CreateObject("WIA.ImageFile") 'create WIA objects
Set IP = CreateObject("WIA.ImageProcess")
IP.Filters.Add IP.FilterInfos("RotateFlip").filterid 'setup filter
IP.Filters(1).Properties("RotationAngle") = degreesRotate
Img.LoadFile inFile 'load image
Set Img = IP.Apply(Img) 'apply change
Img.SaveFile outFile 'save new image
End Sub
翻转
Sub imgFlip(inFile As String, outFile As String, Optional Horizontal As Boolean = True)
Dim Img As Object, IP As Object
Set Img = CreateObject("WIA.ImageFile") 'create WIA objects
Set IP = CreateObject("WIA.ImageProcess")
IP.Filters.Add IP.FilterInfos("RotateFlip").filterid 'setup filter
IP.Filters(1).Properties("FlipHorizontal") = Horizontal
IP.Filters(1).Properties("FlipVertical") = Not Horizontal
Img.LoadFile inFile 'load image
Set Img = IP.Apply(Img) 'apply change
Img.SaveFile outFile 'save new image
End Sub
Re-size
Sub imgResize(inFile As String, outFile As String, Optional maxWidth As Long, Optional maxHeight As Long, Optional KeepAspect As Boolean = True)
'if KeepAspect = True then both maxWidth and maxHeight must be specified
Dim Img As Object, IP As Object
Set IP = CreateObject("WIA.ImageProcess") 'create WIA objects
Set Img = CreateObject("WIA.ImageFile")
Img.LoadFile inFile 'load image
IP.Filters.Add IP.FilterInfos("Scale").filterid 'setup filter
If maxWidth <> 0 Then IP.Filters(1).Properties("MaximumWidth") = maxWidth
If maxHeight <> 0 Then IP.Filters(1).Properties("MaximumHeight") = maxHeight
IP.Filters(1).Properties("PreserveAspectRatio") = KeepAspect
Set Img = IP.Apply(Img) 'apply change
Img.SaveFile outFile 'save image
End Sub
裁剪
Sub imgCrop(inFile As String, outFile As String, left As Long, top As Long, right As Long, bottom As Long)
Dim Img As Object, IP As Object
Set IP = CreateObject("WIA.ImageProcess") 'create WIA objects
Set Img = CreateObject("WIA.ImageFile")
Img.LoadFile inFile 'load image
IP.Filters.Add IP.FilterInfos("Crop").filterid 'setup filter
With IP.Filters(1)
.Properties("Left") = Img.Width \ 4
.Properties("Top") = Img.Height \ 4
.Properties("Right") = Img.Width \ 4
.Properties("Bottom") = Img.Height \ 4
End With
Set Img = IP.Apply(Img) 'apply change
Img.SaveFile outFile 'save image
End Sub
(改编自 source)
支持的格式:
使用WIA.ImageProcess
对象:
WIA.ImageProcess
对象处理我对 图像旋转 的初始要求以及其他 进程 感兴趣。总结如下:
要进一步获取 ImageProcess
的 文档 ,我们可以 "ask it"! 运行 这个:
Sub List_WIA_ImageProcess_Filters()
Dim f As Object, x As Long
For Each f In CreateObject("WIA.ImageProcess").FilterInfos
x = x + 1
Debug.Print "#" &x &": " &f.Name &" = " &f.Description &vbLf
Next f
End Sub
...得到这个:
(实际上是怎么来的,我只是复制粘贴到这里!)
RotateFlip = Rotates in 90 degree increments and Flips, horizontally or vertically.
RotationAngle - Set the RotationAngle property to 90, 180, or 270 if you wish
to rotate, otherwise 0 [the default]
FlipHorizontal - Set the FlipHorizontal property to True if you wish to flip
the image horizontally, otherwise False [the default]
FlipVertical - Set the FlipVertical property to True if you wish to flip
the image vertically, otherwise False [the default]
FrameIndex - Set the FrameIndex property to the index of a frame if you
wish to modify a frame other than the ActiveFrame,
otherwise 0 [the default]
Crop = Crops the image by the specified Left, Top, Right, and Bottom margins.
Left - Set the Left property to the left margin (in pixels)
if you wish to crop along the left, otherwise 0 [the default]
Top - Set the Top property to the top margin (in pixels)
if you wish to crop along the top, otherwise 0 [the default]
Right - Set the Right property to the right margin (in pixels)
if you wish to crop along the right, otherwise 0 [the default]
Bottom - Set the Bottom property to the bottom margin (in pixels)
if you wish to crop along the bottom, otherwise 0 [the default]
FrameIndex - Set the FrameIndex property to the index of a frame if you
wish to modify a frame other than the ActiveFrame,
otherwise 0 [the default]
Scale = Scales image to the specified Maximum Width and Maximum Height preserving
Aspect Ratio if necessary.
MaximumWidth - Set the MaximumWidth property to the width (in pixels)
that you wish to scale the image to.
MaximumHeight - Set the MaximumHeight property to the height (in pixels)
that you wish to scale the image to.
PreserveAspectRatio - Set the PreserveAspectRatio property to True
[the default] if you wish to maintain the current aspect
ration of the image, otherwise False and the image will
be stretched to the MaximumWidth and MaximumHeight
FrameIndex - Set the FrameIndex property to the index of a frame if
you wish to modify a frame other than the ActiveFrame,
otherwise 0 [the default]
Stamp = Stamps the specified ImageFile at the specified Left and Top coordinates.
ImageFile - Set the ImageFile property to the ImageFile object that you wish
to stamp
Left - Set the Left property to the offset from the left (in pixels)
that you wish to stamp the ImageFile at [default is 0]
Top - Set the Top property to the offset from the top (in pixels) that
you wish to stamp the ImageFile at [default is 0]
FrameIndex - Set the FrameIndex property to the index of a frame if you wish to
modify a frame other than the ActiveFrame, otherwise 0
[the default]
Exif = Adds/Removes the specified Exif Property.
Remove - Set the Remove property to True if you wish to remove the
specified Exif property, otherwise False [the default] to add the
specified exif property
ID - Set the ID property to the PropertyID you wish to Add or Remove
Type - Set the Type property to indicate the WiaImagePropertyType of the
Exif property you wish to Add (ignored for Remove)
Value - Set the Value property to the Value of the Exif property you wish
to Add (ignored for Remove)
FrameIndex - Set the FrameIndex property to the index of a frame if you
wish to modify a frame other than the ActiveFrame,
otherwise 0 [the default]
#6: Frame = Adds/Removes the specified Frame.
Remove - Set the Remove property to True if you wish to remove the
specified FrameIndex, otherwise False [the default] to Insert the
ImageFile before the specified FrameIndex
ImageFile - Set the ImageFile property to the ImageFile object whose
ActiveFrame that you wish to add (ignored for Remove)
FrameIndex - For Remove, set the FrameIndex property to the index of the frame
you wish to remove, otherwise for add, set the FrameIndex to the
index of the frame to insert the ImageFile before, otherwise 0
[the default] to append a frame from the ImageFile specified
#7: ARGB = Updates the image bits with those specified.
ARGBData - Set the ARGBData property to the Vector of Longs that represent
the ARGB data for the specified FrameIndex (the width and height
must match)
FrameIndex - Set the FrameIndex property to the index of the frame whose ARGB
data you wish to modify, otherwise 0 [the default] to modify the
ActiveFrame
#8: Convert = Converts the resulting ImageFile to the specified type.
FormatID - Set the FormatID property to the supported raster image format
desired, currently you can choose from wiaFormatBMP,
wiaFormatPNG, wiaFormatGIF, wiaFormatJPEG, or wiaFormatTIFF
Quality - For a JPEG file, set the Quality property to any value from 1 to
100 [the default] to specify quality of JPEG compression
Compression - For a TIFF file, set the Compression property to CCITT3, CCITT4,
RLE or Uncompressed to specify the compression scheme,
otherwise LZW [the default]
@ashleedawg.
感谢您提供以上代码。好久不见'see'。
我花了 3 年时间才找到这个非常有用的帖子。
只是想我会使用 WIA 添加另一个有用的代码片段。
EXIF 数据包含从 1 到 8 的方向值:
- = 0度:方向正确,无需调整。
- = 0 度,镜像:图像已从后向前翻转。
- = 180 度:图像上下颠倒。
- = 180 度,镜像:图像上下颠倒并从后向前翻转。
- = 90 度:图像已前后翻转并位于其一侧。
- = 90 度,镜像:图像在它的一边。
- = 270 度:图像已从后向前翻转并位于其远侧。
- = 270 度,镜像:图像在其远端。
此代码用于修改方向值(intOrientation)
它假定已对原始图像进行了复制,然后将修改后的图像用于 'overwrite' 原始图像
outFile为原图,inFile为临时拷贝
设置 EXIF 方向值
Sub imgOrientation(inFile As String, outFile As String, intOrientation As Integer)
Dim img As Object, IP As Object
Set img = CreateObject("WIA.ImageFile") 'create WIA objects
Set IP = CreateObject("WIA.ImageProcess")
With IP
.filters.Add (.FilterInfos("Exif").FilterID)
.filters(1).Properties("ID") = 274 'Orientation
.filters(1).Properties("Type") = 1003 'UnsignedIntegerImagePropertyType
.filters(1).Properties("Value") = intOrientation '1-8 where 1=Normal orientation
img.LoadFile inFile 'load temp image
Set img = .Apply(img) 'apply changes
Kill outFile 'delete original image so it can be overwritten
img.SaveFile outFile 'save updated image to original path
End With
End Sub
注意:
图片本身不是 rotated/flipped 使用上面的代码。它需要与合适的代码一起完成,例如您上面的代码
我正在开始一个大型项目,该项目需要 VBA 多种形式的 图像处理功能 以及 Access 和 Excel。
第一个是jpeg images. Existing answers involve workarounds or
如何以编程方式裁剪、重新缩放、翻转或旋转图像?
解决方法和第 3 方插件无法满足此项目的需求。
Windows Image Acquisition API (WIA) is an ideal solution in this case. It provides all of the functionality used by Microsoft Paint, Windows Fax & Scan,以及以前的 Windows 相机和扫描仪向导...WIA 非常直观。
这是四个最常见的操作 裁剪、翻转、旋转和调整大小(更多内容即将推出)我将从代码开始,让您无需滚动浏览我的对话。
这些程序都相当 self-explanatory 并准备好 copy/paste as-is(不需要引用。)这些子程序在 Access & Excel 2016 年 Windows 7.
旋转
Sub imgRotate(inFile As String, outFile As String, degreesRotate As Long) '90, 180, 270
Dim Img As Object, IP As Object
Set Img = CreateObject("WIA.ImageFile") 'create WIA objects
Set IP = CreateObject("WIA.ImageProcess")
IP.Filters.Add IP.FilterInfos("RotateFlip").filterid 'setup filter
IP.Filters(1).Properties("RotationAngle") = degreesRotate
Img.LoadFile inFile 'load image
Set Img = IP.Apply(Img) 'apply change
Img.SaveFile outFile 'save new image
End Sub
翻转
Sub imgFlip(inFile As String, outFile As String, Optional Horizontal As Boolean = True)
Dim Img As Object, IP As Object
Set Img = CreateObject("WIA.ImageFile") 'create WIA objects
Set IP = CreateObject("WIA.ImageProcess")
IP.Filters.Add IP.FilterInfos("RotateFlip").filterid 'setup filter
IP.Filters(1).Properties("FlipHorizontal") = Horizontal
IP.Filters(1).Properties("FlipVertical") = Not Horizontal
Img.LoadFile inFile 'load image
Set Img = IP.Apply(Img) 'apply change
Img.SaveFile outFile 'save new image
End Sub
Re-size
Sub imgResize(inFile As String, outFile As String, Optional maxWidth As Long, Optional maxHeight As Long, Optional KeepAspect As Boolean = True)
'if KeepAspect = True then both maxWidth and maxHeight must be specified
Dim Img As Object, IP As Object
Set IP = CreateObject("WIA.ImageProcess") 'create WIA objects
Set Img = CreateObject("WIA.ImageFile")
Img.LoadFile inFile 'load image
IP.Filters.Add IP.FilterInfos("Scale").filterid 'setup filter
If maxWidth <> 0 Then IP.Filters(1).Properties("MaximumWidth") = maxWidth
If maxHeight <> 0 Then IP.Filters(1).Properties("MaximumHeight") = maxHeight
IP.Filters(1).Properties("PreserveAspectRatio") = KeepAspect
Set Img = IP.Apply(Img) 'apply change
Img.SaveFile outFile 'save image
End Sub
裁剪
Sub imgCrop(inFile As String, outFile As String, left As Long, top As Long, right As Long, bottom As Long)
Dim Img As Object, IP As Object
Set IP = CreateObject("WIA.ImageProcess") 'create WIA objects
Set Img = CreateObject("WIA.ImageFile")
Img.LoadFile inFile 'load image
IP.Filters.Add IP.FilterInfos("Crop").filterid 'setup filter
With IP.Filters(1)
.Properties("Left") = Img.Width \ 4
.Properties("Top") = Img.Height \ 4
.Properties("Right") = Img.Width \ 4
.Properties("Bottom") = Img.Height \ 4
End With
Set Img = IP.Apply(Img) 'apply change
Img.SaveFile outFile 'save image
End Sub
(改编自 source)
支持的格式:
使用WIA.ImageProcess
对象:
WIA.ImageProcess
对象处理我对 图像旋转 的初始要求以及其他 进程 感兴趣。总结如下:
要进一步获取 ImageProcess
的 文档 ,我们可以 "ask it"! 运行 这个:
Sub List_WIA_ImageProcess_Filters()
Dim f As Object, x As Long
For Each f In CreateObject("WIA.ImageProcess").FilterInfos
x = x + 1
Debug.Print "#" &x &": " &f.Name &" = " &f.Description &vbLf
Next f
End Sub
...得到这个:
(实际上是怎么来的,我只是复制粘贴到这里!)
RotateFlip = Rotates in 90 degree increments and Flips, horizontally or vertically.
RotationAngle - Set the RotationAngle property to 90, 180, or 270 if you wish
to rotate, otherwise 0 [the default]
FlipHorizontal - Set the FlipHorizontal property to True if you wish to flip
the image horizontally, otherwise False [the default]
FlipVertical - Set the FlipVertical property to True if you wish to flip
the image vertically, otherwise False [the default]
FrameIndex - Set the FrameIndex property to the index of a frame if you
wish to modify a frame other than the ActiveFrame,
otherwise 0 [the default]
Crop = Crops the image by the specified Left, Top, Right, and Bottom margins.
Left - Set the Left property to the left margin (in pixels)
if you wish to crop along the left, otherwise 0 [the default]
Top - Set the Top property to the top margin (in pixels)
if you wish to crop along the top, otherwise 0 [the default]
Right - Set the Right property to the right margin (in pixels)
if you wish to crop along the right, otherwise 0 [the default]
Bottom - Set the Bottom property to the bottom margin (in pixels)
if you wish to crop along the bottom, otherwise 0 [the default]
FrameIndex - Set the FrameIndex property to the index of a frame if you
wish to modify a frame other than the ActiveFrame,
otherwise 0 [the default]
Scale = Scales image to the specified Maximum Width and Maximum Height preserving
Aspect Ratio if necessary.
MaximumWidth - Set the MaximumWidth property to the width (in pixels)
that you wish to scale the image to.
MaximumHeight - Set the MaximumHeight property to the height (in pixels)
that you wish to scale the image to.
PreserveAspectRatio - Set the PreserveAspectRatio property to True
[the default] if you wish to maintain the current aspect
ration of the image, otherwise False and the image will
be stretched to the MaximumWidth and MaximumHeight
FrameIndex - Set the FrameIndex property to the index of a frame if
you wish to modify a frame other than the ActiveFrame,
otherwise 0 [the default]
Stamp = Stamps the specified ImageFile at the specified Left and Top coordinates.
ImageFile - Set the ImageFile property to the ImageFile object that you wish
to stamp
Left - Set the Left property to the offset from the left (in pixels)
that you wish to stamp the ImageFile at [default is 0]
Top - Set the Top property to the offset from the top (in pixels) that
you wish to stamp the ImageFile at [default is 0]
FrameIndex - Set the FrameIndex property to the index of a frame if you wish to
modify a frame other than the ActiveFrame, otherwise 0
[the default]
Exif = Adds/Removes the specified Exif Property.
Remove - Set the Remove property to True if you wish to remove the
specified Exif property, otherwise False [the default] to add the
specified exif property
ID - Set the ID property to the PropertyID you wish to Add or Remove
Type - Set the Type property to indicate the WiaImagePropertyType of the
Exif property you wish to Add (ignored for Remove)
Value - Set the Value property to the Value of the Exif property you wish
to Add (ignored for Remove)
FrameIndex - Set the FrameIndex property to the index of a frame if you
wish to modify a frame other than the ActiveFrame,
otherwise 0 [the default]
#6: Frame = Adds/Removes the specified Frame.
Remove - Set the Remove property to True if you wish to remove the
specified FrameIndex, otherwise False [the default] to Insert the
ImageFile before the specified FrameIndex
ImageFile - Set the ImageFile property to the ImageFile object whose
ActiveFrame that you wish to add (ignored for Remove)
FrameIndex - For Remove, set the FrameIndex property to the index of the frame
you wish to remove, otherwise for add, set the FrameIndex to the
index of the frame to insert the ImageFile before, otherwise 0
[the default] to append a frame from the ImageFile specified
#7: ARGB = Updates the image bits with those specified.
ARGBData - Set the ARGBData property to the Vector of Longs that represent
the ARGB data for the specified FrameIndex (the width and height
must match)
FrameIndex - Set the FrameIndex property to the index of the frame whose ARGB
data you wish to modify, otherwise 0 [the default] to modify the
ActiveFrame
#8: Convert = Converts the resulting ImageFile to the specified type.
FormatID - Set the FormatID property to the supported raster image format
desired, currently you can choose from wiaFormatBMP,
wiaFormatPNG, wiaFormatGIF, wiaFormatJPEG, or wiaFormatTIFF
Quality - For a JPEG file, set the Quality property to any value from 1 to
100 [the default] to specify quality of JPEG compression
Compression - For a TIFF file, set the Compression property to CCITT3, CCITT4,
RLE or Uncompressed to specify the compression scheme,
otherwise LZW [the default]
@ashleedawg.
感谢您提供以上代码。好久不见'see'。 我花了 3 年时间才找到这个非常有用的帖子。
只是想我会使用 WIA 添加另一个有用的代码片段。
EXIF 数据包含从 1 到 8 的方向值:
- = 0度:方向正确,无需调整。
- = 0 度,镜像:图像已从后向前翻转。
- = 180 度:图像上下颠倒。
- = 180 度,镜像:图像上下颠倒并从后向前翻转。
- = 90 度:图像已前后翻转并位于其一侧。
- = 90 度,镜像:图像在它的一边。
- = 270 度:图像已从后向前翻转并位于其远侧。
- = 270 度,镜像:图像在其远端。
此代码用于修改方向值(intOrientation) 它假定已对原始图像进行了复制,然后将修改后的图像用于 'overwrite' 原始图像
outFile为原图,inFile为临时拷贝
设置 EXIF 方向值
Sub imgOrientation(inFile As String, outFile As String, intOrientation As Integer)
Dim img As Object, IP As Object
Set img = CreateObject("WIA.ImageFile") 'create WIA objects
Set IP = CreateObject("WIA.ImageProcess")
With IP
.filters.Add (.FilterInfos("Exif").FilterID)
.filters(1).Properties("ID") = 274 'Orientation
.filters(1).Properties("Type") = 1003 'UnsignedIntegerImagePropertyType
.filters(1).Properties("Value") = intOrientation '1-8 where 1=Normal orientation
img.LoadFile inFile 'load temp image
Set img = .Apply(img) 'apply changes
Kill outFile 'delete original image so it can be overwritten
img.SaveFile outFile 'save updated image to original path
End With
End Sub
注意: 图片本身不是 rotated/flipped 使用上面的代码。它需要与合适的代码一起完成,例如您上面的代码