Link to home
Start Free TrialLog in
Avatar of DebbieDRR
DebbieDRR

asked on

Poor Quality On Uploaded Images

I am trying to resize a posted file and then upload it to my server.  I can resize it and upload it, but once its there the quality is very poor.  Can someone tell me what I need to add or change in my code so the image quality is high.
Thanks

<%@ Page Language="vb" aspcompat=true debug=true%>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script language="VB" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
Response.Expires = 60
dim imgHeight, imgWidth, h, w, xInt, yInt, optRatio, imgRatio, optWidth, optHeight, cngWidth, cngHeight
dim conn, rs, cmd, prm, c, ft, errFile, i, m, x, photosArray(4,7), ErrorMsg, imgURL, perChange
Dim thumbNailImg as System.Drawing.Image
Dim fullSizeImg as System.Drawing.Image
optWidth = 115
optHeight = 100
optRatio = optWidth\optHeight
Try
      imgURL = myPath + myFileName
      dim g as System.Drawing.Image = System.Drawing.Image.FromStream(flPosOne.PostedFile.InputStream)
      dim thisFormat = g.rawformat
      if thisformat.equals(system.drawing.imaging.imageformat.Gif) then
            response.contenttype="image/gif"
      else
            response.contenttype="image/jpeg"
      end if

      imgHeight = g.Height
      imgWidth = g.Width
      imgRatio = imgWidth/imgHeight
      if imgRatio > 1 then
            cngWidth = optWidth
            perChange = optWidth/imgWidth
            cngHeight = imgHeight * perChange
      Else
            cngHeight = optHeight
            perChange = optHeight/imgHeight
            cngWidth = imgWidth * perChange                        
      End If
      
      dim imgOutput as New Bitmap(g, cngWidth, cngHeight)
      imgOutput.save(imgURL, thisformat)
      g.dispose()
      imgOutput.dispose()
      catch Exp as exception
            Response.Write(exp)
End Try
End Sub
</script>
ASKER CERTIFIED SOLUTION
Avatar of AgentSmith007
AgentSmith007

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
Avatar of AgentSmith007
AgentSmith007

Actually, just try replacing your code with this:

Sub Page_Load(sender as Object, e as EventArgs)
Response.Expires = 60
dim imgHeight, imgWidth, h, w, xInt, yInt, optRatio, imgRatio, optWidth, optHeight, cngWidth, cngHeight
dim conn, rs, cmd, prm, c, ft, errFile, i, m, x, photosArray(4,7), ErrorMsg, imgURL, perChange
Dim thumbNailImg as System.Drawing.Image
Dim fullSizeImg as System.Drawing.Image
optWidth = 115
optHeight = 100
optRatio = optWidth\optHeight
Try
     imgURL = myPath + myFileName
     dim g as System.Drawing.Image = System.Drawing.Image.FromStream(flPosOne.PostedFile.InputStream)
     dim thisFormat = g.rawformat
     if thisformat.equals(system.drawing.imaging.imageformat.Gif) then
          response.contenttype="image/gif"
     else
          response.contenttype="image/jpeg"
     end if

     imgHeight = g.Height
     imgWidth = g.Width
     imgRatio = imgWidth/imgHeight
     if imgRatio > 1 then
          cngWidth = optWidth
          perChange = optWidth/imgWidth
          cngHeight = imgHeight * perChange
     Else
          cngHeight = optHeight
          perChange = optHeight/imgHeight
          cngWidth = imgWidth * perChange                    
     End If
     
dim imgOutput as New Bitmap(g, cngWidth, cngHeight)
 'Microsoft code for setting JPEG quality level
        Dim eps As EncoderParameters = New EncoderParameters(1)
        eps.Param(0) = New EncoderParameter(Encoder.Quality, imageQuality)
        Dim ici As ImageCodecInfo = GetEncoderInfo("image/jpeg")
        Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbort
        dummyCallBack = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)

     imgOutput.save(imgURL, ".jpg",ici,eps)

     g.dispose()
     imgOutput.dispose()
     catch Exp as exception
          Response.Write(exp)
End Try
End Sub

    'More Microsoft code for setting JPEG quality level
    Private Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
        Dim j As Integer
        Dim encoders As ImageCodecInfo()
        encoders = ImageCodecInfo.GetImageEncoders()
        For j = 0 To encoders.Length
            If encoders(j).MimeType = mimeType Then
                Return encoders(j)
            End If
        Next j
        Return Nothing
    End Function

    Function ThumbnailCallback() As Boolean
        Return False
    End Function