Click here to Skip to main content
16,014,677 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

can any one tell me how to calculate image dpi by using resolution of an image?

what is the formula for the dpi calculation?

please help me.

Thank you.

What I have tried:

VB
Dim Image As Image = Image.FromFile(Image_Path)
Dim unit As GraphicsUnit = GraphicsUnit.Point
Dim rect As RectangleF = Image.GetBounds(unit)
Dim hres As Single = Image.HorizontalResolution
Dim vres As Single = Image.VerticalResolution
Posted
Updated 9-May-16 0:56am
v4
Comments
Peter_in_2780 9-May-16 3:33am    
"dpi" = dots per inch, i.e. pixels per 25.4mm. This is a function of the display hardware, not the image. Some images may contain hints as to the intended display size/resolution, but they are only ever hints.
kasunthilina 9-May-16 5:34am    
'found it.
Dim dx As Single, dy As Single
Dim imageFile1 As Image = Image.FromFile(SourcePath)
Dim g As Graphics = Graphics.FromImage(imageFile1)

Try
dx = g.DpiX '// selected file DPI
dy = g.DpiY

Finally
g.Dispose()
End Try

The Image.HorizontalResolution Property (System.Drawing)[^] and Image.VerticalResolution Property (System.Drawing)[^] already gives you the DPI (horizontal and vertical).
However, as Peter_in_2780 pointed out, that is just a hint.
 
Share this answer
 
Quote:
can any one tell me how to calculate image dpi by using resolution of an image?
You don't one does not imply the other.

HorizontalResolution and VerticalResolution are image size in pixels.
The DPI setting is used to reduce the size of physical printing.
printed Width = HorizontalResolution / DPI
printed Height = VerticalResolution / DPI

Understanding DPI, Resolution and Print vs. Web Images[^]
 
Share this answer
 
C#
'found it.
Dim dx As Single, dy As Single
Dim imageFile1 As Image = Image.FromFile(SourcePath)
Dim g As Graphics = Graphics.FromImage(imageFile1)

Try
dx = g.DpiX '// selected file DPI
dy = g.DpiY

Finally
g.Dispose()
End Try
 
Share this answer
 
Comments
Patrice T 9-May-16 7:00am    
Use Accept answer to close the question when solved.
kasunthilina 9-May-16 7:11am    
thank you for the help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900