Did I do this right? - Resized image from DB.

N

Neo Geshel

Greetings.

I am making an admin interface, which allows me to upload photos to an
access DB. The admin interface also needs to display the uploaded
photos, but only needs to show them at a maximum resolution of 100px per
side. All photos will be JPEG. Below is a showimage.aspx file that
allows me to display the images from the database in the admin
interface. It doesn't seem to work right, so I believe I did something
wrong. It is supposed to take two variables (the table name and the
desired key) and extract the image, resize it proportionally, and then
display it. Could someone please look over it and correct any mistakes?

<%@ 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" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat=server>
Public Sub Page_Load(sender As Object, e As EventArgs)
Dim strTable as String = Request.QueryString("table")
Dim strID as String = Request.QueryString("id")
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("SELECT [Image] FROM " & strTable & "
WHERE [ID] = " & strID, myConn)
Try
myConn.Open()
Dim myDataReader as OleDbDataReader
myDataReader = myCmd.ExecuteReader(CommandBehavior.CloseConnection)
Do While (myDataReader.Read())
Dim imgStream As Stream = myDataReader.Item("Image")
Dim imgbin() As Byte
imgbin = createThumbnail(imgStream, 100, 100)
Response.ContentType="image/jpeg"
Response.BinaryWrite(imgbin)
Loop
myConnection.Close()
End Try
End Sub
Private Function createThumbnail(ByVal ImageStream As Stream, ByVal
tWidth As Double, ByVal tHeight As Double) As Byte()
Dim g As System.Drawing.Image
=System.Drawing.Image.FromStream(ImageStream)
Dim thumbSize As New Size()
thumbSize =NewthumbSize(g.Width, g.Height, tWidth, tHeight)
Dim imgOutput As New Bitmap(g, thumbSize.Width, thumbSize.Height)
Dim imgStream As New MemoryStream()
Dim thisFormat = g.RawFormat
imgOutput.Save(imgStream, thisFormat)
Dim imgbin(imgStream.Length) As Byte
imgStream.Position = 0
Dim n As Int32 = imgStream.Read(imgbin, 0, imgbin.Length)
g.Dispose()
imgOutput.Dispose()
Return imgbin
End Function
Function NewthumbSize(ByVal currentwidth As Double, ByVal currentheight
As Double, ByVal newWidth As Double, ByVal newHeight As Double)
Dim tempMultiplier As Double
If currentheight > currentwidth Then ' portrait
tempMultiplier = newHeight / currentheight
Else
tempMultiplier = newWidth / currentwidth
End If
Dim NewSize As New Size(CInt(currentwidth * tempMultiplier),
CInt(currentheight * tempMultiplier))
Return NewSize
End Function
</script>


Thanks.
....Geshel
--
**********************************************************************
My reply-to is an automatically monitored spam honeypot. Do not use it
unless you want to be blacklisted by SpamCop. Please reply to my first
name at my last name dot org.
**********************************************************************
 
N

Neo Geshel

Cor said:
Neo,

You know this sample from me in this message. It is made with VBNet and IDE,
therefore you have to change it maybe if you are not using that.

http://groups-beta.google.com/group/microsoft.public.dotnet.languages.vb/msg/e06cba49dcca34dc?hl=en

I hope this helps,

Cor

The only portion of the web page you sent to me that made sense was the
"webform 2" section (the second half of the code). And with that
particular section, I got the impression that it only took an image, and
re-sized it to 100x100. I don't want this. If an image is 640x480, I
want to resize it to 100x75, not 100x100.

Remember, the code I posted was contained within the showimage.aspx
file. It was meant to accept two variables and pass on a dynamically
(and proportionally correct) re-sized image (from the access DB) to the
web page that called it.

Thanks, anyways.
....Geshel
--
**********************************************************************
My reply-to is an automatically monitored spam honeypot. Do not use it
unless you want to be blacklisted by SpamCop. Please reply to my first
name at my last name dot org.
**********************************************************************
 
C

Cor Ligthert

Neo,

The only portion of the web page you sent to me that made sense was the
"webform 2" section (the second half of the code). And with that
particular section, I got the impression that it only took an image, and
re-sized it to 100x100. I don't want this. If an image is 640x480, I want
to resize it to 100x75, not 100x100.
Why do you want to make your own thumbnails while there is a complet class
for that?

I saw by the way that you created as well a 100 * 100 image in your class,
however a lot more difficult as in that sample that I gave that is in that
message.

It is a sample to show the complete working, otherwise the name is snippet.

Cor
 
N

Neo Geshel

Cor said:
Neo,

Why do you want to make your own thumbnails while there is a complet class
for that?

The images stored in the database will be shown on two web sites: The
public one and the private, admin site. Only the private admin site
needs to show the images as thumbnails, which is why the resizing is
done inside the showimage.aspx file.

Besides, what you you mean by, "there is a complete class for that"?
I saw by the way that you created as well a 100 * 100 image in your class,
however a lot more difficult as in that sample that I gave that is in that
message.

If you look closer, 100 represents the MAXIMUM size that the image can
have per side. a 640x480 image will still be proportionally resized to
100x75 (and a 480x640 image would be resized to 75x100).
It is a sample to show the complete working, otherwise the name is snippet.

Okay, that made no sense at all. Could you rephrase?

....Geshel
--
**********************************************************************
My reply-to is an automatically monitored spam honeypot. Do not use it
unless you want to be blacklisted by SpamCop. Please reply to my first
name at my last name dot org.
**********************************************************************
 
N

Neo Geshel

Cor said:
Neo,

You know this sample from me in this message. It is made with VBNet and IDE,
therefore you have to change it maybe if you are not using that.

http://groups-beta.google.com/group/microsoft.public.dotnet.languages.vb/msg/e06cba49dcca34dc?hl=en

I hope this helps,

Cor

Cor,

Just to let you know, although the link above didn't give me *all* the
answers I needed, it had enough of the "right stuff" to give me a big
help and point me in the right direction.

BTW, your image resizing in the webform2.aspx doesn't seem resize the
image proportionally (a thumbnail of a 640x480 image will be distorted
as a 100x100 thumbnail). If you want, I can provide you with my version,
which does.

Thanks, Cor.

....Geshel
--
**********************************************************************
My reply-to is an automatically monitored spam honeypot. Do not use it
unless you want to be blacklisted by SpamCop. Please reply to my first
name at my last name dot org.
**********************************************************************
 
C

Cor Ligthert

Neo,

It is only a sample, and every extra that I make in it, can confuse people.
I can make thumbnails shaped. In my opinion is a sample just a lead in the
right direction, as you saw yourself.

However keep in mind that a thumbnail is mostly meant to give an idea of the
original picture. When it is shaped as the original and you have 5 on a page
that can look weird.

Although that I have a program (a kind of foto scroller as is in explorer
but than with a database) where I thought to shape different for portrait
and landscape pictures.

Thanks for your response.

Cor
 

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

Top