473,569 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary images - thumbnails

Hi all

I have a SQL database where images are stored in a varBinary field. I can
display those images within a image control on a page by using the following:

ImageUrl='<%# Eval("PictureID ", "~/ShowPicture.asp x?PictureID={0} ") %>'

The image is generated from an aspx page called “showPicture. aspx” using the
following code behind:

Imports System.Data.Sql Client

Partial Class ShowPicture
Inherits System.Web.UI.P age

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s)
Handles Me.Load
Dim PictureID As Integer = Convert.ToInt32 (Request.QueryS tring("PictureI D"))

'Connect to the database and bring back the image contents & MIME type for
the specified picture
Using myConnection As New
SqlConnection(C onfigurationMan ager.Connection Strings("ImageG alleryConnectio nString").Conne ctionString)

Const SQL As String = "SELECT [MIMEType], [ImageData] FROM [Pictures] WHERE
[PictureID] = @PictureID"
Dim myCommand As New SqlCommand(SQL, myConnection)
myCommand.Param eters.AddWithVa lue("@PictureID ", PictureID)

myConnection.Op en()

Dim myReader As SqlDataReader = myCommand.Execu teReader
If myReader.Read Then
Response.Conten tType = myReader("MIMET ype").ToString( )
Response.Binary Write(myReader( "ImageData" ))
End If
myReader.Close( )
myConnection.Cl ose()
End Using
End Sub
End Class

The problem I have is that the image control needs to be a thumbnail of the
original image. When I use the above technique the images are not
proprotionately resized to fit the image control width and height – rather
they are stretched to fit.
Can anyone give me some guidance on how once the binary data has been
retrieved it can be proportionatley resized to fit the image control?
Essentialy producing ‘thumbnails of the original.

Many thanks

Aug 4 '08 #1
4 1572
On Aug 4, 1:34*pm, James Page <JamesP...@disc ussions.microso ft.com>
wrote:
Hi all

I have a SQL database where images are stored in a varBinary field. I can
display those images within a image control on a page by using the following:

ImageUrl='<%# Eval("PictureID ", "~/ShowPicture.asp x?PictureID={0} ") %>'

The image is generated from an aspx page called showPicture.as px using the
following code behind:

Imports System.Data.Sql Client

Partial Class ShowPicture
Inherits System.Web.UI.P age

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s)
Handles Me.Load
Dim PictureID As Integer = Convert.ToInt32 (Request.QueryS tring("PictureI D"))

'Connect to the database and bring back the image contents & MIME type for
the specified picture
Using myConnection As New
SqlConnection(C onfigurationMan ager.Connection Strings("ImageG alleryConnectio *nString").Conn ectionString)

Const SQL As String = "SELECT [MIMEType], [ImageData] FROM [Pictures] WHERE
[PictureID] = @PictureID"
Dim myCommand As New SqlCommand(SQL, myConnection)
myCommand.Param eters.AddWithVa lue("@PictureID ", PictureID)

myConnection.Op en()

Dim myReader As SqlDataReader = myCommand.Execu teReader
If myReader.Read Then
Response.Conten tType = myReader("MIMET ype").ToString( )
Response.Binary Write(myReader( "ImageData" ))
End If
myReader.Close( )
myConnection.Cl ose()
End Using
End Sub
End Class

The problem I have is that the image control needs to be a thumbnail of the
original image. When I use the above technique the images are not
proprotionately resized to fit the image control width and height rather
they are stretched to fit.
Can anyone give me some guidance on how once the binary data has been
retrieved it can be proportionatley resized to fit the image control?
Essentialy producing thumbnails of the original.

Many thanks
I am sure you will find many examples in google. Here's the one, which
could help you. Hope it works :-)
Response.Conten tType = myReader("MIMET ype").ToString( )
Dim content As Byte() = CByte(myReader( "ImageData" ))

lnWidth = 100
lnHeight = 100

Dim s As New MemoryStream(co ntent)
Dim image As New Bitmap(s)

Dim bmpOut As System.Drawing. Bitmap

Try
Dim loBMP As New Bitmap(s)
Dim loFormat As ImageFormat = loBMP.RawFormat
Dim lnRatio As Decimal
Dim lnNewWidth As Integer = 0
Dim lnNewHeight As Integer = 0

If loBMP.Width loBMP.Height Then
lnRatio = CDec(lnWidth) / loBMP.Width
lnNewWidth = lnWidth
Dim lnTemp As Decimal = loBMP.Height * lnRatio
lnNewHeight = CInt(lnTemp)
Else
lnRatio = CDec(lnHeight) / loBMP.Height
lnNewHeight = lnHeight
Dim lnTemp As Decimal = loBMP.Width * lnRatio
lnNewWidth = CInt(lnTemp)
End If

