Loading tif images by vba

BurqanKpln

New Member
Joined
Jul 21, 2023
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi,
I am struggling about the loading tif format of the image by vba.
Is there any way to get a tiff image to display on a vba form without having to convert them all to a different format?
We have an image library storing images in Tiff format. The VBA Image object does not display images in tiff format. But due to the nature of the library, I cannot convert them for the purpose of my project.

Image1.Picture = LoadPicture(FullFileName_of_image.tif)

Would be grateful for any positive guidance.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
If an Image Control will not accept a tif format, you'll have to do something else.
or batch convert with Irfanview, which is a free and very good program, into a Temp folder.
with a macro you can also insert picture into sheet, save it to a folder as a jpg type and import it into your image control.
Assume you have a UserForm with an Image Control and a Command Button.
Change all references as and where required.
Code:
Private Sub CommandButton1_Click()
'Thanks to Dante Amor
Dim sPath As String
Dim Pict As Picture
Dim tempChartObj As ChartObject
sPath = "C:\Picture Folder Name Here"    '<---- Change
    With Application.FileDialog(msoFileDialogFilePicker)
        .Title = "Select pict"     '<---- Change ?
        .InitialFileName = sPath
            If .Show Then
                On Error Resume Next: ActiveSheet.Pictures("Temp_Pic").Delete: On Error GoTo 0
                Set Pict = ActiveSheet.Pictures.Insert(.SelectedItems.Item(1))
                    With Pict
                        .Name = "Temp_Pic"
                    End With
            End If
    End With
Application.ScreenUpdating = False
Set tempChartObj = Sheets("Sheet1").ChartObjects.Add(100, 100, Pict.Width, Pict.Height)
Sheets("Sheet1").Shapes("Temp_Pic").Cut
DoEvents
tempChartObj.Chart.ChartArea.Select
DoEvents
tempChartObj.Chart.Paste
tempChartObj.Chart.Export sPath & "\Temp_Pic.jpg"
tempChartObj.Delete
Me.Image1.Picture = LoadPicture(sPath & "\Temp_Pic.jpg")
Image1.PictureSizeMode = fmPictureSizeModeStretch
Application.ScreenUpdating = True
End Sub
 
Upvote 0
VBA - WIA - Convert the Image Format | DEVelopers HUT
A VBA Function which converts images to another file format using WIA. It can convert to BMP, GIF, JPEG, PNG or TIFF formats. www.devhut.net

The tricky thing about relying on Daniel P's blog is that he has recently gone back and forth about removing his website and all the data on it, and now appears to have put it back. Watch this space, it would seem.

The VBA Image object does not display images in tiff format.
This is true, but you can use what is called the WIA COM Object. I wrote a few guides on how to display more modern image formats in VBA here: Working with images in VBA - Displaying PNG files

The same methods in that apply to TIFF files, so using the function below:

VBA Code:
Function LoadImage(ByVal Filename As String) As StdPicture
        With CreateObject("WIA.ImageFile")
                .LoadFile Filename
                Set LoadImage = .FileData.Picture
        End With
End Function

you would just rewrite your code as:

VBA Code:
Image1.Picture = LoadImage("FullFileName_of_image.tif")

In this thread, I wrote a class module which adds further functionality - like resizing, rotating, and - to your point about converting the TIF files - converting any loaded image file into one of the other compatible formats (PNG, BMP, GIF, JPG, TIF), if you were so minded... Resize Image inside a Image Control on userform

The only slightly tricky thing about TIF files is that they can contain multiple pages - is that an issue in your case? Will you need to be able to access specific pages within the TIF image file?
 
Upvote 0
Thanks Dan.
I don't know much, if anything at all, about this but I have something to experiment with now.
I sure do appreciate it.
What I showed is more or less a stop gap I would say until someone with more understanding of it, like yourself, comes around and shows how it should be done.
Thanks again Dan.
Regards
 
Upvote 0
Thanks Dan.
I don't know much, if anything at all, about this but I have something to experiment with now.
I sure do appreciate it.
What I showed is more or less a stop gap I would say until someone with more understanding of it, like yourself, comes around and shows how it should be done.
Thanks again Dan.
Regards
Hi - if it weren't for learning about the WIA COM Object, I would have absolutely no idea how to deal with a TIF Image file, and frankly, I would probably have done exactly what you proposed. It's a tried and true method.

I've written two threads on the topic - basically compiling all my research on the topic. They are both linked above. If you (or anyone) is ever interested in CREATING a TIF image file (with multiple pages), I have written a routine here and provided sample images:
For completeness (because I know the mods want us to include the code here rather than just referencing it), this referenced code is:

