Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 17th, 2004, 06:26 AM
Authorized User
 
Join Date: Sep 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Resize image before saving?

With asp.net (c#) i've successfully uploaded image-files (jpg and gif) into my image directory.

However, before i save it to the server, I'd like to manipulate the width and height (while maintaining the aspect ratio), to save space on the server.
For example I have picture that is 1024x768 and I would like it to be 320x240 before saving it.
I would also like to rename the file to a name based on 'username' + 'a number'. Or to simplify it, just be able to choose a new name for the file.

Anyone who knows how to do this in asp.net (preferably c# but vb.net is ok)?

Also, if possible, is there a way to do the resizing on the client to save time for the upload?

Thanks in advance/moz

 
Old September 17th, 2004, 09:34 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

How are you keeping track of the images that get created on the server? Are they saved in a database?

In a system I built, the user uploads a picture which will be associated with them in a database. So I generate a GUID, save that to the DB, and save the file to the server with the name set to the GUID. This is a remarkably simple way of generating unique file names. It eliminates the need to look thru file lists to get the next number for a particular user. Of course, if you aren't saving anything to a database or list, then you might need to approach it differently.


I wouldn't recommend trying to write something that will resize on the client. That's just asking for trouble. You'll have to take into account a lot of things and it will be much more trouble than it's worth. The user should have some idea that if they are going to upload a 3 meg image, it's going to take some time.
 
Old September 17th, 2004, 09:47 AM
Authorized User
 
Join Date: Sep 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok. I figured it's like that, anyhooo..
Code you give me a code example of DB-procedure, I'm really interested in associating a picture to a post in the DB + saving the pic on the server as you described, how to do it?

And about the resizing, I think I'll be able to solve that myself so don't bother about that.

 
Old September 17th, 2004, 10:51 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Well.. all you need to do is great a guid:

Guid objGuid = Guid.NewGuid();

Then use that guid as the name of the file and for the string to put in the database.
 
Old September 18th, 2004, 06:54 AM
Authorized User
 
Join Date: Sep 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, I'm new at this, I don't get it. When I said that I could upload a file I meant to the server, I don't know how to upload an image to a database...at all. So I would like to get some more code showing the hole procedure (Upload the GUID-value to DB and the image with GUID-value as name to server).

The way I upload text to my database today is:
public void LäggTill(object obj, EventArgs e)
{
Page.Validate();
if (!Page.IsValid)
{
return;
}
if (tbMärke.Text == "" || tbModell.Text == "" || tbÅrsmodell.Text == "" || tbMotorCC.Text
== "" || tbFärg.Text == "" || tbRegNr.Text == "")
{
Message.Text = "Fel: Ingen ruta får vara tom";
Message.Style["color"] = "red";
BindGrid("");
return;
}
string sqlstr = "INSERT INTO EgnaBilar (Märke, Modell, Årsmodell, MotorCC, Färg, RegNr, Bild) VALUES ('" + tbMärke.Text + "','" + tbModell.Text + "','" + tbÅrsmodell.Text + "','"
+ tbMotorCC.Text + "','" + tbFärg.Text + "','" + tbRegNr.Text + "')";

SqlCommand myCommand = new SqlCommand(sqlstr, myConnection);

myCommand.Connection.Open();

try
{
myCommand.ExecuteNonQuery();
Message.Text = "<b>Posten är skapad</b><br>" + sqlstr.ToString() + "<br>&nbsp;";
}
catch (SqlException E)
{

if (E.Number == 2627)
{
Message.Text = "Det finns redan en post med samma RegNr<br>&nbsp;";
Message.Style["color"] = "red";
}
else
{
Message.Text = "Kunde inte skapa post, kolla att alla värden stämmer";
Message.Style["color"] = "red";
}

}

myCommand.Connection.Close();

BindGrid();

}

How can I change this to include the FileUpload + The GUID-function you described?
 
Old September 19th, 2004, 03:12 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Before you proceed with this you should read any of the 50 threads on this forum that address whether you should store an image in a DB or not. After you decide what you'd like to do, we can help with the upload code. It basically comes down to taking the binary stream of the image and storing that value to a data field instead of a text. I never store images to the database so I don't have any such example code handy.
 
Old September 20th, 2004, 05:54 AM
Authorized User
 
Join Date: Sep 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry I didn't make myself clear, I ment to go by the example that you provided in your first answer, upload a file to the server and use the GUID-example to associate it with a post in the DB.

"...So I generate a GUID, save that to the DB, and save the file to the server with the name set to the GUID..."

 
Old September 20th, 2004, 10:15 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Well, all you need to do is generate a new guid, as I showed you... then you use the guid string value in a regular old database row insert. The guid string will be the filename. If you use an HtmlInputFile control in the code-behind page class then you can use the save method (I forget the exact method name) to save the file to disk. When you call the method, you just use the guid string as the file name. This is your association between the DB entry and the file name.
 
Old September 21st, 2004, 04:20 AM
Authorized User
 
Join Date: Sep 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, I understand that I have to do that, however, since I'm so new to programming I would like help with the code. What I would like is a code that shows the entire procedure as:

code
void AddToDB
save file on server with the name from the GUID-function
save the association to the file in DB with the GUID-value (name of the file)

html
<form runat="server">
<asp:fileupload id="Pic" runat="server"/>
<asp:button Onclick="AddToDB" runat="server"/>
..etc

Can I use the code sample (which adds text to the DB) attached to my previous reply (18/9)?

 
Old September 22nd, 2004, 02:33 AM
Authorized User
 
Join Date: Sep 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey, I just realized that my Replies have been kinda indistinct, ha ha, must have been tired.
What I really want to know by now is the guid-function, I can't get it working, when I use it for renaming the file:

Guid objGuid = Guid.NewGuid();
String fileName = objGuid;

I get the message: Cannot implicitly convert type 'System.Guid' to 'string'
What am I supposed to do here to get it working?






Similar Threads
Thread Thread Starter Forum Replies Last Post
resize image dhoward VB.NET 2002/2003 Basics 1 June 14th, 2011 04:05 AM
php image resize danu_322 PHP FAQs 0 November 2nd, 2007 09:29 AM
Image Resize Magen Classic ASP Basics 5 January 23rd, 2007 12:17 PM
hyperlink image won't resize chowdn ASP.NET 2.0 Basics 0 October 23rd, 2006 01:19 PM
Image Resize after Upload echovue PHP How-To 2 July 13th, 2006 12:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.