PDA

View Full Version : ASP.NET proportional thumbnails from database images


david7777
03-12-2003, 01:14 PM
I could find out how to make thumbnails from files on disk, but from a database was a diffirent story... So i had to do it myself.

This code will take an image from a database called "mainBase". The table used is "big_images".
The binary content field is "BinaryContent".

If you want to change the database details, all you must do is edit the connection settings and sql statement...

If you name the page "thumbnail.aspx", you must enter the url:
"thumbnail.aspx?FileID=123"

Where 123 is the ID of the image in the database.

This page will return a jpg or gif image, so you canuse it in another page's <img src="thumbnail.aspx?FileID=123"> tag.

You can set a max height and width for the thumbnail where it says:

thumbSize=NewthumbSize(g.width,g.height,150,100)

(Change 150 for max width, 100 for max height)


Hopefully someone will use this:


<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">

Function NewthumbSize(currentwidth, currentheight, maxWidth, maxHeight)

' Calculate the Size of the New image

dim tempMultiplier as Double = 1.0
dim newWidth as Double, newHeight as Double

newWidth = currentwidth
newHeight = currentheight

'do while not (newHeight <= maxHeight) or not (newWidth <= maxWidth)
if newHeight > newWidth then ' portrait
newWidth = newWidth * (maxHeight / newHeight)
newHeight = maxHeight

if newWidth > maxWidth then
newHeight = newHeight * (maxWidth / newWidth)
newWidth = maxWidth
end if
Else ' landscape or square
newHeight = newHeight * (maxWidth / newWidth)
newWidth = maxWidth

if newHeight > maxHeight then
newWidth = newWidth * (maxHeight / newHeight)
newHeight = maxHeight
end if
end if
'loop

dim NewSize as New Size(CInt(newWidth), CInt(newHeight))


return NewSize
End Function


Sub sendFile()
Dim nFileID,dbconn,sql,dbcomm,dbread,thisFormat
dim byteArray as byte()
dim stream as MemoryStream
dim g as System.Drawing.Image
dim thumbSize as New size
dim imgOutput as Bitmap

' try
nFileID = Request("FileID")

dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("mainBase.mdb"))
dbconn.Open()
SQL = "SELECT FileName, ContentType, BinaryContent FROM big_images WHERE FileID = " & Request.QueryString("FileID")
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()

' Get the dataset ready for reading
dbread.read()
' Get the binary data from the dataset
byteArray = dbread("BinaryContent")
' Create a stream to read the binary content
stream = new MemoryStream(byteArray)
' Creat the bitmap image from the stream
g = new Bitmap(stream)
' Get the format of the object
thisFormat = g.rawformat
' Set the new dimensions of the thumbnail
thumbSize=NewthumbSize(g.width,g.height,150,100)
' Create the newly dimensioned thumbnail
imgOutput = New Bitmap(g, thumbSize.width, thumbSize.height)

' Set the content type
if thisformat.equals(system.drawing.imaging.imageformat.Gif) then
response.contenttype="image/gif"
else
response.contenttype="image/jpeg"
end if

' send the resized image to the viewer
imgOutput.save(response.outputstream, thisformat)

' tidy up
g.dispose()
imgOutput.dispose()
dbread.Close()
dbconn.Close()
stream.Close()
'catch e as Exception ' If error occurs, show no-image
' call sendError()
'end try
end sub

Sub sendError()

' if no height, width, src then output "error"
dim imgOutput as New bitmap(120, 120, pixelformat.format24bpprgb)
dim g as graphics = graphics.fromimage(imgOutput) ' create a New graphic object from the above bmp
g.clear(color.white) ' blank the image

' Draw a border
g.DrawRectangle(new pen(new SolidBrush(color.red), 1),0,0,119,119)
g.DrawRectangle(new pen(new SolidBrush(color.red), 1),10,10,100,100)

' Wite string
g.drawString("No", New font("verdana",14,fontstyle.bold),new SolidBrush(color.red), New pointF(44,37))
g.drawString("Picture", New font("verdana",14,fontstyle.bold),new SolidBrush(color.red), New pointF(19,57))

' Set the contenttype
response.contenttype="image/gif"

' send the resized image to the viewer
imgOutput.save(response.outputstream, imageformat.gif) ' output to the user

' tidy up
g.dispose()
imgOutput.dispose()
response.end
end sub

Function BitmapToBase64(ByVal image As System.Drawing.Bitmap) As String
Dim base64 As String
Dim memory As New System.IO.MemoryStream()
image.Save(memory, Imaging.ImageFormat.Bmp)
base64 = System.Convert.ToBase64String(memory.ToArray)
memory.Close()
memory = Nothing
Return base64
End Function

Function BitmapFromBase64(ByVal base64 As String) As System.Drawing.Bitmap
Dim oBitmap As System.Drawing.Bitmap
Dim memory As New System.IO.MemoryStream(Convert.FromBase64String(base64))
oBitmap = New System.Drawing.Bitmap(memory)
memory.Close()
memory = Nothing
Return oBitmap
End Function

</script>
<html>
<head>
</head>
<body>
<%
' make sure Nothing has gone to the client
response.clear
call sendFile()
response.end
%>
</body>
</html>


Please let me know of any problems or performance enhancements.

Enjoy! :cool:

david7777
03-12-2003, 01:29 PM
Sorry - I forgot to uncomment the try/catch block So you better do it to prevent nasty messages coming up...
I also forgot to take out the

'do while not (newHeight <= maxHeight) or not (newWidth <= maxWidth)

'loop

It is commented out but makes it messy - just take that out too...

Mercury
03-29-2005, 02:06 PM
Keeeej, thats cool, I was looking for this myself for a looong time, helped me great ;)

Have Fun

Mercury