bmpOut = New Bitmap(lnNewWid th, lnNewHeight)
Dim g As Graphics = Graphics.FromIm age(bmpOut)
g.Interpolation Mode =
System.Drawing. Drawing2D.Inter polationMode.Hi ghQualityBicubi c
g.FillRectangle (Brushes.White, 0, 0, lnNewWidth, lnNewHeight)
g.DrawImage(loB MP, 0, 0, lnNewWidth, lnNewHeight)

loBMP.Dispose()
Catch
Return
End Try

stream.Close()
stream.Dispose( )

Try
bmp.Save(Respon se.OutputStream ,
System.Drawing. Imaging.ImageFo rmat.Jpeg)
Catch
Finally
bmp.Dispose()
End Try
Aug 4 '08 #2
Thanks Alexey

But having problems:
Here is the revised code:

Imports System.Data.Sql Client
Imports System.IO
Imports System.Drawing
Imports System.Drawing. Imaging

Partial Class showPicture
Inherits System.Web.UI.P age

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Load
Dim ID As Integer = Convert.ToInt32 (Request.QueryS tring("PictureI D"))

'Connect to the database and bring back the image contents & MIME
type for the specified picture
Using myConnection As New
SqlConnection(C onfigurationMan ager.Connection Strings("pictur esConnectionStr ing").Connectio nString)

Const SQL As String = "SELECT [ID], [MIMEtype], [Image] FROM
[Pictures] WHERE [ID] = @PictureID"
Dim myCommand As New SqlCommand(SQL, myConnection)
myCommand.Param eters.AddWithVa lue("@PictureID ", ID)

myConnection.Op en()
Dim myReader As SqlDataReader = myCommand.Execu teReader

If myReader.Read Then
Response.Conten tType = myReader("MIMEt ype").ToString( )
'Response.Binar yWrite(myReader ("Image"))
Dim content As Byte() = CByte(myReader( "image"))
End If

Dim lnWidth As Integer = 100
Dim lnHeight As Integer = 100

Dim s As New MemoryStream(Co ntent)
Dim image As New Bitmap(s)

Dim bmpOut As System.Drawing. Bitmap

Try
Dim loBMP As New Bitmap(s)
Dim loFormat As ImageFormat = loBMP.RawFormat
Dim lnRatio As Decimal
Dim lnNewWidth As Integer = 0
Dim lnNewHeight As Integer = 0

If loBMP.Width loBMP.Height Then
lnRatio = CDec(lnWidth) / loBMP.Width
lnNewWidth = lnWidth
Dim lnTemp As Decimal = loBMP.Height * lnRatio
lnNewHeight = CInt(lnTemp)
Else
lnRatio = CDec(lnHeight) / loBMP.Height
lnNewHeight = lnHeight
Dim lnTemp As Decimal = loBMP.Width * lnRatio
lnNewWidth = CInt(lnTemp)
End If

bmpOut = New Bitmap(lnNewWid th, lnNewHeight)
Dim g As Graphics = Graphics.FromIm age(bmpOut)
g.Interpolation Mode =
System.Drawing. Drawing2D.Inter polationMode.Hi ghQualityBicubi c()
g.FillRectangle (Brushes.White, 0, 0, lnNewWidth, lnNewHeight)
g.DrawImage(loB MP, 0, 0, lnNewWidth, lnNewHeight)

loBMP.Dispose()
Catch
Return
End Try

stream.Close()
stream.Dispose( )

Try
bmp.Save(Respon se.OutputStream ,
System.Drawing. Imaging.ImageFo rmat.Jpeg)
Catch
Finally
bmp.Dispose()
End Try
End Using
End Sub
End Class
However line 25: Dim content As Byte() = CByte(myReader( "image"))
shows an error ("Value of type byte cannot be converted to 1 dimentional
array of byte)

And I'm having problems with:
line 31: Dim s As New MemoryStream(Co ntent)
shows an error (Content is a type and cannot be used as an expression)

Line 57: g.Interpolation Mode =
System.Drawing. Drawing2D.Inter polationMode.Hi ghQualityBicubi c()
Shows an error (Expression is not an array or method and cannot have an
arguement list)

Lines 66 & 67:
stream.Close()
stream.Dispose( )

Shows an error: (reference to a non shared member requires an object
reference)

Finaly lines 70 & 73 reference a 'bmp' how do I declare that - dim bmp as
new bitmap?

I'm keen to sort this out as I have tried many ways to get a proportional
thumbnail without success!!

Thanks


Aug 5 '08 #3
Hi Alexey

Just got this working: Heres the code

Imports System.Data.Sql Client
Imports System.IO
Imports System.Drawing
Imports System.Drawing. Imaging

Partial Class showPicture
Inherits System.Web.UI.P age

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Load
Dim ID As Integer = Convert.ToInt32 (Request.QueryS tring("PictureI D"))

