By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,679 Members | 2,749 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,679 IT Pros & Developers. It's quick & easy.

Resize image from postef file without saving to disk VB.net?

P: 8
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.
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1"%>
  2. <%@ Import Namespace="System.IO" %>
  3. <%@ Import Namespace="System.Data" %>
  4. <%@ Import Namespace="System.Data.OleDb" %>
  5. <%@ Import Namespace="System.Configuration" %>
  6. <script language="VB" runat="server">
  7. Public  Sub UploadBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
  8.  
  9. 'Get the posted file
  10. Dim fileDataStream As Stream =  MyFile.PostedFile.InputStream 
  11.  
  12. 'get file type
  13. Dim FileType As string = MyFile.PostedFile.ContentType
  14. 'Get size of file
  15. Dim fileLength As Integer =  MyFile.PostedFile.ContentLength 
  16.  
  17. 'Create a byte array with file length
  18. Dim fileData() As Byte =  New Byte(fileLength) {} 
  19.  
  20. 'Read the stream into the byte array
  21. fileDataStream.Read(fileData,0,fileLength)
  22.  
  23. 'get file name (excluding the path)
  24.       Dim strFileNamePath as String = MyFile.PostedFile.FileName
  25.       Dim intFileNameLength = Instr(1, StrReverse(strFileNamePath), "\")
  26.       Dim strFileNameOnly as String = Mid(strFileNamePath, (Len(strFileNamePath)-intFileNameLength)+2)
  27.  
  28.            Dim DBConnection As New OleDbConnection(ConfigurationSettings.AppSettings("MSEMM_Connection_string"))
  29.         Dim UpdateCommand as new oledbcommand
  30.             UpdateCommand.Connection = DBConnection
  31.             UpdateCommand.Commandtype = Commandtype.text 
  32.             UpdateCommand.Commandtext= "INSERT INTO dbo.Files (FileName, FileSize, ContentType, FileData) Values(?,?,?,?)"
  33.             UpdateCommand.Parameters.AddWithValue("?", strFileNameOnly)
  34.             UpdateCommand.Parameters.AddWithValue("?", fileLength)
  35.             UpdateCommand.Parameters.AddWithValue("?", FileType)
  36.             UpdateCommand.Parameters.AddWithValue("?", fileData)
  37.  
  38.         DBConnection.open()
  39.         UpdateCommand.ExecuteNonQuery
  40.         DBConnection.Close()
  41.  
  42. End Sub
  43. </script>
  44. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  45. <html>
  46. <head>
  47. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  48. <title>file upload test</title>
  49. </head>
  50. <body>
  51. <h3>File Upload</h3>
  52.  
  53. <hr>
  54. <asp:label id="Message" Text="Select a file and filename" runat="server"/>
  55. <hr>
  56.  
  57. <h3>File Upload</h3>
  58.  
  59. <form enctype="multipart/form-data" runat="server">
  60.    File: <input id="myFile" type="file" runat="server">
  61.    <input type=button value="Upload" OnServerClick="UploadBtn_Click" runat="server">
  62. </form>
  63. </body>
  64. </html>
  65.  
Mar 1 '08 #1
Share this Question
Share on Google+
3 Replies


P: 8
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.
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" debug="true"%>
  2. <%@ Import Namespace="System.IO" %>
  3. <%@ Import Namespace="System.Data" %>
  4. <%@ Import Namespace="System.Data.OleDb" %>
  5. <%@ Import Namespace="System.Configuration" %>
  6. <%@ Import Namespace="System.Drawing" %>
  7. <%@ Import Namespace="System.Drawing.Imaging" %>
  8. <%@ Import Namespace="System.Drawing.Drawing2D" %>
  9. <script language="VB" runat="server">
  10. Public  Sub UploadBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
  11.  
  12. 'Get the posted file
  13. Dim fileDataStream As Stream =  MyFile.PostedFile.InputStream 
  14.  
  15. 'get file type
  16. Dim FileType As string = MyFile.PostedFile.ContentType
  17. 'Get size of file
  18. Dim fileLength As Integer =  MyFile.PostedFile.ContentLength 
  19.  
  20. 'Create a byte array with file length
  21. Dim fileData() As Byte =  New Byte(fileLength) {} 
  22.  
  23. 'Read the stream into the byte array
  24. fileDataStream.Read(fileData,0,fileLength)
  25.  
  26.  
  27. Dim origImg As System.Drawing.Image
  28.     origImg = System.Drawing.Image.FromStream(fileDataStream)
  29. Dim imageWidth As Integer = origImg.Width
  30. Dim imageHeight As Integer = origImg.Height
  31.  
  32.  
  33. Dim processedBP As Bitmap = New Bitmap (180, 120)
  34. Dim g As Graphics = Graphics.FromImage(processedBP)
  35.     g.SmoothingMode = SmoothingMode.HighQuality
  36.     g.InterpolationMode = InterpolationMode.HighQualityBicubic
  37.     g.PixelOffsetMode = PixelOffsetMode.HighQuality
  38. Dim rect As Rectangle = New Rectangle (0, 0, 180, 120)
  39.     g.DrawImage(origImg, rect, 0, 0, origImg.Width, origImg.Height,GraphicsUnit.Pixel)
  40.  
  41. Dim processedMemStream As MemoryStream = New MemoryStream ()
  42. processedBP.Save(processedMemStream, ImageFormat.Jpeg)
  43. dim FileType2 as string = "image/jpeg"
  44.  
  45. 'Dim processedImageData As Byte = processedMemStream.ToArray()
  46. Dim fileLength2 As Integer = processedMemStream.Length.ToString()
  47. Dim fileData2() As Byte =  New Byte(fileLength2) {}
  48. processedMemStream.Read(fileData2,0,fileLength2)
  49.  
  50. 'get file name (excluding the path)
  51.       Dim strFileNamePath as String = MyFile.PostedFile.FileName
  52.       Dim intFileNameLength = Instr(1, StrReverse(strFileNamePath), "\")
  53.       Dim strFileNameOnly as String = Mid(strFileNamePath, (Len(strFileNamePath)-intFileNameLength)+2)
  54.  
  55.            Dim DBConnection As New OleDbConnection(ConfigurationSettings.AppSettings("MSEMM_Connection_string"))
  56.         Dim UpdateCommand as new oledbcommand
  57.             UpdateCommand.Connection = DBConnection
  58.             UpdateCommand.Commandtype = Commandtype.text 
  59.             UpdateCommand.Commandtext= "INSERT INTO dbo.Files (FileName, FileSize, ContentType, FileData) Values(?,?,?,?)"
  60.             UpdateCommand.Parameters.AddWithValue("?", strFileNameOnly)
  61.             UpdateCommand.Parameters.AddWithValue("?", fileLength2)
  62.             UpdateCommand.Parameters.AddWithValue("?", FileType2)
  63.             UpdateCommand.Parameters.AddWithValue("?", fileData2)
  64.  
  65.         DBConnection.open()
  66.         UpdateCommand.ExecuteNonQuery
  67.         DBConnection.Close()
  68.  
  69. End Sub
  70. </script>
  71. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  72. <html>
  73. <head>
  74. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  75. <title>file upload test</title>
  76. </head>
  77. <body>
  78. <h3>File Upload</h3>
  79.  
  80. <hr>
  81. <asp:label id="Message" Text="Select a file and filename" runat="server"/>
  82. <hr>
  83.  
  84. <h3>File Upload</h3>
  85.  
  86. <form enctype="multipart/form-data" runat="server">
  87.    File: <input id="myFile" type="file" runat="server">
  88.    <input type=button value="Upload" OnServerClick="UploadBtn_Click" runat="server">
  89. </form>
  90. </body>
  91. </html>
  92.  
Mar 1 '08 #2

P: 8
Don't worry I've sorted it out myself, I will post the code when i have prettied it up.
Mar 2 '08 #3

kenobewan
Expert 2.5K+
P: 4,871
Well done, we gave you the placebo and it worked ;)
Mar 2 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.