excel - 在用户表单中旋转图片

标签 excel vba image userform

我正在尝试在用户表单中旋转图像,这是我正在使用的 cose:

Private Declare Function GetTempPath Lib "kernel32" Alias"GetTempPathA_(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Private Const MAX_PATH As Long = 260

Dim NewPath As String

Function TempPath() As String
TempPath = String$(MAX_PATH, Chr$(0))
GetTempPath MAX_PATH, TempPath
TempPath = Replace(TempPath, Chr$(0), "")
End Function

Sub RotatePic(deg As Long)
Dim ws As Worksheet
Dim p As Object
Dim chrt As Chart

'~~> Adding a temp sheet
Set ws = ThisWorkbook.Sheets.Add

'~~> Insert the picture in the newly created worksheet
Set p = ws.Pictures.Insert(**PROBLEM**)

'~~> Rotate the pic
p.ShapeRange.IncrementRotation deg

'~~> Add a chart. This is required so that we can paste the picture in it
'~~> and export it as jpg
Set chrt = Charts.Add()

With ws
    '~~> Move the chart to the newly created sheet
    chrt.Location Where:=xlLocationAsObject, Name:=ws.Name

    '~~> Resize the chart to match shapes picture. Notice that we are
    '~~> setting chart's width as the pictures `height` becuse even when
    '~~> the image is rotated, the Height and Width do not swap.
    With .Shapes(2)
        .Width = p.Height
        .Height = p.Width
    End With

    .Shapes(p.Name).Copy

    With ActiveChart
        .ChartArea.Select
        .Paste
    End With

    '~~> Temp path where we will save the pic
    NewPath = TempPath & "NewFile.Jpg"

    '~~> Export the image
    .ChartObjects(1).Chart.Export filename:=NewPath, FilterName:="jpg"
End With

'~~> Delete the temp sheet
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
End Sub

问题(您可以在代码中看到问题)是我不知道如何获取图片框中图片的路径(我通过图片对话框上传图片)
我该如何解决?

最佳答案

可以简单地尝试这样
enter image description here

代码:

Private Sub CommandButton1_Click()
Me.Image1.Picture = LoadPicture("C:\users\user\desktop\Range.jpg")
End Sub

Sub test()
Dim Ws As Worksheet, fname As String
Dim Shp As ShapeRange, p As Object
Dim Chrt As Chart
fname = "C:\users\user\desktop\TempXXX.jpg"
Set Ws = ThisWorkbook.Sheets("Sheet1")
SavePicture Me.Image1.Picture, fname
DoEvents
Set p = Ws.Pictures.Insert(fname)
p.ShapeRange.Rotation = 90

    Ws.Shapes(p.Name).Copy
    Set Chrt = Ws.ChartObjects.Add(10, 10, Ws.Shapes(p.Name).Height, Ws.Shapes(p.Name).Width).Chart
    Chrt.Paste

Chrt.Export Filename:=fname, FilterName:="jpg"
DoEvents
Me.Image1.Picture = LoadPicture(fname)
'clean temp objects
 Kill fname
 p.Delete
 Chrt.Parent.Delete
End Sub

Private Sub CommandButton2_Click()
test
End Sub

关于excel - 在用户表单中旋转图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57151230/

相关文章:

python - 循环遍历并比较 Python 中的电子表格单元格

Excel VBA - 如果单元格是整数,则删除整行

html - IMG 和 Favicon 不显示

excel - IF ELSE 与 Select Case

ruby - 导出到 Excel - 如何处理时间戳?

excel - 在 VBA 中使用 Excel 函数

c++ - QImage 转换返回 null

html - 为所有浏览器设置带有背景图像的 HTML 表单样式

excel - 用VB6生成Excel文件

VBA excel : How do I compare a string array to a string?