'Connect to the database and bring back the image contents & MIME
type for the specified picture
Using myConnection As New
SqlConnection(C onfigurationMan ager.Connection Strings("pictur esConnectionStr ing").Connectio nString)

Const SQL As String = "SELECT [ID], [MIMEtype], [Image] FROM
[Pictures] WHERE [ID] = @PictureID"
Dim myCommand As New SqlCommand(SQL, myConnection)
myCommand.Param eters.AddWithVa lue("@PictureID ", ID)

myConnection.Op en()
Dim myReader As SqlDataReader = myCommand.Execu teReader

If myReader.Read Then
Response.Conten tType = myReader("MIMEt ype").ToString( )
End If
Dim content As Byte() = (myReader("Imag e"))

Dim lnWidth As Integer = 200
Dim lnHeight As Integer = 200

Dim s As New MemoryStream(co ntent)
Dim image As New Bitmap(s)

Dim bmpOut As System.Drawing. Bitmap

Try
Dim loBMP As New Bitmap(s)
Dim loFormat As ImageFormat = loBMP.RawFormat
Dim lnRatio As Decimal
Dim lnNewWidth As Integer = 0
Dim lnNewHeight As Integer = 0

If loBMP.Width loBMP.Height Then
lnRatio = CDec(lnWidth) / loBMP.Width
lnNewWidth = lnWidth
Dim lnTemp As Decimal = loBMP.Height * lnRatio
lnNewHeight = CInt(lnTemp)
Else
lnRatio = CDec(lnHeight) / loBMP.Height
lnNewHeight = lnHeight
Dim lnTemp As Decimal = loBMP.Width * lnRatio
lnNewWidth = CInt(lnTemp)
End If

bmpOut = New Bitmap(lnNewWid th, lnNewHeight)
Dim g As Graphics = Graphics.FromIm age(bmpOut)
g.Interpolation Mode =
System.Drawing. Drawing2D.Inter polationMode.Hi ghQualityBicubi c
g.FillRectangle (Brushes.White, 0, 0, lnNewWidth, lnNewHeight)
g.DrawImage(loB MP, 0, 0, lnNewWidth, lnNewHeight)

loBMP.Dispose()
Catch
Return
End Try

myConnection.Cl ose()
myReader.Dispos e()

Try
bmpOut.Save(Res ponse.OutputStr eam,
System.Drawing. Imaging.ImageFo rmat.Jpeg)
Catch
Finally
bmpOut.Dispose( )
End Try
End Using
End Sub
End Class
All I need to do now is position the resultant thumbnail in a div correctly
and were done - hurrah!!

Thanks for your help Alexey
Aug 5 '08 #4
On Aug 5, 5:26*pm, James Page <JamesP...@disc ussions.microso ft.com>
wrote:
Hi Alexey

Just got this working: Heres the code

Imports System.Data.Sql Client
Imports System.IO
Imports System.Drawing
Imports System.Drawing. Imaging

Partial Class showPicture
* * Inherits System.Web.UI.P age

* * Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Load
* * * * Dim ID As Integer = Convert.ToInt32 (Request.QueryS tring("PictureI D"))

* * * * 'Connect to the database and bring back the image contents & MIME
type for the specified picture
* * * * Using myConnection As New
SqlConnection(C onfigurationMan ager.Connection Strings("pictur esConnectionStr *ing").Connecti onString)

* * * * * * Const SQL As String = "SELECT [ID], [MIMEtype],[Image] FROM
[Pictures] WHERE [ID] = @PictureID"
* * * * * * Dim myCommand As New SqlCommand(SQL, myConnection)
* * * * * * myCommand.Param eters.AddWithVa lue("@PictureID ", ID)

* * * * * * myConnection.Op en()
* * * * * * Dim myReader As SqlDataReader = myCommand.Execu teReader

* * * * * * If myReader.Read Then
* * * * * * * * Response.Conten tType = myReader("MIMEt ype").ToString( )
* * * * * * End If
* * * * * * Dim content As Byte() = (myReader("Imag e"))

* * * * * * Dim lnWidth As Integer = 200
* * * * * * Dim lnHeight As Integer = 200

* * * * * * Dim s As New MemoryStream(co ntent)
* * * * * * Dim image As New Bitmap(s)

* * * * * * Dim bmpOut As System.Drawing. Bitmap

* * * * * * Try
* * * * * * * * Dim loBMP As New Bitmap(s)
* * * * * * * * Dim loFormat As ImageFormat = loBMP.RawFormat
* * * * * * * * Dim lnRatio As Decimal
* * * * * * * * Dim lnNewWidth As Integer = 0
* * * * * * * * Dim lnNewHeight As Integer = 0

