B
Brad
I have code which takes an image, uploaded from a web page, and saves it to
a database. Now I want to always resize an uploaded image before it is
saved to the database. My code to resize is below and of course it's not
working properly because my web page which displays the resulting image from
the database is not showing the image. I **think** the problem is in my
converting the bitmap back to a byte array. If I change my code back to
saving the image as originally uploaded the image displays fine.
Brad
'===============================
' Upload conversion code
Public Sub SaveUploadedImage(ByVal itemId As Integer, ByVal imageFile As
System.Web.HttpPostedFile)
Dim contentSize As Integer
Dim contentType As String = imageFile.ContentType
'=====================================================
Dim origImage As Bitmap =
CType(System.Drawing.Image.FromStream(imageFile.InputStream), Bitmap)
Dim maxHeight As Integer = 340
Dim maxWidth As Integer = 170
Dim origWidth As Integer = origImage.Width
Dim origHeight As Integer = origImage.Height
Dim newWidth As Integer = origWidth
Dim newHeight As Integer = origHeight
If ((origWidth > maxWidth) And (maxWidth > 0)) Or ((origHeight > maxHeight)
And (maxHeight > 0)) Then
Dim factor As Double = 1
If (maxWidth > 0) Then
factor = Math.Min(factor, (maxWidth / origWidth))
End If
If (maxHeight > 0) Then
factor = Math.Min(factor, (maxHeight / origHeight))
End If
newWidth = CInt(origWidth * factor)
newHeight = CInt(origHeight * factor)
End If
Dim newImage As Bitmap = New Bitmap(newWidth, newHeight)
Dim gfx As Graphics = Graphics.FromImage(newImage)
With gfx
.CompositingMode = Drawing2D.CompositingMode.SourceOver
.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
.DrawImage(origImage, 0, 0, newWidth, newHeight)
.Dispose()
End With
'==========================================
' Is problem in this section of code????
'==========================================
Dim objStream As New MemoryStream
If contentType.IndexOf("jpeg") >= 0 Then
newImage.Save(objStream, Imaging.ImageFormat.Jpeg)
Else
newImage.Save(objStream, Imaging.ImageFormat.Gif)
End If
newImage.Dispose()
contentSize = CType(objStream.Length, Integer)
Dim imageData(contentSize) As Byte
objStream.Read(imageData, 0, contentSize)
'==========================================
' set sql sp parameters
With dataObj
Dim parameterContent As SqlParameter = New SqlParameter("@Content",
SqlDbType.Image)
parameterContent.Value = imageData
.Parameters.Add(parameterContent)
Dim parameterContentSize As SqlParameter = New
SqlParameter("@ContentSize", SqlDbType.Int, 4)
parameterContentSize.Value = contentSize
.Parameters.Add(parameterContentSize)
Dim parameterContentType As SqlParameter = New
SqlParameter("@ContentType", SqlDbType.VarChar, 25)
parameterContentType.Value = contentType
.Parameters.Add(parameterContentType)
End With
End Sub
******************************************
FYI - Here's excerpt of code from the web page which displays the image
Dim content As Byte()
Dim byteSize As Integer
Dim contentType As String
Dim dr As SqlDataReader = dataObj.GetImage(itemId)
If dr.Read Then
byteSize = Ctype(dr("contentsize"),integer)
If byteSize > 0 Then
content = CType(dr("content"), Byte())
contentType = dr("contentType").ToString
End If
End If
dr.Close()
If byteSize > 0 Then
Response.ContentType = contentType
Response.OutputStream.Write(content, 0, byteSize)
End If
a database. Now I want to always resize an uploaded image before it is
saved to the database. My code to resize is below and of course it's not
working properly because my web page which displays the resulting image from
the database is not showing the image. I **think** the problem is in my
converting the bitmap back to a byte array. If I change my code back to
saving the image as originally uploaded the image displays fine.
Brad
'===============================
' Upload conversion code
Public Sub SaveUploadedImage(ByVal itemId As Integer, ByVal imageFile As
System.Web.HttpPostedFile)
Dim contentSize As Integer
Dim contentType As String = imageFile.ContentType
'=====================================================
Dim origImage As Bitmap =
CType(System.Drawing.Image.FromStream(imageFile.InputStream), Bitmap)
Dim maxHeight As Integer = 340
Dim maxWidth As Integer = 170
Dim origWidth As Integer = origImage.Width
Dim origHeight As Integer = origImage.Height
Dim newWidth As Integer = origWidth
Dim newHeight As Integer = origHeight
If ((origWidth > maxWidth) And (maxWidth > 0)) Or ((origHeight > maxHeight)
And (maxHeight > 0)) Then
Dim factor As Double = 1
If (maxWidth > 0) Then
factor = Math.Min(factor, (maxWidth / origWidth))
End If
If (maxHeight > 0) Then
factor = Math.Min(factor, (maxHeight / origHeight))
End If
newWidth = CInt(origWidth * factor)
newHeight = CInt(origHeight * factor)
End If
Dim newImage As Bitmap = New Bitmap(newWidth, newHeight)
Dim gfx As Graphics = Graphics.FromImage(newImage)
With gfx
.CompositingMode = Drawing2D.CompositingMode.SourceOver
.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
.DrawImage(origImage, 0, 0, newWidth, newHeight)
.Dispose()
End With
'==========================================
' Is problem in this section of code????
'==========================================
Dim objStream As New MemoryStream
If contentType.IndexOf("jpeg") >= 0 Then
newImage.Save(objStream, Imaging.ImageFormat.Jpeg)
Else
newImage.Save(objStream, Imaging.ImageFormat.Gif)
End If
newImage.Dispose()
contentSize = CType(objStream.Length, Integer)
Dim imageData(contentSize) As Byte
objStream.Read(imageData, 0, contentSize)
'==========================================
' set sql sp parameters
With dataObj
Dim parameterContent As SqlParameter = New SqlParameter("@Content",
SqlDbType.Image)
parameterContent.Value = imageData
.Parameters.Add(parameterContent)
Dim parameterContentSize As SqlParameter = New
SqlParameter("@ContentSize", SqlDbType.Int, 4)
parameterContentSize.Value = contentSize
.Parameters.Add(parameterContentSize)
Dim parameterContentType As SqlParameter = New
SqlParameter("@ContentType", SqlDbType.VarChar, 25)
parameterContentType.Value = contentType
.Parameters.Add(parameterContentType)
End With
End Sub
******************************************
FYI - Here's excerpt of code from the web page which displays the image
Dim content As Byte()
Dim byteSize As Integer
Dim contentType As String
Dim dr As SqlDataReader = dataObj.GetImage(itemId)
If dr.Read Then
byteSize = Ctype(dr("contentsize"),integer)
If byteSize > 0 Then
content = CType(dr("content"), Byte())
contentType = dr("contentType").ToString
End If
End If
dr.Close()
If byteSize > 0 Then
Response.ContentType = contentType
Response.OutputStream.Write(content, 0, byteSize)
End If