GetThumbnailImage Question

J

John

I am trying to convert the regular sized images in a directory to
thumbnails and then display the thumbnails in a datalist. My code
below is displaying the first image and nothing else after it. What
do I have to do to have the thumbnails displayed in my datalist?
Thanks.

Dim strFile As String
Dim pics As ArrayList = New ArrayList

' Get image folder path on server - use "\" string if root
Dim strServerPath As String = Server.MapPath("photos\")

For Each strFile In System.IO.Directory.GetFiles(strServerPath,
"*.jpg")
' Get the regular sized image
Dim FullSizeImg As System.Drawing.Image
FullSizeImg = System.Drawing.Image.FromFile(strFile)

' Create the delegate
Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbort
dummyCallBack = New
System.Drawing.Image.GetThumbnailImageAbort(AddressOf
ThumbnailCallback)

' Create the thumbnail
Dim ThumbNailImg As System.Drawing.Image
ThumbNailImg = FullSizeImg.GetThumbnailImage(160, 120,
dummyCallBack, IntPtr.Zero)
ThumbNailImg.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg)
pics.Add(ThumbNailImg)
Next

DataList1.DataSource = pics
DataList1.DataBind()
 
G

Guest

Hi John

First, in the item template of the datalist you need an image control

Second, the image control needs a url (either src= or imageurl=, depending on whether it's a server control)

Third, you can do the url the easy way or the harder-but-cooler way

Easier way: Don't store your thumbnails in the array, instead write them to disk with temp names and store just the names in the array, then bind the source url to those names and you're off

Harder way: Keep your images in the arraylist, but now you need a url that returns each image from memory. Consider GetImage.aspx, which returns a byte stream the browser will interpret as an image, with something like this in PageLoad

Dim bmp As Image = New Bitmap(Server.MapPath("images\myfile.jpg")
Dim thumb as image = bmp.GetThumbnailImage, etc
Response.ContentType = "image/jpeg
bmp.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg

Now you bind the source url to GetImage.aspx and the thumbnail is fed from memory rather than disk

Finally, in my example GetImage returns the *same* image each time, so if your directory has 30 images you'd get the same one each time...not good. A few ways to go about it, buy maybe the easiest is that you build your arraylist with just the file names, pass the file name to GetImage.aspx in a query string, and let GetImage convert it to a thumbnail and stream it back

hth

Bil

P.S. If you're serious about it, there's a great discussion in Dino Esposito's book, Programming Microsoft ASP.NET

----- John wrote: ----

I am trying to convert the regular sized images in a directory t
thumbnails and then display the thumbnails in a datalist. My cod
below is displaying the first image and nothing else after it. Wha
do I have to do to have the thumbnails displayed in my datalist?
Thanks

Dim strFile As Strin
Dim pics As ArrayList = New ArrayLis

' Get image folder path on server - use "\" string if roo
Dim strServerPath As String = Server.MapPath("photos\"

For Each strFile In System.IO.Directory.GetFiles(strServerPath
"*.jpg"
' Get the regular sized imag
Dim FullSizeImg As System.Drawing.Imag
FullSizeImg = System.Drawing.Image.FromFile(strFile

' Create the delegat
Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbor
dummyCallBack = Ne
System.Drawing.Image.GetThumbnailImageAbort(AddressO
ThumbnailCallback

' Create the thumbnai
Dim ThumbNailImg As System.Drawing.Imag
ThumbNailImg = FullSizeImg.GetThumbnailImage(160, 120
dummyCallBack, IntPtr.Zero
ThumbNailImg.Save(Response.OutputStream
System.Drawing.Imaging.ImageFormat.Jpeg
pics.Add(ThumbNailImg
Nex

DataList1.DataSource = pic
DataList1.DataBind(
 
J

John

Thanks Bill. That was what I needed.

Bill Borg said:
Hi John,

First, in the item template of the datalist you need an image control.

Second, the image control needs a url (either src= or imageurl=, depending on whether it's a server control).

Third, you can do the url the easy way or the harder-but-cooler way.

Easier way: Don't store your thumbnails in the array, instead write them to disk with temp names and store just the names in the array, then bind the source url to those names and you're off.

Harder way: Keep your images in the arraylist, but now you need a url that returns each image from memory. Consider GetImage.aspx, which returns a byte stream the browser will interpret as an image, with something like this in PageLoad:

Dim bmp As Image = New Bitmap(Server.MapPath("images\myfile.jpg"))
Dim thumb as image = bmp.GetThumbnailImage, etc.
Response.ContentType = "image/jpeg"
bmp.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)

Now you bind the source url to GetImage.aspx and the thumbnail is fed from memory rather than disk.

Finally, in my example GetImage returns the *same* image each time, so if your directory has 30 images you'd get the same one each time...not good. A few ways to go about it, buy maybe the easiest is that you build your arraylist with just the file names, pass the file name to GetImage.aspx in a query string, and let GetImage convert it to a thumbnail and stream it back.

hth,

Bill

P.S. If you're serious about it, there's a great discussion in Dino Esposito's book, Programming Microsoft ASP.NET.

----- John wrote: -----

I am trying to convert the regular sized images in a directory to
thumbnails and then display the thumbnails in a datalist. My code
below is displaying the first image and nothing else after it. What
do I have to do to have the thumbnails displayed in my datalist?
Thanks.

Dim strFile As String
Dim pics As ArrayList = New ArrayList

' Get image folder path on server - use "\" string if root
Dim strServerPath As String = Server.MapPath("photos\")

For Each strFile In System.IO.Directory.GetFiles(strServerPath,
"*.jpg")
' Get the regular sized image
Dim FullSizeImg As System.Drawing.Image
FullSizeImg = System.Drawing.Image.FromFile(strFile)

' Create the delegate
Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbort
dummyCallBack = New
System.Drawing.Image.GetThumbnailImageAbort(AddressOf
ThumbnailCallback)

' Create the thumbnail
Dim ThumbNailImg As System.Drawing.Image
ThumbNailImg = FullSizeImg.GetThumbnailImage(160, 120,
dummyCallBack, IntPtr.Zero)
ThumbNailImg.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg)
pics.Add(ThumbNailImg)
Next

DataList1.DataSource = pics
DataList1.DataBind()
 
G

Guest

P.S. In Esposito's book, he skips the dummy callback and just does this (which worked for me too)

Dim bmp As Image = New Bitmap(Server.MapPath("images\myimage.jpg")
Dim newbmp As Image = bmp.GetThumbnailImage(20, 20, *Nothing*, IntPtr.Zero

Enjoy

----- John wrote: ----

Thanks Bill. That was what I needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top