* * * * * * * * If loBMP.Width loBMP.Height Then
* * * * * * * * * * lnRatio = CDec(lnWidth) / loBMP..Width
* * * * * * * * * * lnNewWidth = lnWidth
* * * * * * * * * * Dim lnTemp As Decimal = loBMP.Height * lnRatio
* * * * * * * * * * lnNewHeight = CInt(lnTemp)
* * * * * * * * Else
* * * * * * * * * * lnRatio = CDec(lnHeight) / loBMP.Height
* * * * * * * * * * lnNewHeight = lnHeight
* * * * * * * * * * Dim lnTemp As Decimal = loBMP.Width * lnRatio
* * * * * * * * * * lnNewWidth = CInt(lnTemp)
* * * * * * * * End If

* * * * * * * * bmpOut = New Bitmap(lnNewWid th, lnNewHeight)
* * * * * * * * Dim g As Graphics = Graphics.FromIm age(bmpOut)
* * * * * * * * g.Interpolation Mode =
System.Drawing. Drawing2D.Inter polationMode.Hi ghQualityBicubi c
* * * * * * * * g.FillRectangle (Brushes.White, 0, 0, lnNewWidth, lnNewHeight)
* * * * * * * * g.DrawImage(loB MP, 0, 0, lnNewWidth, lnNewHeight)

* * * * * * * * loBMP.Dispose()
* * * * * * Catch
* * * * * * * * Return
* * * * * * End Try

* * * * * * myConnection.Cl ose()
* * * * * * myReader.Dispos e()

* * * * * * Try
* * * * * * * * bmpOut.Save(Res ponse.OutputStr eam,
System.Drawing. Imaging.ImageFo rmat.Jpeg)
* * * * * * Catch
* * * * * * Finally
* * * * * * * * bmpOut.Dispose( )
* * * * * * End Try
* * * * End Using
* * End Sub
End Class

All I need to do now is position the resultant thumbnail in a div correctly
and were done - hurrah!!

Thanks for your help Alexey
Great, glad it works :-)
Aug 5 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2860
by: wtgee | last post by:
I posted this elsewhere but got no answer so I thought I would see if anyone here is smarter. :) I am creating a image management system and have a few questions, if anyone could help 1) I read somewhere that storing images in a mysql database as a BLOB reduces speed and efficiency. Is this true? I don't expect to be serving many...
0
1876
by: Michael Rostkowski | last post by:
I posted this in alt.php, but as a reply, so now I'm posting it here for people who might have missed it but still could find it useful. Thanks to Andy Hassall for giving me good information. I compared the smooth image resampling used in PHP to create good quality thumbnails of large images, used commonly in image galleries. i compared...
11
2487
by: Michel | last post by:
Here is a litle script that preload images and show a thumbnail. Once you get on the picture you see the real size It worsk fine with normal picture but when the url has some parameters like in the sample. The image is loaded again every time I go over the thumbnail. Is there a solution for havint just one load? function AddImage(url)...
11
1970
by: Ian Davies | last post by:
Hello Im having problems displaying my images as thumbnails in a table. My code for producing the new width and height from the original image is as follows ************************************************** if ($ImagePath) { //$Image = WEB_ROOT . 'images/PupilTester/' . $ImagePath; $Image = 'images/PupilTester/' . $ImagePath; } else {...
2
1945
by: Celine | last post by:
I am building a website using C#, asp.net and SqlServer. I need to store a lot of images in my database. I need to store thumbnails but also larger pictures(when user clicks thumbnails a larger picture is displayed). For doing that I was told to store only the URL's. My problem now is how to display these images. I know that the tag <img> is...
2
2037
by: S.Creek | last post by:
Hi, I need to take few files (images in my case) and create one file out of them, this file should be accessed so i can grab a single image from it if necessary, i thought of taking all the images and put them in one binary file, but couldn't figure out a way to do that, anyone has an idea? thanks
16
3562
by: Stan The Man | last post by:
I'm a CSS novice trying unsuccessfully to make three thumbnail images display horizontally instead of vertically. I suspect I'm missing something really stupid but I'll take the flak if someone could kindly point me in the right direction (using words that the vicar's wife would understand). The vertical thumbnails can be seen at the bottom of...
3
3569
by: =?Utf-8?B?UiBSZXllcw==?= | last post by:
Hi! This discussion may help other programmers get a better idea of how to save uploaded images through a website. Why? Well currently, I save 3 versions of every uploaded image on my own little website: 1. Small: DOWNsize of original image to be used as a thumbnail. 2. Medium: DOWNsize of original image to be used as user...
1
2671
by: onyris | last post by:
Hi guys , I have a problem trying to unload some images from a movie clip , not sure what i'm doing wrong , if anyone can help me please.. What i have is a movie clip called " thumbnails" on the stage which loads some images from an array . the movieclip "thumbnails" , holds at the begining the first 4 images , then i have a button called...
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.