Solved

Using Saveas() method for Ajax asyncupload using VB.NET

Posted on 2010-08-22
4
543 Views
Last Modified: 2013-12-26
I am having trouble to find a VB.NET tutorial on how to use the SaveAs() method for the Ajax AsyncUpload control.

What I need to do when the upload is complete is:
  - I have to either pass a parameter or access a session variable (call the variable "pictype")
  - I need to validate what type the file is ("picformat" - ex: GIF or JPG)
  - I need to validate the width of the image, and rescale it to no more than 150px wide
  - I  need to save the image as a new file name which includes the session variable and fileformat (ex: filename = "pic" & session("pictype") & "01" & "." & picformat  

I need help with these points in my subroutine.

Thanks, Peter
0
Comment
Question by:pbissegger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 33530030
0
 

Author Comment

by:pbissegger
ID: 33555266
Although this is in C (not VB), I think I can figure out the SaveAs() functionality from this. Thank you.

Do you have any idea how to address the other points in my message (filetype ? picture width ?)

Thanks, Peter
0
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 500 total points
ID: 33555947
check this post regarding image:

http://forums.asp.net/p/1572277/3943629.aspx
0
 

Accepted Solution

by:
pbissegger earned 0 total points
ID: 33653368
OK, your last post didn't help ,e out that much, but I have bridged the gap between your inputs and a final solution. Please find attached my final solution.
Sub RescaleImage(ImageFilename as String)
    Nx = 150 ' max width for large thumbnail
    Ny = 150 ' max height for large thumbnail
        
    ' Load the Image
    Dim originalimg As System.Drawing.Image = System.Drawing.Image.FromFile(trim(ImageFilename)) 

    ' Work out the width/height for the thumbnail. Preserve aspect ratio and honour max width/height
    ' Note: if the original is smaller than the thumbnail size it will be scaled up
        
    If originalimg.Width <> Nx then

        ' Need to rescale image
        
        Try
            If (originalimg.Width / Nx) > (originalimg.Width / Ny) Then
                N2 = originalimg.Width
                newWidth = Nx
                newHeight = originalimg.Height * (Nx / N2)
                If newHeight > Ny Then
                    newWidth = newWidth * (Ny / newHeight)
                    newHeight = Ny
                End If
            Else
                N2 = originalimg.Height
                newHeight = Ny
                newWidth = originalimg.Width * (Ny / N2)
                If newWidth > Nx Then
                    newHeight = newHeight * (Nx / newWidth)
                    newWidth = Nx
                End If
            End If
	
            Dim thumb As New Bitmap(newWidth, newHeight)
	
            'Create a graphics object for the thumbnail
            Dim gr_dest As Graphics = Graphics.FromImage(thumb)
	
            ' just in case it's a transparent GIF force the bg to white
            Dim sb = New SolidBrush(System.Drawing.Color.White)
            gr_dest.FillRectangle(sb, 0, 0, thumb.Width, thumb.Height)
	
            ' Re-draw the image to the specified height and width
            gr_dest.DrawImage(originalimg, 0, 0, thumb.Width, thumb.Height)
 
            thumb.Save(ImageFilename, originalimg.RawFormat)
                
            ' House Keeping !
            If Not thumb Is Nothing Then
                thumb.Dispose()
                thumb = Nothing
            End If

            If Not originalimg Is Nothing Then
                originalimg.Dispose()
                originalimg = Nothing
            End If           
                
        Catch
            Outputmessage = "Sorry, there was a problem saving the image."
        End Try

End If
    
End Sub

Protected Sub AsyncFileUpload1_UploadedComplete(ByVal sender As Object, ByVal e As EventArgs)
    If AsyncFileUpload1.HasFile then
        ' Determine new file name
        fileExtension = System.IO.Path.GetExtension(AsyncFileUpload1.Filename).ToLower()
        newfilename = "elogo-" & Session("sponsor") & fileExtension
        FolderToSave = Server.MapPath("\") & "Userdata\L-" & Trim(Session("Leaguenum")) & "\Advertisement\"

        ' Validate file is of correct type
        fileOK = False
        Dim allowedExtensions As String() = {".jpg", ".jpeg", ".png", ".gif"}
        For i As Integer = 0 To allowedExtensions.Length - 1
            If fileExtension = allowedExtensions(i) Then
                fileOK = True
            End If
        Next
        
        ' Save the file    
        If fileOK Then
            Try
                AsyncFileUpload1.PostedFile.SaveAs(trim(FolderToSave & newfilename))
                Outputmessage = "File uploaded!"
            Catch ex As Exception
                Outputmessage = "File could not be uploaded."
            End Try
        Else
            Outputmessage = "Cannot accept files of this type."
        End If

        ' Update database entry to indicate new banner name
            cmd.Connection = MyConn
            cmd.Connection.Open()     
            cmd.CommandText = "Update sponsors set sponsorlogoeng = '" & newfilename & "' where sponsorid = " & Session("sponsor")
            cmd.ExecuteNonQuery()
            cmd.Connection.Close()

        ' Display the output message
        upload1Label.Text = Outputmessage
    End If

    ' RescaleImage(trim(FolderToSave & newfilename))

    englishlogo.ImageUrl = Foldertosave & newfilename
    Showenglishlogoimages.Visible = "true"

End Sub

Open in new window

0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

728 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