troubleshooting Question

Trying to resize an image in a memoryStream

Avatar of TheWebGuy38
TheWebGuy38 asked on
Anti-Virus AppsVisual Basic ClassicWeb Development
8 Comments1 Solution771 ViewsLast Modified:
I wrote a piece of code that grabs a fileupload, puts it into a memory stream, converts the main pic, and thumbnail pic, then saves them as jpegs, then updates the database.

Everything works fine, except one little glitch. If the DPI is above 300 then the image doesn't save right. It get's pixelated and blury. If anyone knows what is doing this, or how to solve it, I'd greatly appreciate the help.

here is the code.


#  Dim fileLen As Integer
#         Dim myStream As System.IO.Stream
#         Dim ImgContentType As String
#         Dim UserName = Dal.GetNickName(Dal.GetUserID)
#  
#         Dim FileUploadPicture As FileUpload = SuperFormAquariums.FindControl("FileUploadEditPicture")
#  
#         'Response.Write(FileUploadPicture.HasFile)
#         'Response.End()
#  
#         If FileUploadPicture.HasFile Then
#             'Create Dir
#             Dim nd As New DirectoryInfo(Server.MapPath("\" + Application("SysUserDir") & "\" & UserName & "\Aquariums\Picture\"))
#             If nd.Exists = False Then
#                 nd.Create()
#             End If
#  
#             'Create Dir
#             Dim nd2 As New DirectoryInfo(Server.MapPath("\" + Application("SysUserDir") & "\" & UserName & "\Aquariums\PictureThumb\"))
#             If nd2.Exists = False Then
#                 nd2.Create()
#             End If
#  
#  
#  
#             fileLen = FileUploadPicture.PostedFile.ContentLength
#             ImgContentType = FileUploadPicture.PostedFile.ContentType
#  
#             Dim Input(fileLen) As Byte
#             myStream = FileUploadPicture.PostedFile.InputStream
#             myStream.Read(Input, 0, fileLen)
#  
#             'Response.Write(fileLen)
#             'Response.End()
#  
#             Dim Filename = FileUploadPicture.PostedFile.FileName
#  
#             Dim Path1 = Server.MapPath("\" + Application("SysUserDir") & "\" & UserName & "\Aquariums\Picture\" & Filename)
#             Dim DataPath1 = "/" & UserName & "/Aquariums/Picture/" & Filename
#             Dim OldPath1 = Server.MapPath("/" & Application("SysUserDir") & "/" & e.OldValues("Picture"))
#  
#             Dim Path2 = Server.MapPath("\" + Application("SysUserDir") & "\" & UserName & "\Aquariums\PictureThumb\" & Filename)
#             Dim DataPath2 = "/" & UserName & "/Aquariums/PictureThumb/" & Filename
#             Dim OldPath2 = Server.MapPath("/" & Application("SysUserDir") & "/" & e.OldValues("PictureThumb"))
#  
#  
#             'Response.Write(DataPath1 & " " & DataPath2)
#             'Response.End()
#             'delete old image
#  
#             'delete old files
#             If FileUploadPicture.HasFile Then
#                 Dim fd1 As New FileInfo(OldPath1)
#                 If fd1.Exists = True Then
#                     fd1.Delete()
#                 End If
#             End If
#  
#             If FileUploadPicture.HasFile Then
#                 Dim fd2 As New FileInfo(OldPath2)
#                 If fd2.Exists = True Then
#                     fd2.Delete()
#                 End If
#             End If
#  
#  
#  
#  
#  
#             If File.Exists(Path1) Or File.Exists(Path2) Then
#  
#                 'LabelPhotos.Text = "Photo has not been added to the system"
#                 LabelConfirm.Text = "This photo image already exists in the system"
#                 e.Cancel = True
#  
#             Else
#  
#  
#  
#                 LabelConfirm.Text = ""
#                 Dim PictureInfoLarge = System.Drawing.Image.FromStream(myStream)
#  
#  
#  
#  
#                 'Get Picture To be Resized large
#                 Dim maxWidth1 As Integer = 800
#                 Dim maxHeight1 As Integer = 800
#                 Dim imgHeight1, imgWidth1 As Integer
#                 'Dim PictureInfo
#  
#  
#  
#                 imgHeight1 = PictureInfoLarge.Height
#                 imgWidth1 = PictureInfoLarge.Width
#  
#  
#                 ' Determine Scaling
#                 If imgWidth1 >= maxWidth1 Or imgHeight1 >= maxHeight1 Then
#                     'Determine what dimension is off by more
#                     Dim deltaWidth1 As Integer = imgWidth1 - maxWidth1
#                     Dim deltaHeight1 As Integer = imgHeight1 - maxHeight1
#                     Dim scaleFactor1 As Double
#  
#                     If deltaHeight1 > deltaWidth1 Then
#                         'Scale by the height
#                         scaleFactor1 = maxHeight1 / imgHeight1
#                     Else
#                         'Scale by the Width
#                         scaleFactor1 = maxWidth1 / imgWidth1
#                     End If
#  
#                     imgWidth1 *= scaleFactor1
#                     imgHeight1 *= scaleFactor1
#  
#                 Else
#  
#  
#                 End If
#  
#                 'Dim objThumbnail As System.Drawing.Image = PictureInfoLarge.GetThumbnailImage(imgWidth1, imgHeight1, Nothing, System.IntPtr.Zero)
#                 Dim objThumbnail As System.Drawing.Bitmap = PictureInfoLarge.GetThumbnailImage(imgWidth1, imgHeight1, Nothing, System.IntPtr.Zero)
#                 ' Dim graphicsImage As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(objThumbnail)
#  
#  'this is where the glitch is I think---------------supposed to fix -----------------------------------
#                 Dim graphicsImage As Graphics
#                 graphicsImage = Graphics.FromImage(objThumbnail)
#                 graphicsImage.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
#                 graphicsImage.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
#                 graphicsImage.DrawImage(objThumbnail, 0, 0, imgWidth1, imgHeight1)
#                 '--------------------------------------------------------------------
#                 graphicsImage.Save()
#  
#                 objThumbnail.Save(Path1, Imaging.ImageFormat.Jpeg)
#  
#                 'Response.Write(objThumbnail.PixelFormat & "<BR><BR>")
#                 'Response.Write(objThumbnail.PhysicalDimension)
#                 'Response.Write(objThumbnail.Size)
#  
#                 'Response.End()
#  
#  
#                 graphicsImage.Dispose()
#  
#                 e.NewValues("Picture") = DataPath1
#  
#                 PictureInfoLarge = Nothing
#                 objThumbnail = Nothing
#  
#  
#                 Dim PictureInfoSmall = System.Drawing.Image.FromStream(myStream)
#  
#  
#                 'Get Picture To be Resized small
#                 Dim maxWidth2 = 150
#                 Dim maxHeight2 = 150
#                 'imgHeight, imgWidth As Integer
#                 'Dim PictureInfo
#  
#  
#  
#                 Dim imgHeight2 = PictureInfoSmall.Height
#                 Dim imgWidth2 = PictureInfoSmall.Width
#  
#                 ' Determine Scaling
#                 If imgWidth2 >= maxWidth2 Or imgHeight2 >= maxHeight2 Then
#                     'Determine what dimension is off by more
#                     ' Dim deltaWidth As Integer = imgWidth - maxWidth
#                     'Dim deltaHeight As Integer = imgHeight - maxHeight
#                     ' Dim scaleFactor As Double
#                     Dim deltaWidth2 As Integer = imgWidth2 - maxWidth2
#                     Dim deltaHeight2 As Integer = imgHeight2 - maxHeight2
#                     Dim scaleFactor2 As Double
#  
#                     If deltaHeight2 > deltaWidth2 Then
#                         'Scale by the height
#                         scaleFactor2 = maxHeight2 / imgHeight2
#                     Else
#                         'Scale by the Width
#                         scaleFactor2 = maxWidth2 / imgWidth2
#                     End If
#  
#                     imgWidth2 *= scaleFactor2
#                     imgHeight2 *= scaleFactor2
#  
#                 Else
#  
#                 End If
#  
#  
#                 Dim objThumbnail2 As System.Drawing.Bitmap = PictureInfoSmall.GetThumbnailImage(imgWidth2, imgHeight2, Nothing, System.IntPtr.Zero)
#  
#                 '---------------supposed to fix -----------------------------------
#                 Dim graphicsImage2 As Graphics
#                 graphicsImage2 = Graphics.FromImage(objThumbnail2)
#                 graphicsImage2.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
#                 graphicsImage2.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
#                 graphicsImage2.DrawImage(objThumbnail2, 0, 0, imgWidth1, imgHeight1)
#                 '--------------------------------------------------------------------
#                 graphicsImage2.Save()
#  
#                 objThumbnail2.Save(Path2, Imaging.ImageFormat.Jpeg)
#  
#                 graphicsImage2.Dispose()
#  
#  
#                 e.NewValues("PictureThumb") = DataPath2
#  
#  
#                 myStream.Close()
#                 myStream.Dispose()
#  
#                 PictureInfoSmall = Nothing
#                 objThumbnail2 = Nothing
#  
#  
#  
#  
#  
#  
#  
#             End If
#  
#  
#         ElseIf Not FileUploadPicture.HasFile Then
#  
#             'Dim PictureOld = CType(SuperFormAquariums.FindControl("HiddenFieldPictureEdit"), HiddenField).Value
#             'Dim PictureThumbOld = CType(SuperFormAquariums.FindControl("HiddenFieldPictureThumbedit"), HiddenField).Value
#             'Response.Write("new:" & e.NewValues("Picture") & " " & e.NewValues("PictureThumb") & " OLD: " & e.OldValues("Picture") & " " & e.OldValues("PictureThumb"))
#             'Response.End()
#  
#             e.NewValues("Picture") = e.OldValues("Picture")
#  
#             e.NewValues("PictureThumb") = e.OldValues("PictureThumb")
#  
#         End If
ASKER CERTIFIED SOLUTION
TheWebGuy38

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros