How to insert a picture with info into excel

Trial_and_error

Board Regular
Joined
Feb 15, 2005
Messages
53
Hello,

Is it possible with a macro to insert not only a picture in excel, but also the width and height of the original picture. The width and height properties can be displayed with the attributes in windows explorer (as dimensions), so this info must be captured somewhere. Another possibility perhaps could be to extract it via windows paint. It doesn't matter wether width or height would be extracted as pixels, mm, inch or cm.

Any info on this could help me a lot for my application!!

:unsure:
 

Some videos you may like

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Trial_and_error,

Some tests (Windows 98SE, Excel XP) learned me that every pasted shape (tested gif and bmp) had displayed Width and Height values exactly 3/4 of the values found in Bitmap.

can this give you a start?

kind regards,
Erik

Code:
Option Explicit

Sub insert_picture()
Dim shape_address As String, cell As Range, msg As String
shape_address = "D:\evg\flower.gif" 'or bmp ...

Set cell = Range("C65536").End(xlUp).Offset(1, 0)
cell.Offset(0, -2).Select
ActiveSheet.Pictures.Insert(shape_address).Name = "rrr"
With ActiveSheet.Pictures("rrr")
cell.Hyperlinks.Add Anchor:=cell, Address:=shape_address, TextToDisplay:=shape_address
cell.Offset(0, 1) = .Height
cell.Offset(0, 2) = .Width
Columns().AutoFit
End With

End Sub
 

Trial_and_error

Board Regular
Joined
Feb 15, 2005
Messages
53
Thanks Eric for your reply,

There are some parts in the macro which gave me some good ideas and which can be very useful for my application. I have run the macro several times to try to understand the functionning of it because I'm not an expert at all in this matter. Unfortunately I believe that the macro returns the width and height of the picture in "font points" (for width) and points for height. Actually, what I'm really looking for is the height and width IN PIXELS from the ORIGINAL picture and I don't think this can be retrieved by the macro, is it? People told me that perhaps API is a solution for me? Anyway, thanks for the macro example and additionnal help is always welcome.
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
You wrote
I believe that the macro returns the width and height of the picture in "font points" (for width) and points for height. Actually, what I'm really looking for is the height and width IN PIXELS from the ORIGINAL picture and I don't think this can be retrieved by the macro, is it?

in my first post there was
Some tests (Windows 98SE, Excel XP) learned me that every pasted shape (tested gif and bmp) had displayed Width and Height values exactly 3/4 of the values found in Bitmap.
so you could do this
cell.Offset(0, 1) = .Height *4/3
cell.Offset(0, 2) = .Width *4/3


or is it not always the same proportion 3 = Excel /4 = Original ?

kind regards,
Erik

EDIT:
API: don't even know what those three characters mean
 

Trial_and_error

Board Regular
Joined
Feb 15, 2005
Messages
53

ADVERTISEMENT

Hello Eric,

I believe I paid to much attention to the macro you sent earlier and not to the remark that the pixel size should be 4/3 of the dimension for height and width. I did some tests with jpg photos from 2 different camera's and I found indeed a constant relationship between width and pixel size. For my 1st camera, the relationship width/horizontal pixel size = 0.4805. The photos taken by this 1st cam are taken with a resolution of 150x150 dpi. So, I suppose that an explanation for the ratio of 0.4805 can be explained by the fact that photos are transferred to 72 dpi in excel (I had read this somewhere). And 150dpi/72dpi equals 0.48.
The same is true for my second camera (taking photos at 96 dpi) but then the ratio is 0.75. I will do some more tests next week at work but I believe the macro shall be very valuable for my application.

One more question : Is it possible to insert several photos on the sheet? I tried to insert a few pictures to the same sheet and this works fine but only the width and height size for the 1st inserted photo is correct. The other photos receive exactly the same values for height and width as the 1st one.

Anyway, thanks for the help!
Kind regards
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Is it possible to insert several photos on the sheet? I tried to insert a few pictures to the same sheet and this works fine but only the width and height size for the 1st inserted photo is correct. The other photos receive exactly the same values for height and width as the 1st one.
Don't hardcode the name given to your pictures.
ActiveSheet.Pictures.Insert(shape_address).Name = "rrr"
With ActiveSheet.Pictures("rrr")

(not tested: just typed here, but should work)
picture_nr = Application.WorkSheetFunction.Counta(Columns(1))
ActiveSheet.Pictures.Insert(shape_address).Name = picture_nr


kind regards,
Erik
 

Watch MrExcel Video

Forum statistics

Threads
1,122,719
Messages
5,597,734
Members
414,170
Latest member
Mdm

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
Top