Solved

Uploaded picture sometimes blurry

Posted on 2007-12-05
2
765 Views
Last Modified: 2012-08-14
Hello, I need some advise and direction on my code below or a better way to accomplish my goal of allowing a user to upload an image to the server. When the image is uploaded I need to create a thumbnail and ensure the image is no wider than 400px. I'm using the code below which functions correctly for me but some users get blurry results for the image and thumb. I use photoshop for my picture adjustment and have no idea what others use. I have them convert their pictures to 400px width before uploading and still they sometimes come out blurry. I take the original picture and adjust in photoshop and bam it looks great!

Any advice or tweeks to my code would be greatly apreciated!

Here is what I'm working with:
Private Sub InsertPicture1()
        'Insert imageurl and username to db
        If fuPicture1.HasFile Then
            If ddlEventGroup.Visible = True Then

                Dim EventGroup As String = ddlEventGroup.Text.Trim
                Dim ImageUrL As String = (EventGroup + "_" + fuPicture1.FileName)
                Dim myConnection As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("~/App_Data/LandT.mdb"))
                Dim ad As OleDbDataAdapter = New OleDbDataAdapter("insert into EventPhotos ([EventGroup], [ImageURL]) values ('" & Replace(EventGroup, "'", "''") & "', '" & Replace(ImageUrL, "'", "''") & "')", myConnection)
                Dim ds As DataSet = New DataSet()
                ad.Fill(ds)

                'upload the picture
                Dim fileName As String = Server.HtmlEncode(fuPicture1.FileName)
                Dim extension As String = System.IO.Path.GetExtension(fileName)
                Dim newFileName As String = ddlEventGroup.Text + "_" + fileName
                Dim Path As String = Server.MapPath("~/EventPictures/" & newFileName)

                If (extension = ".jpeg") Or (extension = ".jpg") Or (extension = ".gif") Or (extension = ".JPEG") Or (extension = ".JPG") Or (extension = ".GIF") Then
                    fuPicture1.SaveAs(Path)

                    CreateImage(Path, Server.MapPath("~/EventPictures/Image/" & newFileName))
                    CreateThumbnail(Path, Server.MapPath("~/EventPictures/Thumb/" & newFileName))

                    lblErrorMessage.Text = ""
                Else
                    lblErrorMessage.Text = "Sorry, but the picture must be a .jpg, .gif"
                End If
                'delete original picture
                Dim PictueName As String = (ddlEventGroup.Text + "_" + fuPicture1.FileName)

                Dim filePath As String = ("~/EventPictures/" + PictueName)
                File.Delete(Server.MapPath(Trim(filePath)))

            Else
                If txtEventGroup.Visible = True Then
                    Dim EventGroup As String = txtEventGroup.Text.Trim

                    Dim ImageUrL As String = (EventGroup + "_" + fuPicture1.FileName)
                    Dim myConnection As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("~/App_Data/LandT.mdb"))
                    Dim ad As OleDbDataAdapter = New OleDbDataAdapter("insert into EventPhotos ([EventGroup], [ImageURL]) values ('" & Replace(EventGroup, "'", "''") & "', '" & Replace(ImageUrL, "'", "''") & "')", myConnection)
                    Dim ds As DataSet = New DataSet()
                    ad.Fill(ds)

                    'upload the picture
                    Dim fileName As String = Server.HtmlEncode(fuPicture1.FileName)
                    Dim extension As String = System.IO.Path.GetExtension(fileName)
                    Dim newFileName As String = txtEventGroup.Text + "_" + fileName
                    Dim Path As String = Server.MapPath("~/EventPictures/" & newFileName)

                    If (extension = ".jpeg") Or (extension = ".jpg") Or (extension = ".gif") Or (extension = ".JPEG") Or (extension = ".JPG") Or (extension = ".GIF") Then
                        fuPicture1.SaveAs(Path)

                        CreateImage(Path, Server.MapPath("~/EventPictures/Image/" & newFileName))
                        CreateThumbnail(Path, Server.MapPath("~/EventPictures/Thumb/" & newFileName))

                        lblErrorMessage.Text = ""
                    Else
                        lblErrorMessage.Text = "Sorry, but the picture must be a .jpg, or .gif"
                    End If
                    'delete original picture
                    Dim PictueName As String = (txtEventGroup.Text + "_" + fuPicture1.FileName)

                    Dim filePath As String = ("~/EventPictures/" + PictueName)
                    File.Delete(Server.MapPath(Trim(filePath)))

                Else
                End If
            End If
        End If
       
    End Sub

    Public Sub CreateImage(ByVal srcpath As String, ByVal destpath As String)

        Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(srcpath)
        Dim imgRatio As Double = img.Width / img.Height
        Dim tnWidth As Double = 400
        Dim tnHeight As Double = 400
        If imgRatio > 1 Then
            tnHeight = (400 * img.Height) / img.Width
        Else
            tnWidth = (400 * img.Width) / img.Height
        End If
        Dim Image As System.Drawing.Image = img.GetThumbnailImage(tnWidth, tnHeight, Nothing, New System.IntPtr(0))
        Image.Save(destpath, ImageFormat.Jpeg)

        img.Dispose()
        Image.Dispose()

    End Sub

    Public Sub CreateThumbnail(ByVal srcpath As String, ByVal destpath As String)

        Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(srcpath)
        Dim imgRatio As Double = img.Width / img.Height
        Dim tnWidth As Double = 100
        Dim tnHeight As Double = 100
        If imgRatio > 1 Then
            tnHeight = (100 * img.Height) / img.Width
        Else
            tnWidth = (100 * img.Width) / img.Height
        End If
        Dim Thumb As System.Drawing.Image = img.GetThumbnailImage(tnWidth, tnHeight, Nothing, New System.IntPtr(0))
        Thumb.Save(destpath, ImageFormat.Jpeg)
        img.Dispose()
        Thumb.Dispose()
    End Sub
0
Comment
Question by:gogetsome
2 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
ID: 20411468
The GetThumbnailImage method uses the embedded thumbnail of the image if there is one. The embedded thumbnail is rather small, so when it's enlarged to 400 pixels, it gets blurry.

Instead of using the GetThumbnailImage method, create a new Bitmap object with the desired size, use Graphics.FromImage to create a Graphics object for drawing on the Bitmap, then use the DrawImage method to draw the original image onto the Bitmap.

Dim image As New System.Drawing.Bitmap(tnWidth, tnHeight);

Dim gr As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(image);

gr.DrawImage(img, 0, 0, tnWidth, tnHeight);

gr.Dispose()

image.Save(destpath, ImageFormat.Jpeg)

image.Dispose()

Open in new window

0
 

Author Comment

by:gogetsome
ID: 20412524
Thanks GreenGhost! That sounds like what I'm looking for. Have a tough time converting to VB.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remove greater than sign 3 42
ASP.NET Core index.html  LOCALHOST page cannot be found 2 22
Error in JQuery 5 36
Entity Framework 7 28
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now