[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Howto change image resolution in .Net

Posted on 2007-10-01
2
Medium Priority
?
2,179 Views
Last Modified: 2013-12-04
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

0
Comment
Question by:mextine
1 Comment
 
LVL 4

Accepted Solution

by:
aponcealbuerne earned 500 total points
ID: 20003005
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this Micro Tutorial viewers will learn how to remove an unwanted object using Photoshop’s feature known as content-aware fill.
Learn how to download your full Prezi presentation for offline presenting. Prezi doesn’t have to be viewed and shared in a web browser, even with a free account you can download your full presentation to share with others. Be sure to download any vi…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question