Image upload
Image upload
Image upload
Image upload
Image upload
Image upload Image upload Image upload Image upload Image upload Image upload Image upload Image upload
Image upload Image upload
Image upload
Go Back  Xtreme Visual Basic Talk > > > Image upload


Reply
 
Thread Tools Display Modes
  #1  
Old 12-21-2011, 01:34 PM
nick5798 nick5798 is offline
Newcomer
 
Join Date: Dec 2011
Posts: 1
Default 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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Image upload
Image upload
Image upload Image upload
Image upload
Image upload
Image upload Image upload Image upload Image upload Image upload Image upload Image upload
Image upload
Image upload
 
Image upload
Image upload
 
-->