Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying image from database issue. Need JPEG, comes as BMP

Status
Not open for further replies.

mjjks

Programmer
Jun 22, 2005
138
US
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.

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.


 
try refreshing the page, then trying to save the image again. It's a weird problem with I.E.

If it still doesnt work, you might want to ensure that you have the MIME type set to "image/jpeg" or whatever it is. Ok i see you did that.

Finnaly, when you write to the output stream, SAVE the image (as jpg) to the stream, meaning it will execute jpeg comrpession before writting, or else its just copying the bits to the output stream.
 
Sorry to ask an obvious question, but was the data saved to the database as a .bmp instead of a .jpg? Also, what file extension is used when requesting this dynamic resource?

If the data really is in .bmp format, you can load its data up into a .NET Bitmap object and change it either on the fly before you serve the image (which would probably be a somewhat expensive operation) or you can do the same, but save the resulting bytes back to the database so you're serving up .jpg data with each request (better performing, but you'd have to sit down and run a conversion on everything in your database).

MCP, MCTS - .NET Framework 2.0 Web Applications
 
Thanks for the replies - got sidetracked with another project.

BoulderBum: Yes - it goes as JPEG into database as content type is specified as JPEG and there's validation that allows JPEGs only.

NeilTrain: I will try saving as JPEG, but part I don't get is - I already specify content type before image is streamed out, and it stored as JPEG in a database.

There's another app that uses the same codebase and it allows to save image as JPEG or BMP (from IE).

Difference is - data type in SQL is defined as varbinary(Max), in app that works. One that I'm troubleshooting - uses "Image" data type. I tried converting "image" to "varbinary" in stored procedure but that didn't work.

I'll try a few things and will update on result. Thanks

 
if you load from the database into a System.Drawing.Image type, even though it was loaded from a jpeg format, it will save in the bitmap format by default.

So saving as jpeg to the output strea would solve your problem, but add unessesary overhead, essentially converting it twice. You should be able to take the bits as is and write them to the output stream, never instantiating an image object. Not only will this be faster, but save on bandwith since jpegs are considerably smaller than bitmaps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top