VBA Code:
Sub CreateTIFF()
    
    Dim Page1   As Object
    Dim Page2   As Object
    Dim Page3   As Object
    Dim IMG     As Object
    
    Set Page1 = CreateObject("WIA.ImageFile")
    Set Page2 = CreateObject("WIA.ImageFile")
    Set Page3 = CreateObject("WIA.ImageFile")
    Set IMG = CreateObject("WIA.ImageProcess")
    
    Page1.LoadFile "D:\page1.png"
    Page2.LoadFile "D:\page2.png"
    Page3.LoadFile "D:\page3.png"
    
    ' Add a page/frame filter - Page 2
    IMG.Filters.Add IMG.FilterInfos("Frame").FilterID
    Set IMG.Filters(IMG.Filters.Count).Properties("ImageFile") = Page2
    
    ' Add a page/frame filter - Page 3
    IMG.Filters.Add IMG.FilterInfos("Frame").FilterID
    Set IMG.Filters(IMG.Filters.Count).Properties("ImageFile") = Page3
    
    ' Add a converter filter for the TIFF file format
    IMG.Filters.Add IMG.FilterInfos("Convert").FilterID
    IMG.Filters(IMG.Filters.Count).Properties("FormatID") = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}"
    
    ' Apply the filters to the first image / page 1
    Set Page1 = IMG.Apply(Page1)
    
    ' Save Page 1 as a TIFF file
    Page1.SaveFile "D:\IDislikeTIF.tif"
    
    Set Page1 = Nothing
    Set Page2 = Nothing
    Set Page3 = Nothing
    Set IMG = Nothing
End Sub
 
Upvote 0
Hi all,
I was the one created this topic but forgot the tell you my special thanks! Now my I can show load my tif files on my userform. I am importing my Xray images to the form. Some of them are really dark and hard to differentiate the ones with the pixel failures. Do we have a chance to tune brightness and contrast of the images with i.e. spin button? THANKS A LOT FOR EVERYONE
 
Upvote 0
Hi - it can be done, yes, but the easier the method, the longer it will take.

1. WIA COM Object - if you use the WIA COM Object, which is what I've used above, it's arguably the easiest approach in terms of code because when you load the TIF image, it also gets all the pixel color data (ARGB data values). You can adjust the brightness in a somewhat rudimentary way by going through each of the pixels, and recalculating them, and given the way that the WIA COM object was engineered by Microsoft, it's arbitrarily slower than it ought to be for [insert reasons here].

2. Win32 APIs - GetPixel/SetPixel - this is trickier, but might(?) be a bit faster. Not sure. Haven't tested, but I've written a demo of it in the past - screen capture below. It takes a couple of seconds to produce the sample image.
1690816640123.png


3. Win32 APIs - DIBs - a much faster approach, but I'd need to check to see if I have something already to hand on that. I can check when I get home after work.

4. Excel native functions - a lesser known/appreciated method is using Excel's native image manipulation functionality. Here, I'm referring to the Corrections button in the Picture tab:
1690816824535.png

There is a way of executing this same functionality in VBA. It's a little rudimentary and not ideal, but it's another possibility.
 
Upvote 0
Hi,
I decided to follow the second option. I tried the code given on your link on the picture: VBnet™ Visual Basic Developers Resource Centre
There are some methods like Image1.hDC, Image1.ScaleWidth, Image1.ScaleHeight, et. Those are not recognized by vba. Do you have a chance to check it?
Sorry I am a little bit beginner in this area :)

VBA Code:
Private Sub CommandButton4_Click()
'variables for brightness, colour calculation, positioning
   Dim Brightness As Single
   Dim NewColour As Long
   Dim pixHdc As Long
   Dim x As Long, y As Long
   Dim r As Integer, g As Integer, b As Integer
   
  'change the brightness to a percent
   Brightness = CSng(Val(TextBox1.Text) / 100)
   
   pixHdc = Image1.hDC
   
  'run a loop through the picture to change every pixel
   For x = 0 To Image1.ScaleWidth
   
      For y = 0 To Image1.ScaleHeight
     
        'get the current colour value
         NewColour = GetPixel(pixHdc, x, y)
      
        'extract the R,G,B values from the long returned by GetPixel
         r = (NewColour Mod 256)
         b = (Int(NewColour \ 65536))
         g = ((NewColour - (b * 65536) - r) \ 256)
      
        'change the RGB settings to their appropriate brightness
         r = r * Brightness
         b = b * Brightness
         g = g * Brightness
      
        'make sure the new variables aren't too high or too low
         If r > 255 Then r = 255
         If r < 0 Then r = 0
         If b > 255 Then b = 255
         If b < 0 Then b = 0
         If g > 255 Then g = 255
         If g < 0 Then g = 0
      
        'set the new pixel
         SetPixelV pixHdc, x, y, RGB(r, g, b)
      
     'continue through the loop
      Next y
      
     'refresh the picture box
     '(a nice visual progress effect)
      Image1.Refresh
   
   Next x
   
  'final picture refresh
   Image1.Refresh
End Sub
 
Upvote 0
Yes, those things don't exist in VBA - it's code designed for VB6. Same language, but different controls, etc. As per the screen capture, you see that the demo I wrote references vbaPicturebox - that's a project I've been working on which recreates the VB6 control, so that the code like the one above will work in VBA. But it's still a work in progress. But it is very slow.
A few questions - how many TIF images do you need to process/view? Why is it that you can't convert them to a different image type? That might make things quicker/easier.
I was going to look into digging out the code for option 3, but I need to wait until I get home later.
 
Upvote 0

Forum statistics

Threads
1,215,234
Messages
6,123,773
Members
449,123
Latest member
StorageQueen24

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top