Answered by:
How do I reduce jpg image size as I am uploading a file to a webserver (Thumbnail) (VB)

Question
-
User-1995349159 posted
Dear Forum,
First of all, I would like to thank you for all of your help in the past. You really are a wealth of knowledge and support.I have a form that will upload a jpg to a folder on a webserver. I thought it would be neat if I could add a section of code that will take that large image, and make a thumbnail and upload that to another folder. I did some poking around and saw some people mentioning something called "Image Format Property" I didn't see any thing about this in my books.
How would I integrate something like that into my code?
Thanks,
JeffHere is a chunk of my existing code that takes the image chosen in the form and uploads it to the server, and then uploads the file name to the database
.
dim UpFilepic1 as HttpPostedFile = UP_FILE_pic1.PostedFileif UpFilePic1.ContentLength = nothing then
Dim NofilePic1 As String
NofilePic1= ""
Dim parameterpic1 As New SqlParameter("@pic1", SqlDbType.VarChar, 50)
parameterpic1.Value = NofilePic1
myCommand.Parameters.Add(parameterpic1)
else
UpFilePic1.SaveAs(Server.MapPath("\pics") & "/" & Path.GetFileName(UpFilepic1.FileName))
Dim fileExtensionpic1 as String = System.IO.Path.GetFileName(UpFilepic1.FileName)
Dim parameterpic1 As New SqlParameter("@pic1", SqlDbType.VarChar, 50)
parameterpic1.Value = fileExtensionpic1
myCommand.Parameters.Add(parameterpic1)
'Then take that same file, reduce it to either a percentage, or a set file size, and then upload it to another folder similarly. Maybe with a modifed name like samefilenamethumb.jpg
End ifThursday, May 7, 2009 10:33 PM
Answers
-
User-1179452826 posted
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 8, 2009 1:17 AM -
User-1179452826 posted
Can't say if your component requires 3.5 or not, but the error you're receiving is coz of the fact that you haven't registered the control with the page. To do so, you need to do something like this:
<%
@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>That needs to go right below the @Page directive on the page.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 9, 2009 12:46 AM -
User-1179452826 posted
Add a reference to the Atax Control Toolkit dll by right clicking the project in solution explorer>>add reference>>browse and browse to the dll. Alternatively, try putting the dll in the bin folder. Create the folder in the root of the website if it doesn't exist.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 15, 2009 11:09 AM -
User-1179452826 posted
Just put the AjaxControlToolkit.dll in a folder called bin in the root of the website. That should do it.
Also, you should really be lloking to use VS or at least the freely downloaded Visual Web Developer 2008 for developing asp.net. Asp.net develpment is as much about using the tools as it is about knowing how it works.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 16, 2009 12:10 AM
All replies
-
User-1179452826 posted
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 8, 2009 1:17 AM -
User-916962509 posted
I wouldn't bother with copying a thumbnail to another folder on upload if you're going to leave the full sized file in place anyway. Just create the thumbnail on-the-fly as and when you need it by way of an image handler (.ashx).
The above link should help, or http://www.codeproject.com/KB/GDI-plus/watermark.aspx gives you a great insight into the power of the System.Drawing namespace- If you can work your way through that article then you'll have a good idea how to do it by the end.
Good luck
bgs264
Friday, May 8, 2009 6:25 AM -
User-1995349159 posted
I appreciate pointing me in the right direction.
I looked at those tutorials, and some others that used vb.
Is .NET Framework 3.5 required to use this functionality? I am getting different errors on another server. So I put it on one that I am pretty sure has .net 3.5
I am getting an error: Unknown server tag 'cc1:GeneratedImage'.
Line 67: <cc1:GeneratedImage ID="ResizedImageGenerator" runat="server" ImageHandlerUrl="ResizeImageHandler.ashx">
What do you think I need to add so that it will understand that tag?
Thanks,
Jeff<%@ Page Explicit = "True" Language="VB" Debug="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace= System %>
<%@ Import Namespace= System.Web %>
<%@ Import Namespace= System.Data %>
<%@ Import Namespace= System.Web.UI %>
<%@ Import Namespace= System.Web.UI.Page %>
<%@ Import Namespace= System.Web.UI.WebControls %>
<%@ Import Namespace= System.Data.SQLClient %>
<%@ Import Namespace= System.IO %>
<%@ Import Namespace= System.Drawing %>
<%@ Import Namespace= Microsoft.Web %>
<script language="VB" runat="server">
Public Class ApplyImageTransformsHandler
Inherits ImageHandler
Public Sub New()
MyBase.ContentType = Imaging.ImageFormat.Jpeg
' Apply the image transform classes
MyBase.ImageTransforms.Add(New SomeImageTransformClass)
Dim someOtherTransform As New SomeOtherImageTransformClass()
someOtherTransform.SomeProperty = 5
...
MyBase.ImageTransforms.Add(someOtherTransform)
End Sub
Public Overrides Function GenerateImage(ByVal parameters As System.Collections.Specialized.NameValueCollection) As Microsoft.Web.ImageInfo
...
End Function
End Class
Public Class ResizeImageHandler
Inherits ImageHandlerPublic Sub New()
MyBase.ContentType = Imaging.ImageFormat.Jpeg
Dim resizeTrans As New ImageResizeTransform
resizeTrans.Width = 200
MyBase.ImageTransforms.Add(resizeTrans)
End SubPublic Overrides Function GenerateImage(ByVal parameters As System.Collections.Specialized.NameValueCollection) As Microsoft.Web.ImageInfo
'Get the parameters
Dim imageUrl As String = parameters("ImageUrl")
Dim imageFile As String = HttpContext.Current.Server.MapPath(imageUrl)If Not File.Exists(imageFile) Then
Throw New ArgumentException(String.Format("The file {0} could not be found", imageFile))
End IfReturn New ImageInfo(File.ReadAllBytes(imageFile))
End Function
End Class
</script><asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<cc1:GeneratedImage ID="ResizedImageGenerator" runat="server" ImageHandlerUrl="ResizeImageHandler.ashx">
<Parameters>
<cc1:ImageParameter Name="ImageUrl" Value="2.jpg" />
<cc1:ImageParameter Name="Width" Value="400" />
</Parameters>
</cc1:GeneratedImage></asp:Content>
Friday, May 8, 2009 2:25 PM -
User-1179452826 posted
Can't say if your component requires 3.5 or not, but the error you're receiving is coz of the fact that you haven't registered the control with the page. To do so, you need to do something like this:
<%
@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>That needs to go right below the @Page directive on the page.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 9, 2009 12:46 AM -
User-1995349159 posted
Thanks for the help. It looks like I am on the right track. However, I am getting an error about Ajax not being found. Do I need to download and install something from Microsoft?
Thanks,
Jeff
Parser Error Message: File or assembly name AjaxControlToolkit, or one of its dependencies, was not found.
Source Error:
Line 1: <%@ Page Explicit = "True" Language="VB" Debug="True" %> Line 2: <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> Line 3: <%@ Import Namespace="System.Data" %> Line 4: <%@ Import Namespace= System %>
Friday, May 15, 2009 11:03 AM -
User-1179452826 posted
Add a reference to the Atax Control Toolkit dll by right clicking the project in solution explorer>>add reference>>browse and browse to the dll. Alternatively, try putting the dll in the bin folder. Create the folder in the root of the website if it doesn't exist.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 15, 2009 11:09 AM -
User-1995349159 posted
Is this done in visual studio.net? I am typing these out in notepad. Is there a way to referrence it by hand in the code? Or is this something that is done in IIS?
Thanks
Jeff
Friday, May 15, 2009 2:18 PM -
User-1179452826 posted
Just put the AjaxControlToolkit.dll in a folder called bin in the root of the website. That should do it.
Also, you should really be lloking to use VS or at least the freely downloaded Visual Web Developer 2008 for developing asp.net. Asp.net develpment is as much about using the tools as it is about knowing how it works.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 16, 2009 12:10 AM