
12-21-2011, 01:34 PM
|
|
Newcomer
|
|
Join Date: Dec 2011
Posts: 1
|
|
Image upload
|
Hi can someone please tell me where i am going wrong I am trying to upload images into a access database and resizing them
Public Overrides Sub GetPhoto1()
' Retrieve the value entered by the user on the Photo1 ASP:FileUpload, and
' save it into the Photo1 field in DataSource PropPics record.
' Custom validation should be performed in Validate, not here.
If Not Me.Photo1.PostedFile is Nothing then
If Me.Photo1.PostedFile.FileName.Length > 0 AndAlso Me.Photo1.PostedFile.ContentLength > 0 Then
' Retrieve the file contents and store them in Photo1 field.
Me.DataSource.Parse(MiscUtils.GetFileContent(Me.Photo1.PostedFile), PropPicsTable.Photo1)
End If
End If
Const bmpW As Integer = 300 'New image target width
Const bmpH As Integer = 200 'New Image target height
If Photo1.HasFile Then
Dim newWidth As Integer = bmpW
Dim newHeight As Integer = bmpH
Dim Pic() As Byte = Me.DataSource.Photo1
' Convert to image for resizing
Dim Img As Bitmap = Nothing
Img = CType(Bitmap.FromStream(New MemoryStream(Pic, 0, Pic.Length - 1)), Drawing.Bitmap)
Dim newImg As Bitmap = New Bitmap(newWidth, newHeight, Imaging.PixelFormat.Format24bppRgb)
newImg.SetResolution(72, 72)
'Get the uploaded image width and height
Dim upWidth As Integer = Img.Width
Dim upHeight As Integer = Img.Height
Dim newX As Integer = 0 'Set the new top left drawing position on the image canvas
Dim newY As Integer = 0
Dim reDuce As Decimal
'Keep the aspect ratio of image the same if not 4:3 and work out the newX and newY positions
'to ensure the image is always in the center of the canvas vertically and horizontally
If upWidth > upHeight Then 'Landscape picture
reDuce = CDec(newWidth / upWidth)
'calculate the width percentage reduction as decimal
newHeight = CInt(Int(upHeight * reDuce))
'reduce the uploaded image height by the reduce amount
newY = CInt(Int((bmpH - newHeight) / 2))
'Position the image centrally down the canvas
newX = 0 'Picture will be full width
ElseIf upWidth < upHeight Then 'Portrait picture
reDuce = CDec(newHeight / upHeight)
'calculate the height percentage reduction as decimal
newWidth = CInt(Int(upWidth * reDuce))
'reduce the uploaded image height by the reduce amount
newX = CInt(Int((bmpW - newWidth) / 2))
'Position the image centrally across the canvas
newY = 0 'Picture will be full hieght
ElseIf upWidth = upHeight Then 'square picture
reDuce = CDec(newHeight / upHeight)
'calculate the height percentage reduction as decimal
newWidth = CInt(Int(upWidth * reDuce))
'reduce the uploaded image height by the reduce amount
newX = CInt(Int((bmpW - newWidth) / 2)) 'Position the image centrally across the canvas
newY = CInt(Int((bmpH - newHeight) / 2)) 'Position the image centrally down the canvas
End If
'Create a new image from the uploaded picture using the Graphics class
'Clear the graphic and set the background color to white
'Use Antialias and High Quality Bicubic to maintain a good quality picture
'Save the new bitmap image using 'jpg' picture format and the calculated canvas positioning
Dim newGraphic As Graphics = Graphics.FromImage(newImg)
newGraphic.Clear(Color.White)
newGraphic.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
newGraphic.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
newGraphic.DrawImage(Img, newX, newY, newWidth, newHeight)
Dim stream As MemoryStream = New MemoryStream
newImg.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
Me.DataSource.Photo1 = stream.ToArray ' assign back to picture
stream.Close()
Img.Dispose()
newImg.Dispose()
newGraphic.Dispose()
End If
End Sub
|
|