Link to home
Start Free TrialLog in
Avatar of mextine
mextine

asked on

Howto change image resolution in .Net

I'm reading a series of images from a MS SQL table and saving them to a directory. These are staff ID pictures from our security card app. Once I've extracted the ID photo from the security app to disk, I need to reference the disk file in our student app. As part of the process, I'm resizing the image and changing its resolution from 96 to 72 dpi. If they are not at a 72 dpi resolution, the student app freezes. The resizing works without a hitch, but I cannot figure out how to reduce the resolution. All of the output files continue to be 96 dpi. Any help would be greatly appreciated.

Here's the code

<snip...>
Dim bytImage() As Byte
bytImage = .ItemArray(4)
bytImage = ChangeImageResolution(bytImage, 72) bytImage = ResizeImage(bytImage, 150) Dim fs As New FileStream(strFileName, FileMode.OpenOrCreate, FileAccess.Write) fs.Write(bytImage, 0, UBound(bytImage))
fs.Close()
fs = Nothing
<snip...>
Private Function ChangeImageResolution(ByVal bytInput As Byte(), ByVal intOutputResolution As Int16) As Byte()
    Dim strmInput As New System.IO.MemoryStream(bytInput)
    Dim strmOutput As New System.IO.MemoryStream
    Dim imgInput As System.Drawing.Image
    Dim imgOutput As System.Drawing.Image
    Dim bmapTemp As Bitmap

    ' Create a temporary bitmap and set to output resolution
    imgInput = System.Drawing.Image.FromStream(strmInput)
    bmapTemp = imgInput
    bmapTemp.SetResolution(intOutputResolution, intOutputResolution)
    imgOutput = New Bitmap(bmapTemp)
    imgOutput.Save(strmOutput, System.Drawing.Imaging.ImageFormat.Jpeg)

    ' Return the output image in a byte array
    ChangeImageResolution = strmOutput.ToArray End Function Private Function ResizeImage(ByVal bytInput As Byte(), ByVal intFinalMaxDim As Int16) As Byte()
    Dim strmInput As New System.IO.MemoryStream(bytInput)
    Dim strmOutput As New System.IO.MemoryStream
    Dim imgInput As System.Drawing.Image
    Dim imgOutput As System.Drawing.Image
    Dim intMaxDim As Int16 = 0
    Dim dblResizePercentage As Double = 0
    Dim sizResize As New Size

    ' Determine the resizing percentage based on current image dimensions
    imgInput = System.Drawing.Image.FromStream(strmInput)
    If imgInput.Height >= imgInput.Width Then
        intMaxDim = imgInput.Height
    Else
        intMaxDim = imgInput.Width
    End If
    dblResizePercentage = (intFinalMaxDim / intMaxDim)
    With sizResize
        .Width = CInt(imgInput.Width * dblResizePercentage)
        .Height = CInt(imgInput.Height * dblResizePercentage)
    End With

    ' Create a new resized version of the image
    imgOutput = New Bitmap(imgInput, sizResize.Width, sizResize.Height)
    imgOutput.Save(strmOutput, System.Drawing.Imaging.ImageFormat.Jpeg)

    ' Return the output image in a byte array
    ResizeImage = strmOutput.ToArray
End Function

ASKER CERTIFIED SOLUTION
Avatar of aponcealbuerne
aponcealbuerne

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial