N
Neo Geshel
I have examined about 80+ different upload scripts on the 'net, both in
VB and C#, and none seem to do what I need them to do. Perhaps someone
here can point me somewhere that Google hasn't reached yet (I have gone
all the way to page 50 on Google's results!!). Here are my requirements:
• I have a DataGrid. Everything will be done from here. Everything. No
exceptions. Everything will also be done in VB, without any code-behind
to confuse a beginner like me. Besides, optimization is not required,
this admin site will be used about 1-2 times a month. Scalability and
performance is NOT an issue.
• Uploading of new images will be done from the "add" line of the
datagrid, the datagrid footer in my case, and will be handled by a sub
with an e.commandname="Insert"
• The images are to be uploaded, and then several checks must be done
before they are inserted into the db.
• First Check: Does the returned stream actually contain anything? If
not, an "add" to the database cannot occur (there must be an image).
• Second Check: Is the returned stream of the filetype "image/jpeg"? If
not, the "add" to the database cannot occur (the image must be a jpeg).
• Third Check: If any dimension of the image exceeds 640 pixels, it
needs to be proportionally re-sized to a maximum size of 640 per side.
That is, an 800x600 image is to be crunched to 640x480; a 400x800 image
is to be crunched to 320x640.
• Fourth Check: If the file size of the image exceeds 150kb, it is to be
resampled downwards until the file size meets that value.
• The images are to be inserted into the database along with a comment
field populated by a <textarea /> in the datagrid.
• Since the datagrids of the Admin site is the only place that needs
thumbnails (the main site doesn't), it needs a showimage.aspx (that can
accept a table name and a unique ID from the Get string) that re-sizes
the image requested (again, proportionally) in a dynamic manner before
the images make it to the browser. All thumbnails are to have a maximum
side length of 100 pixels. This re-sizing must be done inside the
showimage.aspx
Below is the SUB that I have managed to construct so far, but it seems
to input corrupted image data into the DB. Images can't be extracted,
even with a highly simplified showimage.aspx:
Sub dgCarShowGalleryPhotos_Insert(sender As Object, e As
DataGridCommandEventArgs)
If e.CommandName = "Insert" Then
Dim imgContent as Object = e.Item.Cells(2).Controls(1)
Dim strComment as String = CType(e.Item.Cells(3).Controls(1),
Textbox).Text.Replace("'","’")
Dim imgStream As Stream = imgContent.PostedFile.InputStream()
Dim imgType as String = imgContent.PostedFile.ContentType
Dim imgLen As Int64 = imgContent.PostedFile.ContentLength
Dim imgDimension as System.Drawing.Image =
System.Drawing.Image.FromStream(imgContent.PostedFile.InputStream)
Dim imgHeight as String = imgDimension.PhysicalDimension.Height
Dim imgWidth as String = imgDimension.PhysicalDimension.Width
Dim imgbin() as Byte
If Not imgStream Is Nothing Then
If imgContent.PostedFile.ContentLength > 0 And
imgContent.PostedFile.ContentType = "image/jpeg" Then
If imgWidth > 640 Or imgHeight > 640 Then
imgbin = createThumbnail(imgStream, 640, 640)
Else
Dim imgBinaryData(imgLen) as Byte
Dim intStatus as Integer = imgStream.Read(imgBinaryData, 0, imgLen)
imgBin = imgBinaryData
End If
CType(e.Item.FindControl("add_Comment"), TextBox).Text.Replace("'","’")
Dim strSQL As String = "INSERT INTO [tblCarShowGalleryPhotos]
([CarShowGalleryYearID], [Image], [Comment]) VALUES (" &
Session("IDHolder") & ", @Image, @Comment)"
Dim objConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
objConn.Open()
Dim myCommand as OleDbCommand = new OleDbCommand(strSQL, objConn)
myCommand.CommandType = CommandType.Text
Dim parameterImage as OleDbParameter = new OleDbParameter("@Image",
OleDbType.LongVarBinary)
parameterImage.Value = imgBin
myCommand.Parameters.Add(parameterImage)
Dim parameterComment as OleDbParameter = new OleDbParameter("@Comment",
OleDbType.LongVarWChar)
parameterComment.Value = strComment
myCommand.Parameters.Add(parameterComment)
myCommand.ExecuteNonQuery()
objConn.Close()
dgCarShowGalleryPhotos.EditItemIndex = -1
BindData()
End If
End If
End If
End Sub
And here is the showimage.aspx file, which is referenced everywhere a
thumbnail is required by the datagrid:
<%@ 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)
myConn.Open()
Dim myDataReader as OleDbDataReader =
myCmd.ExecuteReader(CommandBehavior.CloseConnection)
myDataReader.Read()
Dim imgStream(myDataReader.Item("Image")) As System.IO.MemoryStream
Dim imgStream As System.IO.MemoryStream
imgStream = myDataReader.Item("Image")
Dim imgbin as Array = createThumbnail(imgStream, 100, 100)
Response.ContentType="image/jpeg"
Response.BinaryWrite(imgbin)
myConn.Close()
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>
It also throws a number of very bad errors, particulary of the
information being of an incompatible type. "a 1-dimensional array of
byte cannot be converted to type of byte" and so forth.
Once again, I would like to say that there is NOTHING on the internet AT
ALL that is anywhere similar to what I am trying to create. If any of
you good ppl could help me find something that could fit the bill, or
correct any mistakes that I have made, I would be greatly indebted.
TIA
...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.
**********************************************************************
VB and C#, and none seem to do what I need them to do. Perhaps someone
here can point me somewhere that Google hasn't reached yet (I have gone
all the way to page 50 on Google's results!!). Here are my requirements:
• I have a DataGrid. Everything will be done from here. Everything. No
exceptions. Everything will also be done in VB, without any code-behind
to confuse a beginner like me. Besides, optimization is not required,
this admin site will be used about 1-2 times a month. Scalability and
performance is NOT an issue.
• Uploading of new images will be done from the "add" line of the
datagrid, the datagrid footer in my case, and will be handled by a sub
with an e.commandname="Insert"
• The images are to be uploaded, and then several checks must be done
before they are inserted into the db.
• First Check: Does the returned stream actually contain anything? If
not, an "add" to the database cannot occur (there must be an image).
• Second Check: Is the returned stream of the filetype "image/jpeg"? If
not, the "add" to the database cannot occur (the image must be a jpeg).
• Third Check: If any dimension of the image exceeds 640 pixels, it
needs to be proportionally re-sized to a maximum size of 640 per side.
That is, an 800x600 image is to be crunched to 640x480; a 400x800 image
is to be crunched to 320x640.
• Fourth Check: If the file size of the image exceeds 150kb, it is to be
resampled downwards until the file size meets that value.
• The images are to be inserted into the database along with a comment
field populated by a <textarea /> in the datagrid.
• Since the datagrids of the Admin site is the only place that needs
thumbnails (the main site doesn't), it needs a showimage.aspx (that can
accept a table name and a unique ID from the Get string) that re-sizes
the image requested (again, proportionally) in a dynamic manner before
the images make it to the browser. All thumbnails are to have a maximum
side length of 100 pixels. This re-sizing must be done inside the
showimage.aspx
Below is the SUB that I have managed to construct so far, but it seems
to input corrupted image data into the DB. Images can't be extracted,
even with a highly simplified showimage.aspx:
Sub dgCarShowGalleryPhotos_Insert(sender As Object, e As
DataGridCommandEventArgs)
If e.CommandName = "Insert" Then
Dim imgContent as Object = e.Item.Cells(2).Controls(1)
Dim strComment as String = CType(e.Item.Cells(3).Controls(1),
Textbox).Text.Replace("'","’")
Dim imgStream As Stream = imgContent.PostedFile.InputStream()
Dim imgType as String = imgContent.PostedFile.ContentType
Dim imgLen As Int64 = imgContent.PostedFile.ContentLength
Dim imgDimension as System.Drawing.Image =
System.Drawing.Image.FromStream(imgContent.PostedFile.InputStream)
Dim imgHeight as String = imgDimension.PhysicalDimension.Height
Dim imgWidth as String = imgDimension.PhysicalDimension.Width
Dim imgbin() as Byte
If Not imgStream Is Nothing Then
If imgContent.PostedFile.ContentLength > 0 And
imgContent.PostedFile.ContentType = "image/jpeg" Then
If imgWidth > 640 Or imgHeight > 640 Then
imgbin = createThumbnail(imgStream, 640, 640)
Else
Dim imgBinaryData(imgLen) as Byte
Dim intStatus as Integer = imgStream.Read(imgBinaryData, 0, imgLen)
imgBin = imgBinaryData
End If
CType(e.Item.FindControl("add_Comment"), TextBox).Text.Replace("'","’")
Dim strSQL As String = "INSERT INTO [tblCarShowGalleryPhotos]
([CarShowGalleryYearID], [Image], [Comment]) VALUES (" &
Session("IDHolder") & ", @Image, @Comment)"
Dim objConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
objConn.Open()
Dim myCommand as OleDbCommand = new OleDbCommand(strSQL, objConn)
myCommand.CommandType = CommandType.Text
Dim parameterImage as OleDbParameter = new OleDbParameter("@Image",
OleDbType.LongVarBinary)
parameterImage.Value = imgBin
myCommand.Parameters.Add(parameterImage)
Dim parameterComment as OleDbParameter = new OleDbParameter("@Comment",
OleDbType.LongVarWChar)
parameterComment.Value = strComment
myCommand.Parameters.Add(parameterComment)
myCommand.ExecuteNonQuery()
objConn.Close()
dgCarShowGalleryPhotos.EditItemIndex = -1
BindData()
End If
End If
End If
End Sub
And here is the showimage.aspx file, which is referenced everywhere a
thumbnail is required by the datagrid:
<%@ 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)
myConn.Open()
Dim myDataReader as OleDbDataReader =
myCmd.ExecuteReader(CommandBehavior.CloseConnection)
myDataReader.Read()
Dim imgStream(myDataReader.Item("Image")) As System.IO.MemoryStream
Dim imgStream As System.IO.MemoryStream
imgStream = myDataReader.Item("Image")
Dim imgbin as Array = createThumbnail(imgStream, 100, 100)
Response.ContentType="image/jpeg"
Response.BinaryWrite(imgbin)
myConn.Close()
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>
It also throws a number of very bad errors, particulary of the
information being of an incompatible type. "a 1-dimensional array of
byte cannot be converted to type of byte" and so forth.
Once again, I would like to say that there is NOTHING on the internet AT
ALL that is anywhere similar to what I am trying to create. If any of
you good ppl could help me find something that could fit the bill, or
correct any mistakes that I have made, I would be greatly indebted.
TIA
...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.
**********************************************************************