• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

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>
0
DebbieDRR
Asked:
DebbieDRR
  • 2
1 Solution
 
AgentSmith007Commented:
I got this code from a microsoft page that lets you set JPEG Quality level


'PUT BELOW CODE IN YOUR SAVING ROUTINE...There's a bit of extra stuff in here, but i'm just copying my old function.
'this line sets the JPG quality level
        dim imageQuality as integery = 80
        '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)


        Dim objBitmap As New Bitmap(imageWidth, imageHeight)
        Dim objGraphic As Graphics = Graphics.FromImage(objBitmap)
        Dim sourceRect As Rectangle
        sourceRect.Width = imageWidth
        sourceRect.Height = imageHeight
        objGraphic.DrawImage(fullSizeImg, sourceRect)
        'Apply watermark if image width is over X pixels
        If imageWidth > 200 Or imageHeight > 200 Then
            Dim imgWatermark As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("watermark.png"))
            'Place watermark in bottom-right corner
            objGraphic.DrawImage(imgWatermark, imageWidth - (imgWatermark.Width + 65), imageHeight - (imgWatermark.Height + 20))
        End If
        objBitmap.Save(Server.MapPath("../../images/") & strBaseName & strSuffix & ".jpg", ici, eps)
        'Cleanup
        fullSizeImg.Dispose()
        objBitmap.Dispose()
        objGraphic.Dispose()

'PUT ABOVE CODE IN SAVING ROUTINE

'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
0
 
AgentSmith007Commented:
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now