0

I used code from "How to rotate, crop, scale, flip an image?" to rotate an image in an Access image control, but I can't find a way to directly load the image object returned from the API into the Access control. I first have to save the object to a file, then load the file into the control, and I then need to delete the file used to save the rotated file. Is there a way to load the image object directly to the control? I have no need to save the altered image.

The following code is from the source referenced above. I modified variable and object names to be consistent with my programming and added some clean up code.

Sub apiRotateImage(strSourcePath As String, strDestPath As String, lngAngle As Long) ' Rotates image in path strSourcePath, returns image rotated to lngAngle (must be 90, 180 or 270) degrees and saves image at strDestPath

Dim objSource As Object
Dim objProcess As Object

If lngAngle <> 90 And lngAngle <> 180 And lngAngle <> 270 Then
    MsgBox "Reqiested angle of " + lngAngle & "° is not valid.  Only 90 180 and 270 are allowed", , "apiRotateImage Invalid Entry Value"
    Exit Sub
End If
Set objSource = CreateObject("WIA.ImageFile") 'create WIA objects
Set objProcess = CreateObject("WIA.ImageProcess")
objProcess.Filters.Add objProcess.FilterInfos("RotateFlip").filterid 'setup filter
objProcess.Filters(1).Properties("RotationAngle") = lngAngle
objSource.LoadFile strSourcePath 'load image
Set objSource = objProcess.Apply(objSource) 'apply change
objSource.SaveFile strDestPath 'save new image
Set objSource = Nothing
Set objProcess = Nothing

End Sub

The calling code provides the source and destination paths and the desired rotation angle, then calls the following procedure to load the the rotated image.

Public Function DisplayImage(ctlImageControl As Control, strFilePath As Variant) As Boolean On Error GoTo Err_DisplayImage

Dim bolResult As Boolean

bolResult = False With ctlImageControl If IsNull(strFilePath) Then 'No path, hide image box .Picture = "" Else ' Display image in box and make it visible .Picture = strFilePath bolResult = True End If .Visible = bolResult End With

Exit_DisplayImage: Exit Function

Err_DisplayImage: Select Case Err.Number Case 2220 ' Can't find the picture. ctlImageControl.Visible = False Resume Exit_DisplayImage: Case Else ' Some other error. MsgBox Err.Number & " " & Err.Description Resume Exit_DisplayImage: End Select End Function

This code works. It seemed logical to me that an image object returned from the Windows API could be loaded into an Access image control, but I could not find a way to do this directly. Creating a file on disk seemed to me to be a work-around, so I thought I would ask.

  • I doubt can be loaded directly. What you are doing is likely the only way. – June7 Dec 18 '21 at 22:54
  • I've found that the image control requires either a url or a file/picture linked at design time. however the attachment control is more flexible. With that in mind try using an attachment variable: like here https://stackoverflow.com/questions/14478860/add-attachment-via-vba-in-ms-access – mazoula Dec 21 '21 at 05:53
  • Please provide enough code so others can better understand or reproduce the problem. – Community Dec 26 '21 at 17:53

0 Answers0