Answered by:
Image format unknown after Size Change

Question
-
User1418935505 posted
Hey, guys.
I've got this.
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. If image.Width > thumbWidth Then 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.Jpeg) Dim imageBytes As Byte() = New Byte(fu.PostedFile.InputStream.Length) {} ms.Read(imageBytes, 0, imageBytes.Length - 1) Return imageBytes ms.Close() ms.Dispose() Else Dim imageBytes As Byte() = New Byte(fu.PostedFile.InputStream.Length) {} fu.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length - 1) Return imageBytes End If Else Return Nothing End If End Function
Protected Sub fvImages_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles fvImages.ItemInserting Dim fu As New FileUpload fu = CType(fvImages.FindControl("fuImage"), FileUpload) e.Values("ImageId") = Guid.NewGuid e.Values("ImageBinary") = ImageData(fu) e.Values("ImageMime") = ImageMime(fu) End Sub
When the record is inserted into the table, and I call it back I'm getting a "Unknown Image Format" error.
I thought it might be because the width of the image I'm uploading being less than what I set to resize to, so I added a condition to bypass resize.
After some rough debugging, the issue seems to lie with in this declaration...
Dim image As System.Drawing.Image = image.FromStream(fu.PostedFile.InputStream)
As always, help is appreciated.
Thanks!
Protected Sub fvImages_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles fvImages.ItemInsertingDim fu As New FileUploadfu = CType(fvImages.FindControl("fuImage"), FileUpload)e.Values("ImageId") = Guid.NewGuide.Values("ImageBinary") = ImageData(fu)e.Values("ImageMime") = ImageMime(fu)End SubWednesday, March 17, 2010 10:01 AM
Answers
All replies
-
-
User1418935505 posted
Not too much different from what I'm currently using for retrieval.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim ImageId As Guid = New Guid(Request.QueryString("ImageId")) 'Connect to the database and bring back the image contents & MIME type for the specified picture Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("csCHToyota").ConnectionString) Const SQL As String = "SELECT [ImageMime], [ImageBinary] FROM [ImageIndex] WHERE [ImageId] = @ImageId" Dim myCommand As New SqlCommand(SQL, myConnection) myCommand.Parameters.AddWithValue("@ImageId", ImageId) myConnection.Open() Dim myReader As SqlDataReader = myCommand.ExecuteReader If myReader.Read Then Response.ContentType = myReader("ImageMime").ToString() Response.AddHeader("content-disposition", "attachment;filename=" & "111.jpg") Response.BinaryWrite(myReader("ImageBinary")) End If myReader.Close() myConnection.Close() End Using End Sub
I really need to know why stream turns to crap during with what I've currently got during resize and insert.
Thursday, March 18, 2010 10:11 AM -
User1418935505 posted
Sorry!
Second URL solved my issue!
Many thanks.
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) '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. If image.Width > thumbWidth Then 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.Jpeg) Dim imageBytes As Byte() = ConvertImagetoByteArray(bmp, ImageFormat.Jpeg) 'New Byte(fu.PostedFile.InputStream.Length) {} ms.Read(imageBytes, 0, imageBytes.Length - 1) Return imageBytes ms.Close() ms.Dispose() Else Dim imageBytes As Byte() = ConvertImagetoByteArray(image, ImageFormat.Jpeg) 'New Byte(fu.PostedFile.InputStream.Length) {} fu.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length - 1) Return imageBytes End If Else Return Nothing End If End Function
The key was converting to ByteArray :)
Thursday, March 18, 2010 10:48 AM