Answered by:
Converting to stream after resize for SQL Insert

Question
-
User1418935505 posted
Hey guys,
I'm running with this.
Public Function ImageData(ByVal fu As FileUpload) As Byte() If fu.HasFile Then Dim imageBytes As Byte() = New Byte(fu.PostedFile.InputStream.Length) {} fu.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length) Return imageBytes Else Return Nothing End If End Function Public Function ImageResize(ByVal fu As FileUpload) As Bitmap Dim thumbWidth As Integer = 1024 Dim image As System.Drawing.Image = image.FromStream(fu.PostedFile.InputStream) 'Create a System.Drawing.Bitmap with the desired width and height of the thumbnail. Dim srcWidth As Integer = image.Width Dim srcHeight As Integer = image.Height Dim thumbHeight As Integer = (srcHeight / srcWidth) * thumbWidth Dim bmp As New Bitmap(thumbWidth, thumbHeight) 'Create a System.Drawing.Graphics object from the Bitmap which we will use to draw the high quality scaled image Dim gr As Graphics = Graphics.FromImage(bmp) 'Set the System.Drawing.Graphics object property SmoothingMode to HighQuality gr.SmoothingMode = Drawing2D.SmoothingMode.HighQuality 'Set the System.Drawing.Graphics object property CompositingQuality to HighQuality gr.CompositingQuality = Drawing2D.CompositingQuality.HighQuality 'Set the System.Drawing.Graphics object property InterpolationMode to High gr.InterpolationMode = Drawing2D.InterpolationMode.High ' Draw the original image into the target Graphics object scaling to the desired width and height Dim rectDestination As New Rectangle(0, 0, thumbWidth, thumbHeight) gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel) Return bmp 'Save to destination file 'bmp.Save(Server.MapPath("~/images/" & FileUpload1.PostedFile.FileName)) ' dispose / release resources bmp.Dispose() image.Dispose() End Function
I need to convert the file to stream, and then resize, and then insert into my SQL table. I've gone back to the start to try and work it out as I've spent hours trying to find the missing piece.Any help is appreciated.
Thanks!
Tuesday, March 16, 2010 2:44 AM
Answers
-
User1439985827 posted
Hello,
You could call the .Save() method on your Bitmap object and save it to a MemoryStream. You can then use the Read() method, respectively, to persist it to your database as a Blob.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 16, 2010 3:55 AM
All replies
-
User1439985827 posted
Hello,
You could call the .Save() method on your Bitmap object and save it to a MemoryStream. You can then use the Read() method, respectively, to persist it to your database as a Blob.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 16, 2010 3:55 AM -
User1418935505 posted
Cool, thanks!
I understand where that slots in now.
Should work like so?
Dim ms As New MemoryStream() bmp.Save(ms, ImageFormat.Jpeg)
Tuesday, March 16, 2010 10:23 AM -
User1418935505 posted
I decided to combine both of my functions, and it seems to work well, however when I run the function, everything adds to the table like it should, yet for some reason every time I run the function to pull the image up, the format can't read.
Public Function ImageData(ByVal fu As FileUpload) As Byte() If fu.HasFile Then Dim thumbWidth As Integer = 1024 Dim image As System.Drawing.Image = image.FromStream(fu.PostedFile.InputStream) 'Create a System.Drawing.Bitmap with the desired width and height of the thumbnail. Dim srcWidth As Integer = image.Width Dim srcHeight As Integer = image.Height Dim thumbHeight As Integer = (srcHeight / srcWidth) * thumbWidth Dim bmp As New Bitmap(thumbWidth, thumbHeight) 'Create a System.Drawing.Graphics object from the Bitmap which we will use to draw the high quality scaled image Dim gr As Graphics = Graphics.FromImage(bmp) 'Set the System.Drawing.Graphics object property SmoothingMode to HighQuality gr.SmoothingMode = Drawing2D.SmoothingMode.HighQuality 'Set the System.Drawing.Graphics object property CompositingQuality to HighQuality gr.CompositingQuality = Drawing2D.CompositingQuality.HighQuality 'Set the System.Drawing.Graphics object property InterpolationMode to High gr.InterpolationMode = Drawing2D.InterpolationMode.High ' Draw the original image into the target Graphics object scaling to the desired width and height Dim rectDestination As New Rectangle(0, 0, thumbWidth, thumbHeight) gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel) Dim ms As New MemoryStream() bmp.Save(ms, ImageFormat.Bmp) Dim imageBytes As Byte() = New Byte(fu.PostedFile.InputStream.Length) {} ms.Read(imageBytes, 0, imageBytes.Length - 1) Return imageBytes Else Return Nothing End If End Function
Tuesday, March 16, 2010 10:43 AM