tommy10101
asked on
ASPJPEG + ASPUPLOAD.. Using VB!
Hi All,
I have a classic ASP page that uses ASPJPEG and ASPLOAD together .. It uploads the image directly into memory, resizes it, and then inserts it into a MS-SQL Database.
I need code that will do the exact same thing, but using ASP.NET; in VB... Unfortunately, the persits site has examples that are in C# only.
Can someone paste me their code? (Upload, resize, insert into MS-SQL) .. in VB!
I have a classic ASP page that uses ASPJPEG and ASPLOAD together .. It uploads the image directly into memory, resizes it, and then inserts it into a MS-SQL Database.
I need code that will do the exact same thing, but using ASP.NET; in VB... Unfortunately, the persits site has examples that are in C# only.
Can someone paste me their code? (Upload, resize, insert into MS-SQL) .. in VB!
To upload to blob use this:
<form runat="server" enctype="multipart/form-da ta" ID="Form2">
<P>
<input type="file" id="file1" runat="server" NAME="file1">
</P>
<P>
<asp:Button id="btn1" runat="server" text="Upload" onclick="upload" />
</P>
</form>
-------------------------- ---------- ---------- ---------- ---------- ---------- ----------
Imports System.Data
Imports System.Data.SqlClient
Public Sub Upload(ByVal sender As Object, ByVal e As System.EventArgs)
Dim b(file1.PostedFile.InputSt ream.Lengt h - 1) As Byte
file1.PostedFile.InputStre am.Read(b, 0, file1.PostedFile.InputStre am.Length)
Dim con As New SqlConnection(Configuratio nSettings. AppSetting s("Connect ionStringS QL"))
Dim sql As String = "INSERT INTO MY_TABLE(MyID, DATABLOB) VALUES(1,@BlobData) "
Dim cmd As New SqlCommand(sql, con)
Dim parmBlob As New SqlParameter("@BlobData", SqlDbType.VarBinary, _
b.Length, ParameterDirection.Input, False, 0, _
0, Nothing, DataRowVersion.Current, b)
cmd.Parameters.Add(parmBlo b)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
You will need to run in trough the image resizing class I gave you if you need that as well.
<form runat="server" enctype="multipart/form-da
<P>
<input type="file" id="file1" runat="server" NAME="file1">
</P>
<P>
<asp:Button id="btn1" runat="server" text="Upload" onclick="upload" />
</P>
</form>
--------------------------
Imports System.Data
Imports System.Data.SqlClient
Public Sub Upload(ByVal sender As Object, ByVal e As System.EventArgs)
Dim b(file1.PostedFile.InputSt
file1.PostedFile.InputStre
Dim con As New SqlConnection(Configuratio
Dim sql As String = "INSERT INTO MY_TABLE(MyID, DATABLOB) VALUES(1,@BlobData) "
Dim cmd As New SqlCommand(sql, con)
Dim parmBlob As New SqlParameter("@BlobData", SqlDbType.VarBinary, _
b.Length, ParameterDirection.Input, False, 0, _
0, Nothing, DataRowVersion.Current, b)
cmd.Parameters.Add(parmBlo
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
You will need to run in trough the image resizing class I gave you if you need that as well.
ASKER
Hi there Scott!
Sorry for the confusion (my fault). I actually didn't mean for you to reinvent the wheel by giving me code that is * equivalent * to Persits ASPJPEG.. What I need * IS * the code to use for Persits ASPJPEG and ASPUPLOAD, in .NET (VB) ..
I'll try your solution also, I appreciate you giving me your code! Do you have the Persits modules I'm talking about? Also, is there a website I can test that is using the code above? Thanks again!
-Tom
Sorry for the confusion (my fault). I actually didn't mean for you to reinvent the wheel by giving me code that is * equivalent * to Persits ASPJPEG.. What I need * IS * the code to use for Persits ASPJPEG and ASPUPLOAD, in .NET (VB) ..
I'll try your solution also, I appreciate you giving me your code! Do you have the Persits modules I'm talking about? Also, is there a website I can test that is using the code above? Thanks again!
-Tom
This code is included only in the administrative section of my n-tier applications. Is there some functionality that your uploading module adds? I just use my image resizing class, and the built in ASP.NET file upload mechanism works great. Sorry I misunderstood you, but I really have no experience with the module you mentioned but I think you'll be happy with my code if you try it;) I'm probably a little biased hehe anyway let me know if theres anything else I can do to help you out.
Regards,
Aeros
Regards,
Aeros
ASKER
I pasted your code into a page, instead of using it as a code-behind .. I put the script tags around it, and it produces an error ..
<script runat="server">
* your code went here *
</script>
------------------------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30188: Declaration expected.
Source Error:
Line 1: <script runat="server">
Line 2: Region " AddProduct Button "
Line 3: Private Sub AddProduct_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddProduct.Click
Line 4: If IsRefresh Then Return
-------------------------- ---------- ---------- ---------- -
.... by the way, what is the #region thing?
<script runat="server">
* your code went here *
</script>
------------------------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30188: Declaration expected.
Source Error:
Line 1: <script runat="server">
Line 2: Region " AddProduct Button "
Line 3: Private Sub AddProduct_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddProduct.Click
Line 4: If IsRefresh Then Return
--------------------------
.... by the way, what is the #region thing?
thats generated by the ide for the codebehind you would need to take it out, although I highly suggest you develop using the codebehind.
This is the only html you neee along with your form tags to create a file upload box:
<TD style="HEIGHT: 34px">Picture <SPAN class="SmallText">(optiona l)</SPAN>< /TD>
<TD style="HEIGHT: 34px"><INPUT class="ThinInput" id="ImageSrc" type="file" size="15" runat="server"></TD>
<TD style="HEIGHT: 34px">Picture <SPAN class="SmallText">(optiona
<TD style="HEIGHT: 34px"><INPUT class="ThinInput" id="ImageSrc" type="file" size="15" runat="server"></TD>
Private Sub AddProduct_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddProduct.Click
If IsRefresh Then Return
ProductNameValidator.Enabl ed = True
Tier1Validator.Enabled = True
QueryValidator.Enabled = False
Validate()
If Not IsValid Then Return
Dim cnn As New OleDb.OleDbConnection(Conf igurationS ettings.Ap pSettings( "SiteDB"))
Dim cmd As New OleDb.OleDbCommand
Dim IncomingFile As IO.FileInfo
Dim ImageSource, PriceValue As String
Dim ImageResizer As New ResizeImage(True, 300, 400)
ImageResizer.ThumbX = 100
ImageResizer.ThumbY = 100
If ImageSrc.PostedFile.Conten tLength <> 0 Then
IncomingFile = New IO.FileInfo(Request.Files( 0).FileNam e)
IncomingFile = New IO.FileInfo(Server.MapPath ("~/Pictur es/Product s/") & IncomingFile.Name)
Request.Files(0).SaveAs(In comingFile .FullName)
ImageSource = ImageResizer.ResizeImage(I ncomingFil e.FullName )
End If
If Price.Text = "" Then
PriceValue = "0"
Else
PriceValue = Price.Text
End If
cmd.CommandType = CommandType.Text
cmd.CommandText = "INSERT INTO Products (Tier1ID, Tier2ID, Tier3ID, Name, " & _
"Description, ImageSrc, Price) VALUES (" & Tier1.SelectedValue & ", " & _
Tier2.SelectedValue & ", " & Tier3.SelectedValue & ", '" & DBSafe(ProductName.Text) & _
"', '" & DBSafe(Description.Text) & "', '" & ImageSource & "', " & PriceValue & ")"
cmd.Connection = cnn
cnn.Open()
cmd.ExecuteNonQuery()
cnn.Close()
cmd.Dispose()
cnn.Dispose()
LoadProductData()
End Sub
That would be the codebehind, note you would still need to include the image resizing class. Just go to new then class in visual studio and paste the image resizing class in. Then you ll need to add that class to your project.
If IsRefresh Then Return
ProductNameValidator.Enabl
Tier1Validator.Enabled = True
QueryValidator.Enabled = False
Validate()
If Not IsValid Then Return
Dim cnn As New OleDb.OleDbConnection(Conf
Dim cmd As New OleDb.OleDbCommand
Dim IncomingFile As IO.FileInfo
Dim ImageSource, PriceValue As String
Dim ImageResizer As New ResizeImage(True, 300, 400)
ImageResizer.ThumbX = 100
ImageResizer.ThumbY = 100
If ImageSrc.PostedFile.Conten
IncomingFile = New IO.FileInfo(Request.Files(
IncomingFile = New IO.FileInfo(Server.MapPath
Request.Files(0).SaveAs(In
ImageSource = ImageResizer.ResizeImage(I
End If
If Price.Text = "" Then
PriceValue = "0"
Else
PriceValue = Price.Text
End If
cmd.CommandType = CommandType.Text
cmd.CommandText = "INSERT INTO Products (Tier1ID, Tier2ID, Tier3ID, Name, " & _
"Description, ImageSrc, Price) VALUES (" & Tier1.SelectedValue & ", " & _
Tier2.SelectedValue & ", " & Tier3.SelectedValue & ", '" & DBSafe(ProductName.Text) & _
"', '" & DBSafe(Description.Text) & "', '" & ImageSource & "', " & PriceValue & ")"
cmd.Connection = cnn
cnn.Open()
cmd.ExecuteNonQuery()
cnn.Close()
cmd.Dispose()
cnn.Dispose()
LoadProductData()
End Sub
That would be the codebehind, note you would still need to include the image resizing class. Just go to new then class in visual studio and paste the image resizing class in. Then you ll need to add that class to your project.
ASKER
Hmm.. I took out the region open and close tags for now .. and it still gives me an error .. I guess I'm stuck here.
ASKER
Just saw your last post .. will try that .. brb
ASKER
Where is the "new" and then "class"? If I go to "File" and "new", I don't see class as one of the options .. you on AIM by any chance?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<TD style="HEIGHT: 34px">Picture <SPAN class="SmallText">(optiona
<TD style="HEIGHT: 34px"><INPUT class="ThinInput" id="ImageSrc" type="file" size="15" runat="server"></TD>
The Code Behind:
Region " AddProduct Button "
Private Sub AddProduct_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddProduct.Click
If IsRefresh Then Return
ProductNameValidator.Enabl
Tier1Validator.Enabled = True
QueryValidator.Enabled = False
Validate()
If Not IsValid Then Return
Dim cnn As New OleDb.OleDbConnection(Conf
Dim cmd As New OleDb.OleDbCommand
Dim IncomingFile As IO.FileInfo
Dim ImageSource, PriceValue As String
Dim ImageResizer As New ResizeImage(True, 300, 400)
ImageResizer.ThumbX = 100
ImageResizer.ThumbY = 100
If ImageSrc.PostedFile.Conten
IncomingFile = New IO.FileInfo(Request.Files(
IncomingFile = New IO.FileInfo(Server.MapPath
Request.Files(0).SaveAs(In
ImageSource = ImageResizer.ResizeImage(I
End If
If Price.Text = "" Then
PriceValue = "0"
Else
PriceValue = Price.Text
End If
cmd.CommandType = CommandType.Text
cmd.CommandText = "INSERT INTO Products (Tier1ID, Tier2ID, Tier3ID, Name, " & _
"Description, ImageSrc, Price) VALUES (" & Tier1.SelectedValue & ", " & _
Tier2.SelectedValue & ", " & Tier3.SelectedValue & ", '" & DBSafe(ProductName.Text) & _
"', '" & DBSafe(Description.Text) & "', '" & ImageSource & "', " & PriceValue & ")"
cmd.Connection = cnn
cnn.Open()
cmd.ExecuteNonQuery()
cnn.Close()
cmd.Dispose()
cnn.Dispose()
LoadProductData()
End Sub
#End Region
Image Resizing Class
Imports System.Drawing
Imports System.Drawing.Imaging
Public Class ResizeImage
Private _CreateThumbnail As Boolean
Private _ThumbX, _ThumbY, _FullSizeX, _FullSizeY As Integer
Private UnacceptableImageFormats As ArrayList
Public Property CreateThumbnail() As Boolean
Get
Return _CreateThumbnail
End Get
Set(ByVal Value As Boolean)
_CreateThumbnail = Value
End Set
End Property
Public Property FullSizeX() As Integer
Get
Return _FullSizeX
End Get
Set(ByVal Value As Integer)
_FullSizeX = Value
End Set
End Property
Public Property FullSizeY() As Integer
Get
Return _FullSizeY
End Get
Set(ByVal Value As Integer)
_FullSizeY = Value
End Set
End Property
Public Property ThumbX() As Integer
Get
Return _ThumbX
End Get
Set(ByVal Value As Integer)
_ThumbX = Value
End Set
End Property
Public Property ThumbY() As Integer
Get
Return _ThumbY
End Get
Set(ByVal Value As Integer)
_ThumbY = Value
End Set
End Property
Public Sub New(ByVal CreateThumbnail As Boolean, ByVal FullSizeX As Integer, ByVal FullSizeY As Integer)
_CreateThumbnail = CreateThumbnail
_FullSizeX = FullSizeX
_FullSizeY = FullSizeY
UnacceptableImageFormats = New ArrayList(10)
With UnacceptableImageFormats
.Add(Imaging.PixelFormat.F
.Add(Imaging.PixelFormat.F
.Add(Imaging.PixelFormat.F
.Add(Imaging.PixelFormat.U
.Add(Imaging.PixelFormat.D
.Add(Imaging.PixelFormat.F
.Add(Imaging.PixelFormat.F
End With
End Sub
Public Function ResizeImage(ByVal ImageSource As String) As String
Dim IncomingImage As System.Drawing.Image
Dim OutputBitmap As Bitmap
Dim JpegCodec As ImageCodecInfo
Dim JpegEncodeParams As EncoderParameters
Dim NewSize As Size
Dim fiSource As New IO.FileInfo(ImageSource)
Dim fsSource As IO.FileStream
Dim fiDestination As New IO.FileInfo(ImageSource)
Dim NewFilename As String
fiSource.MoveTo(fiSource.D
fsSource = fiSource.OpenRead()
IncomingImage = IncomingImage.FromStream(f
OutputBitmap = RedrawImage(IncomingImage,
If fiSource.Extension = ".gif" Then
NewFilename = fiDestination.Name
OutputBitmap.Save(fiDestin
Else
JpegCodec = ReturnJpegCodec()
JpegEncodeParams = ReturnEncoderParams()
NewFilename = fiDestination.Name.Replace
OutputBitmap.Save(fiDestin
JpegCodec, JpegEncodeParams)
End If
If CreateThumbnail Then
OutputBitmap = RedrawImage(IncomingImage,
If fiSource.Extension = ".gif" Then
OutputBitmap.Save(fiDestin
Else
JpegCodec = ReturnJpegCodec()
JpegEncodeParams = ReturnEncoderParams()
OutputBitmap.Save(fiDestin
fiDestination.Name.Replace
End If
End If
fsSource.Close()
OutputBitmap.Dispose()
IncomingImage.Dispose()
fiSource.Delete()
Return NewFilename
End Function
Private Function RedrawImage(ByVal IncomingImage As Image, ByVal MaximumX As Integer, _
ByVal MaximumY As Integer) As Bitmap
Dim ResizedX, ResizedY As Integer
Dim Bitmap As Bitmap
Dim Graphic As Graphics
Dim NewSize As Size
ResizedY = CInt((IncomingImage.Height
ResizedX = CInt((IncomingImage.Width * MaximumY) / IncomingImage.Height)
If ResizedY >= MaximumY Then
NewSize = New Size(ResizedX, MaximumY)
Else
NewSize = New Size(MaximumX, ResizedY)
End If
Bitmap = New Bitmap(IncomingImage, NewSize)
Bitmap.SetResolution(72, 72)
'Some graphic pixel formats prevent the image from being converted into an instance of Graphics.
' If this is the case, return just the resized bitmap without processing.
If UnacceptableImageFormats.C
'All pixel formats that Microsoft says will generate exceptions have been loaded into the array
' above, yet some pixel formats are still throwing exceptions. Resorted to a Try / Catch block - AM
Try
Graphic = Graphics.FromImage(Bitmap)
Catch
Return Bitmap
End Try
Graphic.SmoothingMode = Drawing2D.SmoothingMode.Hi
Graphic.InterpolationMode = Drawing2D.InterpolationMod
Graphic.PixelOffsetMode = Drawing2D.PixelOffsetMode.
Graphic.DrawImage(Bitmap, 0, 0)
Graphic.Dispose()
Return Bitmap
End Function
Private Function ReturnJpegCodec() As ImageCodecInfo
Dim codecs As Imaging.ImageCodecInfo() = ImageCodecInfo.GetImageEnc
For Each codec As ImageCodecInfo In codecs
If codec.MimeType.Equals("ima
Next
End Function
Private Function ReturnEncoderParams() As EncoderParameters
Dim EncoderInstance As Encoder
Dim EncoderParametersInstance As New EncoderParameters(2)
Dim QualityParameter As EncoderParameter
Dim ColorParameter As EncoderParameter
EncoderInstance = Encoder.Quality
QualityParameter = New EncoderParameter(EncoderIn
EncoderParametersInstance.
EncoderInstance = Encoder.ColorDepth
ColorParameter = New EncoderParameter(EncoderIn
EncoderParametersInstance.
Return EncoderParametersInstance
End Function
End Class
This example is actually an access db, but just change the providers to the mysql client and your set. Also this example assumes you only want to keep the path in the db. I'll post an additional snippit incase you want to store them as blobs in your db.
Regards,
Aeros