Solved

Trying to resize an image in a memoryStream

Posted on 2010-11-10
9
725 Views
Last Modified: 2013-12-06
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

Open in new window

0
Comment
Question by:TheWebGuy38
  • 4
  • 3
9 Comments
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 34112237
Can't say for sure about what is causing your problem.  But having worked with code to scale images myself, I'm thinking the issue is going to be with the scale Factor.

Since you say the problem is this images greater than 300dpi, I'm thinking that because you are trying to limit the MAX size of the image (in pixels) in the code, that if you have a fixed size image (in inches) at various DPI settings, that you're getting pixelation when the actual image is much larger than the MAX size in pixels.

In other word, the code seems to be setting the MAX size of the image to be 800 PIXELS in either dimension.  Therefore, at 300DPI, you can't start with an image larger than 2-2/3" is size without causing the image to shrink.

What is the size (in pixels) of these images that are 300 DPI?  If I'm thinking about this correctly, then the real issue is that your scale factor less than 1 for these images.
0
 

Author Comment

by:TheWebGuy38
ID: 34117667
One image that does it is:

1280 x 720
24-bit color
72 PPI
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 34120748
From what I can see of a quick review of the code is that the MAX size is 800x800.  That means your 1280 x 720 image has GOT to loose some fidelity because you are going to shrink it down to 800 x 450.  That is a change from 921,600 pixels to 360,000 pixels, or a 60% reduction to total size.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:TheWebGuy38
ID: 34125927
I know it should loose resolution and dpi, but it shouldn't look like this


Image after uploaded
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 34137396
Have you tried modifiying the Interpolation mode?
According the MSDN, Bicubic is "...not suitable for shrinking an image below 25 percent of its original size".  I assume they mean 25% of the linear size.  In your case, shinking an image from 1280 to 800 is a 37.5% reduction in linear size.  (My previous statement of 60% was % of AREA).
http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode.aspx
0
 

Author Comment

by:TheWebGuy38
ID: 34235125
Anyone have any ideas?
0
 

Accepted Solution

by:
TheWebGuy38 earned 0 total points
ID: 34280358
Here is the Correct working code


Dim fileLen As Integer
        Dim myStream As System.IO.Stream
        Dim ImgContentType As String
        Dim UserName As String = "testUpload"
        Application("SysUserDir") = "Users"

        Dim FileUploadPicture As FileUpload = FileUploadInsertPicture

        'Response.Write(FileUploadPicture.HasFile)
        'Response.End()

        If FileUploadPicture.HasFile Then
            'Create Dir
            Dim nd As New DirectoryInfo(Server.MapPath("\" + Application("SysUserDir") & "\" & UserName & "\test\Picture\"))
            If nd.Exists = False Then
                nd.Create()
            End If

            'Create Dir
            Dim nd2 As New DirectoryInfo(Server.MapPath("\" + Application("SysUserDir") & "\" & UserName & "\test\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 As String = FileUploadPicture.PostedFile.FileName

            Dim Path1 As String = Server.MapPath("\" + Application("SysUserDir") & "\" & UserName & "\test\Picture\" & Filename)
            Dim DataPath1 As String = "/" & UserName & "/test/Picture/" & Filename

            Dim Path2 As String = Server.MapPath("\" + Application("SysUserDir") & "\" & UserName & "\test\PictureThumb\" & Filename)
            Dim DataPath2 As String = "/" & UserName & "/test/PictureThumb/" & Filename


            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 As Image = 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.Bitmap = New Bitmap(imgWidth1, imgHeight1)

                Dim graphicsImage As Graphics = Graphics.FromImage(objThumbnail)
                graphicsImage.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                graphicsImage.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
                graphicsImage.DrawImage(PictureInfoLarge, 0, 0, imgWidth1, imgHeight1)
                graphicsImage.Dispose()

                'PictureInfoLarge.GetThumbnailImage(imgWidth1, imgHeight1, Nothing, System.IntPtr.Zero)


                ''---------------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)

                'e.Values("Picture") = DataPath1

                PictureInfoLarge.Dispose()
                objThumbnail.Dispose()

                Dim PictureInfoSmall As Image = System.Drawing.Image.FromStream(myStream)

                'Get Picture To be Resized small
                Dim maxWidth2 As Integer = 150
                Dim maxHeight2 As Integer = 150
                'imgHeight, imgWidth As Integer
                'Dim PictureInfo

                Dim imgHeight2 As Integer = PictureInfoSmall.Height
                Dim imgWidth2 As Integer = 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)
                Dim objThumbnail2 As System.Drawing.Bitmap = New Bitmap(imgWidth2, imgHeight2)

                Dim graphicsImage2 As Graphics = Graphics.FromImage(objThumbnail2)
                graphicsImage2.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
                graphicsImage2.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                graphicsImage2.DrawImage(PictureInfoSmall, 0, 0, imgWidth2, imgHeight2)

                ''---------------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, imgWidth2, imgHeight2)
                ''--------------------------------------------------------------------
                'graphicsImage2.Save()

                objThumbnail2.Save(Path2, Imaging.ImageFormat.Jpeg)

                graphicsImage2.Dispose()

                'e.Values("PictureThumb") = DataPath2

                myStream.Close()
                myStream.Dispose()

                PictureInfoSmall.Dispose()
                objThumbnail2.Dispose()

            End If


        ElseIf Not FileUploadPicture.HasFile Then


        End If

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 34505839
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Advice in Xamarin 21 78
specific questions about shipping a coat 4 40
move expression web site to a new server 12 29
show form submit button based on jquery sum 5 27
Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 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