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(bytI mage, 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(ByVa l bytInput As Byte(), ByVal intOutputResolution As Int16) As Byte()
Dim strmInput As New System.IO.MemoryStream(byt Input)
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.FromS tream(strm Input)
bmapTemp = imgInput
bmapTemp.SetResolution(int OutputReso lution, intOutputResolution)
imgOutput = New Bitmap(bmapTemp)
imgOutput.Save(strmOutput, System.Drawing.Imaging.Ima geFormat.J peg)
' 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(byt Input)
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.FromS tream(strm Input)
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.Ima geFormat.J peg)
' Return the output image in a byte array
ResizeImage = strmOutput.ToArray
End Function
Here's the code
<snip...>
Dim bytImage() As Byte
bytImage = .ItemArray(4)
bytImage = ChangeImageResolution(bytI
fs.Close()
fs = Nothing
<snip...>
Private Function ChangeImageResolution(ByVa
Dim strmInput As New System.IO.MemoryStream(byt
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.FromS
bmapTemp = imgInput
bmapTemp.SetResolution(int
imgOutput = New Bitmap(bmapTemp)
imgOutput.Save(strmOutput,
' 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(byt
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.FromS
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,
' Return the output image in a byte array
ResizeImage = strmOutput.ToArray
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.