2014-01-08 79 views
2

我目前在Excel中使用它(保存在临时文件夹“C:\ TEMP \照片”)显示的图像用户窗体旋转与VBA中保存的图像

我想要做的是有按钮(90 ,180,270),用于旋转位于“C:\ Temp \ Photos”中的图像。认为它可能是一个FileSystemObject,但不知道他们还不知道如何做到这一点。

谢谢

编辑:添加一些代码的请求。根据在组合框中选择的值插入图片。任何改变都会引用pic1-pic5(任何时候只有5张图片)。

Private Sub ComboBox1_Change() 
pic1 = "C:\Temp\Photos\" & Me.ComboBox1.Text & "\1.jpg" 
pic2 = "C:\Temp\Photos\" & Me.ComboBox1.Text & "\2.jpg" 
pic3 = "C:\Temp\Photos\" & Me.ComboBox1.Text & "\3.jpg" 
pic4 = "C:\Temp\Photos\" & Me.ComboBox1.Text & "\4.jpg" 
pic5 = "C:\Temp\Photos\" & Me.ComboBox1.Text & "\5.jpg" 
If Dir(pic1) <> vbNullString Then 
Me.Image1.Picture = LoadPicture(pic1) 
Else 
Me.Image1.Picture = LoadPicture("") 
End If 
If Dir(pic2) <> vbNullString Then 
Me.Image2.Picture = LoadPicture(pic2) 
Else 
Me.Image2.Picture = LoadPicture("") 
End If 
If Dir(pic3) <> vbNullString Then 
Me.Image3.Picture = LoadPicture(pic3) 
Else 
Me.Image3.Picture = LoadPicture("") 
End If 
If Dir(pic4) <> vbNullString Then 
Me.Image4.Picture = LoadPicture(pic4) 
Else 
Me.Image4.Picture = LoadPicture("") 
End If 
If Dir(pic5) <> vbNullString Then 
Me.Image5.Picture = LoadPicture(pic5) 
Else 
Me.Image5.Picture = LoadPicture("") 
End If 
End Sub 
+0

你能显示一些代码吗? –

+0

最简单的方法是使用同一图像的不同方向保存4个图像,并在每次单击时简单地加载图片。 –

+0

@simoco更改了OP – bmgh1985

回答

9

就像我提到的,没有内置的方式来旋转用户窗体中的图片。话虽如此,还有一种方法可以实现你想要的。下面我已经演示了如何将图像旋转90度。

逻辑

  1. 插入一个临时片

  2. 插入图像成片

  3. 使用IncrementRotation旋转属性

  4. 导出所述图像以用户的临时目录

  5. 删除Temp片

  6. 装入图像背面

准备表单

创建用户窗体和插入图像的控制和命令按钮。你的表格可能看起来像这样。在属性窗口中将图像控件的PictureSizeMode设置为fmPictureSizeModeStretch

enter image description here

代码

我已经写了一子RotatePic到可以传递的程度。就像我刚才提到的那样,这个例子会将它旋转90度,因为我只是在为90进行演示。您可以为余下的度数创建额外的按钮。我也评论了代码,所以你不应该有任何理解它的问题。如果你这样做,然后简单地问:)

Option Explicit 

'~~> API to get the user's temp folder path 
'~~> We will use this to store the rotated image 
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 

'~~> Load the image on userform startup 
Private Sub UserForm_Initialize() 
    Image1.Picture = LoadPicture("C:\Users\Public\Pictures\Sample Pictures\Koala.jpg") 
End Sub 

'~~> Rotating the image 90 degs 
Private Sub CommandButton1_Click() 
    RotatePic 90 

    DoEvents 

    Image1.Picture = LoadPicture(NewPath) 
End Sub 

'~~> Rotating the image 
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("C:\Users\Public\Pictures\Sample Pictures\Koala.jpg") 

    '~~> 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 

'~~> Get the user's temp path 
Function TempPath() As String 
    TempPath = String$(MAX_PATH, Chr$(0)) 
    GetTempPath MAX_PATH, TempPath 
    TempPath = Replace(TempPath, Chr$(0), "") 
End Function 

在行动

当您运行用户窗体,图像上传,当你点击按钮,图像旋转!

enter image description here

+0

很好的答案。没有考虑不能与FSO做到这一点。不错的解决方案。不会考虑把它做成图表并导出。将文件路径传递给函数也同样容易,并且它也使用NewPic的相同路径(因此覆盖临时文件夹中的原始图像,不需要保留旧版本)。是的,应该很容易修改其他角度感谢。 – bmgh1985

+0

@ bmgh1985:很高兴能有帮助:) –

+0

快速的问题,你可以在Userform命令按钮上使用Application.Caller吗?意思是我可以为每个按钮使用相同的代码,只需要看看.text属性并获得角度即可。 – bmgh1985

2

我看这样做是将图片复制到图表的唯一方法,将其旋转,将其导出,然后重新打开它的形式里面你现在正在显示图像的相同方式。

试试这个。

  1. 变化

    If Dir(pic1) <> vbNullString Then 
    Me.Image1.Picture = LoadPicture(pic1) 
    Else ... 
    

    If Dir(pic1) <> vbNullString Then 
    pic1 = myFunction(pic1, rotationDegree) 
    Me.Image1.Picture = LoadPicture(pic1) 
    Else ... 
    

    (和其他地方使用此结构)

  2. 插入,一个模块里面,下面的功能:

    Public Function myFunction(myPicture As String, myRotation As Integer) As String 
    
    ActiveSheet.Pictures.Insert(myPicture).Select 
    Selection.ShapeRange.IncrementRotation myRotation 
    Selection.CopyPicture 
    
    tempPictureName = "C:\testPic.jpg" 
            'Change for the directory/filename you want to use 
    
    Set myChart = Charts.Add 
    
    myChart.Paste 
    myChart.Export Filename:=tempPictureName, Filtername:="JPG" 
    
    Application.DisplayAlerts = False 
    myChart.Delete 
    Selection.Delete 
    Application.DisplayAlerts = True 
    
    myFunction = myDestination 
    
    End Function 
    

编辑:花了这么长的时间的时间写完,我错过了其他用户的答案,这似乎是使用相同的逻辑后(下班)。但是,我的方法可能会更容易使用!

EDIT2: rotationDegree需要设置为旋转的角度(在检索图片之前需要确定)。

+0

感谢您的帮助。另一个很好的答案,虽然一些图像可能根本不需要旋转,但按钮方法将是这种情况下所需的。 – bmgh1985