Quick foreword: I inherited a web app that uploads images into SQL database for storage and displays them on a web page, at request.
Problem is that when I right-click on loaded image, then "Save As", I have only BMP option and need to have whatever content type is, which is currently "image/jpeg" in the database.
Original developer used "image" datatype instead of "varbinary" for SQL2005. In stored procedure I CAST "image" datatype into "varbinary", but still image comes as BMP.
How do I force image to come out as JPEG?
Any help is appreciated. Below is partial code (VB.NET) that retrieves image.
I also tried "Response.BinaryWrite" with SQLDataReader without luck. Thanks.
Problem is that when I right-click on loaded image, then "Save As", I have only BMP option and need to have whatever content type is, which is currently "image/jpeg" in the database.
Original developer used "image" datatype instead of "varbinary" for SQL2005. In stored procedure I CAST "image" datatype into "varbinary", but still image comes as BMP.
How do I force image to come out as JPEG?
Any help is appreciated. Below is partial code (VB.NET) that retrieves image.
Code:
Dim image As Byte() = Nothing
If image Is Nothing Then
Dim connection As SqlConnection = New SqlConnection(connectionString)
Using (connection)
Dim stream As MemoryStream = New MemoryStream
Try
connection.Open()
Dim command As SqlCommand = New SqlCommand(selectCommand, connection)
Using (command)
command.CommandType = CommandType.StoredProcedure
command.Parameters.Add("@ImageId", SqlDbType.Int)
command.Parameters("@ImageId").Value = selectParameter.ToString
command.Parameters("@ImageId").Direction = ParameterDirection.Input
command.Parameters.Add("@Size", SqlDbType.Int)
command.Parameters("@Size").Value = sizeParameter.ToString
command.Parameters("@Size").Direction = ParameterDirection.Input
Using (Stream)
image = CType(command.ExecuteScalar, Byte())
End Using
connection.Close()
End Using
Finally
CType(Stream, IDisposable).Dispose()
End Try
End Using
End If
' Display image
response.ContentType = STR_ContentType ' image/jpeg
response.OutputStream.Write(image, 0, image.Length)
I also tried "Response.BinaryWrite" with SQLDataReader without luck. Thanks.