Results 1 to 9 of 9

Thread: resolution (dpi) info

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    resolution (dpi) info

    I'm trying to retrive resolution info from a jpg file. I was able to obtain width and height info but could not find code to access resolution (dpi) info.

    ' size info
    Dim img = Image.FromFile(path)
    MsgBox(img.Size.Width)
    MsgBox(img.Size.Height)

    Do you know the code to change the resolution and size? Shrinking a file from 300dpi to 72dpi.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: resolution (dpi) info

    dpi is not a property of the image, it's a property of the screen on which the image is shown. The width and height of an image are measured in pixels, pure and simple. dpi merely tells you how many pixels it takes to fill an inch of screen.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: resolution (dpi) info

    Image resolution (dpi) and image size (width and height in pixels) are independent of one another. Most methods in GDI+ ignore the resolution. For example, if you show an image in a PictureBox the displayed size depends only on its Width and Height properties. But the Graphics.DrawImageUnscaled method does respect the image resolution as well as the pixel size, as do various printing functions.

    You can get the image resolution (dpi) from the Image.HorizontalResolution and Image.VerticalResolution properties. You can set the resolution using the Bitmap.SetResolution method. The following example sets the resolution to 72,72 and the pixel size to 600*400:

    Code:
    Dim bmp As New Bitmap(img, 600, 400)
    bmp.SetResolution(72, 72)
    img = bmp
    BB

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    Re: resolution (dpi) info

    When you edit an image say in photoshop, there's a command that allows adjusting image size. The width, height and resolution is adjustable. How do I replicate that same operation using VB 2008? If the image size is unchanged and the resolution if dropped down (ie 300dpi to 72 dpi), the file size changes because information is loss. I'm writing a script that will change all the high resolution photos I put in a folder to a lower resolution web quality image.

  5. #5
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: resolution (dpi) info

    The dpi resolution is just a pair of numbers stored in the file. Changing them doesn't have a direct effect on the image display size or quality. But there is another factor which does affect quality. That is the jpeg compression quality (%). Changing that compression affects both the clarity of the image and the size of the file. It depends a bit on the image, but decreasing the jpeg quality can often lead to a big saving in file size without much visible deterioration.

    In your program, you probably you need to use compression quality in combination with changing the pixel size in order to optimize the file size. Here's a method to save an image as a jpeg file with a chosen compression quality:
    Code:
    Imports System.Drawing.Imaging
    '....
    
    	Dim jpegCodec As ImageCodecInfo
    
    	Public Sub Save(ByVal path As String, ByVal img As Image, ByVal quality As Integer)
    		If quality <= 0 Then quality = 0 : If quality >= 100 Then quality = 100
    		Dim qParam As New EncoderParameter(Encoder.Quality, quality)
    		If jpegCodec Is Nothing Then
    			jpegCodec = ImageCodecInfo.GetImageEncoders.Where _
    			  (Function(e As ImageCodecInfo) e.MimeType = "image/jpeg")(0)
    		End If
    		Dim encoderParams As New EncoderParameters(1)
    		encoderParams.Param(0) = qParam
    		Try
    			img.Save(path, jpegCodec, encoderParams)
    		Catch
    			MessageBox.Show("Unable to save PNG image to path " & path)
    		End Try
    	End Sub
    I think you can afford to ignore the dpi resolution for your present purposes. Or at most, use it to calculate a desired print size in inches instead of a pixel size.

    BB

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    Re: resolution (dpi) info

    boops boops,

    Thank you for your compression code. I'd like to use your code and incorporate it to the sizing code I'm using below. I'm not sure how to integrate the two.

    **********************************
    Dim i As Integer
    i = DataGridView1.CurrentRow.Index
    If DataGridView1.Item(1, i).Value <> DataGridView1.Item(8, i).Value Then
    DataGridView1.Item(7, i).Value = DataGridView1.Item(5, i).Value * (DataGridView1.Item(6, i).Value) / DataGridView1.Item(4, i).Value
    Dim img = Image.FromFile(ListBox1.Text & "\" & DataGridView1.Item(1, i).Value)
    Dim w, h As Integer
    w = DataGridView1.Item(6, i).Value
    h = DataGridView1.Item(7, i).Value
    Dim bmp As Bitmap = New Bitmap(w, h)
    Using g As Graphics = Graphics.FromImage(bmp)
    g.DrawImage(img, 0, 0, bmp.Width, bmp.Height)
    End Using

    bmp.Save(ListBox1.Text & "\" & DataGridView1.Item(8, i).Value, System.Drawing.Imaging.ImageFormat.Jpeg)

  7. #7
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: resolution (dpi) info

    I don't know how PhotoShop does it, but programs I have used allow the user to choose the jpeg quality in order to keep the file size down. Is that what you have in mind? BB

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    Re: resolution (dpi) info

    The code you provided varies the jpeg quality by adjusting the compression. The code I'm using below allows resizing the image. How do I resize an image, compress the jpeg and save it to a new file?

    Dim bmp As Bitmap = New Bitmap(w, h)
    Using g As Graphics = Graphics.FromImage(bmp)
    g.DrawImage(img, 0, 0, bmp.Width, bmp.Height)
    End Using
    bmp.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg)

  9. #9
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: resolution (dpi) info

    Suppose you have a trackbar (TrackBar1) with a range from 1 to 100 for the user to choose the compression %. Replace your bmp.Save line by a call to the Save sub I gave you:

    Code:
    Save(path, bmp, TrackBar1.Value)
    BB

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width