Passing an image memorystream to byte

Last post 05-11-2007 4:56 PM by smcoxon. 5 replies.

Sort Posts:

  • Passing an image memorystream to byte

    05-10-2007, 2:31 PM
    • Member
      4 point Member
    • rich_frost
    • Member since 05-01-2007, 9:33 PM
    • Posts 30

    Hi all!

    I have created an image upload feature on my application that worked well; by taking the image, converting it to a stream then saving the data as a byte in my db.

     I now want to resize the image before upload, resizing any image over 500px wide.  I have created the function, but whenever I convert the stream (now a MemoryStream rather than my original stream.IO) all of the bytes are 0.

    My code is as follows:

    ImageStream = file.InputStream
    objImage = System.Drawing.Image.FromStream(ImageStream, True)
    
    imgWidth = objImage.Width
    imgHeight = objImage.Height
    
    If imgWidth > 500 Then
       ResizedHeight = objImage.Height / (objImage.Width / 500)
       objThumbnail = objImage.GetThumbnailImage(500, ResizedHeight, Nothing, System.IntPtr.Zero)
    End If
    
    Dim ThumbStream As New MemoryStream
    objThumbnail.Save(ThumbStream, Imaging.ImageFormat.Jpeg)
    
    Dim uploadedFile(ThumbStream.Length) As Byte
    ThumbStream.Read(uploadedFile, 0, ThumbStream.Length)
    

     Any clues where I've gone wrong?

    Cheers

  • Re: Passing an image memorystream to byte

    05-10-2007, 5:10 PM
    • Contributor
      3,396 point Contributor
    • smcoxon
    • Member since 12-02-2006, 8:43 AM
    • Oxford UK
    • Posts 582

    Hi, take a look at my earlier post at:

    http://forums.asp.net/thread/1594882.aspx

    Hope it helps.

    Regards

    smcoxon

    Regards
    Smcoxon

    No Gem is ever polished without some friction.
  • Re: Passing an image memorystream to byte

    05-10-2007, 5:26 PM
    • Member
      4 point Member
    • rich_frost
    • Member since 05-01-2007, 9:33 PM
    • Posts 30

    Thanks for the post. I've read the thread, but I can't seem to resolve my problem.

    The previous thread saves the image to the filesystem - although I am saving to a stream I imagine this shouldn't be too different; but converting it to bytes still seems to cause problems.

    Using the WatchWindow the bytes are coming out Byte[0] = 0; Byte[1] = 0 ..........

  • Re: Passing an image memorystream to byte

    05-11-2007, 8:49 AM
    Answer
    • Contributor
      3,396 point Contributor
    • smcoxon
    • Member since 12-02-2006, 8:43 AM
    • Oxford UK
    • Posts 582

    Not exactly sure but look at using ToArray instead of length. 

    Dim uploadedFile(ThumbStream.ToArray) As Byte

    Regards
    Smcoxon

    No Gem is ever polished without some friction.
  • Re: Passing an image memorystream to byte

    05-11-2007, 3:13 PM
    • Member
      4 point Member
    • rich_frost
    • Member since 05-01-2007, 9:33 PM
    • Posts 30

    Hey man, thanks for the hint....so easy when you know how!

    Here is the working code:

     

    'Format image
    Dim OriginalBM As Bitmap = New Bitmap(file.InputStream)
    
    imgWidth = OriginalBM.Width
    imgHeight = OriginalBM.Height
    
    Dim stream As New IO.MemoryStream
    
    If imgWidth > 500 Then
     ResizedHeight = OriginalBM.Height / (OriginalBM.Width / 500)
     Dim newSize As Size = New Size(500, ResizedHeight)
     Dim ResizedBM As Bitmap = New Bitmap(OriginalBM, newSize)
    
     ResizedBM.Save(stream, Imaging.ImageFormat.Jpeg)
    End If

     If you're looking to put this data into a db, then just use stream.ToArray as the parameter input value

    Happy coding!

  • Re: Passing an image memorystream to byte

    05-11-2007, 4:56 PM
    • Contributor
      3,396 point Contributor
    • smcoxon
    • Member since 12-02-2006, 8:43 AM
    • Oxford UK
    • Posts 582

    Glad it worked.

    Smcoxon

    Regards
    Smcoxon

    No Gem is ever polished without some friction.
Page 1 of 1 (6 items)