views:

316

answers:

3

This is the code i got from This Link

I want the user to upload a picture and then resize it.............

Public Sub ResizeFromStream(ByVal ImageSavePath As String, ByVal MaxSideSize As Integer, ByVal Buffer As System.IO.Stream)

    Dim intNewWidth As Integer
    Dim intNewHeight As Integer
    Dim imgInput As System.Drawing.Image = System.Drawing.Image.FromStream(Buffer)


    'Determine image format
    Dim fmtImageFormat As ImageFormat = imgInput.RawFormat

    'get image original width and height
    Dim intOldWidth As Integer = imgInput.Width
    Dim intOldHeight As Integer = imgInput.Height

    'determine if landscape or portrait
    Dim intMaxSide As Integer

    If (intOldWidth >= intOldHeight) Then
        intMaxSide = intOldWidth
    Else
        intMaxSide = intOldHeight
    End If

    If (intMaxSide > MaxSideSize) Then
        'set new width and height
        Dim dblCoef As Double = MaxSideSize / CDbl(intMaxSide)

        intNewWidth = Convert.ToInt32(dblCoef * intOldWidth)
        intNewHeight = Convert.ToInt32(dblCoef * intOldHeight)

    Else

        intNewWidth = intOldWidth
        intNewHeight = intOldHeight
    End If
    'create new bitmap
    Dim bmpResized As Drawing.Bitmap = New Drawing.Bitmap(imgInput, intNewWidth, intNewHeight)

    'save bitmap to disk
    bmpResized.Save(ImageSavePath, fmtImageFormat)

    'release used resources
    imgInput.Dispose()
    bmpResized.Dispose()
    Buffer.Close()

End Sub

Now when i click on my submit button it must execute my code but i am not sure what the input must be for the Buffer field?

Protected Sub btnUpload_Click() Handles btnUpload.Click

     ResizeFromStream("~Pics", 200, ??????????)

End Sub

Thanks in advanced!

Edit I need to get my Image from the File Upload control!

A: 

Hi there.

You could pass a stream object like this:

Dim fs As New FileStream("C:\file.jpg", FileMode.Open)

ResizeFromStream("~Pics", 200, fs)

So the code would be performing IO on the file 'file.jpg'. This is a very rough example, but as Jon Skeet asked, the location of where the image is coming from does matter. My example is just simple 'get you started' type.

Cheers. Jas.

Jason Evans
But i want to get it from my FileUpload control called FileUpload1
Etienne
Jason Evans
A: 

FileUpload.FileContent get's a stream for the file content.

AndrewVos
So you saying i must do this inside my btnUpload_Click method? ResizeFromStream("~Pics", 200, myFile.FileContent)Because when i do this nothing is happening, file does not get uploaded.
Etienne
Yes.You forgot to put in a filename.ResizeFromStream("~Pics\FILENAME.JPG", 200, myFile.FileContent)(You can get the original filename from FileUpload I think)
AndrewVos
Yes, from FileUpload.FileName. So ResizeFromStream(Path.Combine("~Pics", FileUpload.FileName), 200, myFile.FileContent)
cdm9002
Seems right. Did you get this working?
AndrewVos
A: 

Is is possible for this block of code to output as resized version of the Stream that was being inputted? If so, do how will I do it?

Thanks!