Hi,
I've just made the change from ASP to .net.
I have a file (code below) that saves a user submitted file to a MS SQL 2005 database. It collects the file name, file size, file type, and lastly the binary data for the file.
I can sucessfully take the files out of the databse again and display them in a data grid.
I would like to resize the submitted file to a fixed size (say 180 x 120) before I upload it to the database and do this without saving it to a temporary file on the servers HDD.
I also do not want to use the "thumbnail method" as I want to keep the quality of the resized smaller images as high as possible.
The code below is stripped down and does not have any validation to check for files that are not images etc, I will add that later. I also don't mind if i can only rezise and store in the database jpeg and gif files, but I need to be able handle at least these two image types. -
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1"%>
-
<%@ Import Namespace="System.IO" %>
-
<%@ Import Namespace="System.Data" %>
-
<%@ Import Namespace="System.Data.OleDb" %>
-
<%@ Import Namespace="System.Configuration" %>
-
<script language="VB" runat="server">
-
Public Sub UploadBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
-
-
'Get the posted file
-
Dim fileDataStream As Stream = MyFile.PostedFile.InputStream
-
-
'get file type
-
Dim FileType As string = MyFile.PostedFile.ContentType
-
'Get size of file
-
Dim fileLength As Integer = MyFile.PostedFile.ContentLength
-
-
'Create a byte array with file length
-
Dim fileData() As Byte = New Byte(fileLength) {}
-
-
'Read the stream into the byte array
-
fileDataStream.Read(fileData,0,fileLength)
-
-
'get file name (excluding the path)
-
Dim strFileNamePath as String = MyFile.PostedFile.FileName
-
Dim intFileNameLength = Instr(1, StrReverse(strFileNamePath), "\")
-
Dim strFileNameOnly as String = Mid(strFileNamePath, (Len(strFileNamePath)-intFileNameLength)+2)
-
-
Dim DBConnection As New OleDbConnection(ConfigurationSettings.AppSettings("MSEMM_Connection_string"))
-
Dim UpdateCommand as new oledbcommand
-
UpdateCommand.Connection = DBConnection
-
UpdateCommand.Commandtype = Commandtype.text
-
UpdateCommand.Commandtext= "INSERT INTO dbo.Files (FileName, FileSize, ContentType, FileData) Values(?,?,?,?)"
-
UpdateCommand.Parameters.AddWithValue("?", strFileNameOnly)
-
UpdateCommand.Parameters.AddWithValue("?", fileLength)
-
UpdateCommand.Parameters.AddWithValue("?", FileType)
-
UpdateCommand.Parameters.AddWithValue("?", fileData)
-
-
DBConnection.open()
-
UpdateCommand.ExecuteNonQuery
-
DBConnection.Close()
-
-
End Sub
-
</script>
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
-
<html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
-
<title>file upload test</title>
-
</head>
-
<body>
-
<h3>File Upload</h3>
-
-
<hr>
-
<asp:label id="Message" Text="Select a file and filename" runat="server"/>
-
<hr>
-
-
<h3>File Upload</h3>
-
-
<form enctype="multipart/form-data" runat="server">
-
File: <input id="myFile" type="file" runat="server">
-
<input type=button value="Upload" OnServerClick="UploadBtn_Click" runat="server">
-
</form>
-
</body>
-
</html>
-
3 2349
After some more work I've come up with this, it doesn't come up with an error at run time, and when i check the database there is data in it and the size of the file has decreased, but the image doesn't display, the other images in the database the were uploaded with the original file work fine.
The code of the updated file is below. -
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" debug="true"%>
-
<%@ Import Namespace="System.IO" %>
-
<%@ Import Namespace="System.Data" %>
-
<%@ Import Namespace="System.Data.OleDb" %>
-
<%@ Import Namespace="System.Configuration" %>
-
<%@ Import Namespace="System.Drawing" %>
-
<%@ Import Namespace="System.Drawing.Imaging" %>
-
<%@ Import Namespace="System.Drawing.Drawing2D" %>
-
<script language="VB" runat="server">
-
Public Sub UploadBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
-
-
'Get the posted file
-
Dim fileDataStream As Stream = MyFile.PostedFile.InputStream
-
-
'get file type
-
Dim FileType As string = MyFile.PostedFile.ContentType
-
'Get size of file
-
Dim fileLength As Integer = MyFile.PostedFile.ContentLength
-
-
'Create a byte array with file length
-
Dim fileData() As Byte = New Byte(fileLength) {}
-
-
'Read the stream into the byte array
-
fileDataStream.Read(fileData,0,fileLength)
-
-
-
Dim origImg As System.Drawing.Image
-
origImg = System.Drawing.Image.FromStream(fileDataStream)
-
Dim imageWidth As Integer = origImg.Width
-
Dim imageHeight As Integer = origImg.Height
-
-
-
Dim processedBP As Bitmap = New Bitmap (180, 120)
-
Dim g As Graphics = Graphics.FromImage(processedBP)
-
g.SmoothingMode = SmoothingMode.HighQuality
-
g.InterpolationMode = InterpolationMode.HighQualityBicubic
-
g.PixelOffsetMode = PixelOffsetMode.HighQuality
-
Dim rect As Rectangle = New Rectangle (0, 0, 180, 120)
-
g.DrawImage(origImg, rect, 0, 0, origImg.Width, origImg.Height,GraphicsUnit.Pixel)
-
-
Dim processedMemStream As MemoryStream = New MemoryStream ()
-
processedBP.Save(processedMemStream, ImageFormat.Jpeg)
-
dim FileType2 as string = "image/jpeg"
-
-
'Dim processedImageData As Byte = processedMemStream.ToArray()
-
Dim fileLength2 As Integer = processedMemStream.Length.ToString()
-
Dim fileData2() As Byte = New Byte(fileLength2) {}
-
processedMemStream.Read(fileData2,0,fileLength2)
-
-
'get file name (excluding the path)
-
Dim strFileNamePath as String = MyFile.PostedFile.FileName
-
Dim intFileNameLength = Instr(1, StrReverse(strFileNamePath), "\")
-
Dim strFileNameOnly as String = Mid(strFileNamePath, (Len(strFileNamePath)-intFileNameLength)+2)
-
-
Dim DBConnection As New OleDbConnection(ConfigurationSettings.AppSettings("MSEMM_Connection_string"))
-
Dim UpdateCommand as new oledbcommand
-
UpdateCommand.Connection = DBConnection
-
UpdateCommand.Commandtype = Commandtype.text
-
UpdateCommand.Commandtext= "INSERT INTO dbo.Files (FileName, FileSize, ContentType, FileData) Values(?,?,?,?)"
-
UpdateCommand.Parameters.AddWithValue("?", strFileNameOnly)
-
UpdateCommand.Parameters.AddWithValue("?", fileLength2)
-
UpdateCommand.Parameters.AddWithValue("?", FileType2)
-
UpdateCommand.Parameters.AddWithValue("?", fileData2)
-
-
DBConnection.open()
-
UpdateCommand.ExecuteNonQuery
-
DBConnection.Close()
-
-
End Sub
-
</script>
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
-
<html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
-
<title>file upload test</title>
-
</head>
-
<body>
-
<h3>File Upload</h3>
-
-
<hr>
-
<asp:label id="Message" Text="Select a file and filename" runat="server"/>
-
<hr>
-
-
<h3>File Upload</h3>
-
-
<form enctype="multipart/form-data" runat="server">
-
File: <input id="myFile" type="file" runat="server">
-
<input type=button value="Upload" OnServerClick="UploadBtn_Click" runat="server">
-
</form>
-
</body>
-
</html>
-
Don't worry I've sorted it out myself, I will post the code when i have prettied it up.
Well done, we gave you the placebo and it worked ;)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Tim T |
last post by:
Hi,
Could someone please point to to a tutorial / code for dynamically resizing
images on upload, THEN saving to disk on the webserver.
I need users to be able to upload images to my server, but...
|
by: Gustavo De la Espriella |
last post by:
Hi,
I'm having trouble resizing images while mantaining resolution.
I used the following code to save a jpeg file to disk, but with the
GetThumbImage it loses resolution....
|
by: lgbjr |
last post by:
Hello All,
I¡¯m using a context menu associated with some pictureboxes to provide
copy/paste functionality. Copying the image to the clipboard was easy. But
pasting an image from the clipboard...
|
by: David Lozzi |
last post by:
Howdy,
I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders!
Now I am making a public function that will take the...
|
by: Mark Denardo |
last post by:
Hi, I need some expert GDI+ person to help me with my RoundOffImage
Function:
What I'm trying to do is take in an image, crop off the edges around an
ellipse region I set up, and then return the...
|
by: Dale |
last post by:
I am creating GIF images with transparent backgrounds on-the-fly for a web
app and rendering them by using
System.Drawing.Image.Save(Response.OutputStream, ImageType.GIF).
I am confident that...
|
by: Mark Denardo |
last post by:
My question is similar to one someone posted a few months back, but I don't
see any replies.
Basically I want to be able to have users upload photos and save them in a
database (as byte data)...
|
by: Waqas.L.Khan |
last post by:
Hi guys,
I have a problem when trying to create an image file. Basically my
code takes any file and gets it's system icon using SHGetFileInfo and
then saves the file either by converting it into...
|
by: Noorain |
last post by:
Sir
i want to resize image. Following script working in my local server. But This coding doesn't work in php 2.6.0. please help me
<form action="<?php echo $_server; ?>" method="post"...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |