.NET Discussion

.NET Issues, Problems, Code Samples, and Fixes

ASP.NET: Restrict Dimensions While Uploading Images With The FileUpload Control

I just had to deal with uploading images to my server and had to figure out a way to restrict my users’ uploads to certain dimensions. It’s pretty easy to do, just not entirely intuitive at first. So, I’ll take the guesswork out of it for you 🙂

My project requires that the images be no wider than 190px and must be exactly 190px tall.

First, add a FileUpload control to your form and an upload button:
<asp:FileUpload id="FileUpload1" runat="server" /><asp:Button id="Button1" runat="server" text="Upload" />

Next, I created a generic helper function for all file uploads, but you don’t have to do that. All you need to do is do the following:

Const PROD_IMG_MAX_WIDTH As Integer = 190
Const PROD_IMG_HEIGHT As Integer = 190
Const ABS_PATH as String = "[Your Server's Absolute Path, usually C:\something\weborsomethingelse\yoursite.com]"

Dim savePath As String = ABS_PATH & "\images\"

'Create an image object from the uploaded file
Dim UploadedImage As System.Drawing.Image = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream)

If UploadedImage.Width > PROD_IMG_MAX_WIDTH Or UploadedImage.Height <> PROD_IMG_HEIGHT Then
     'whatever fail code you need
Else
     savePath &= FileUpload1.FileName
     FileUpload1.SaveAs(savePath)
End If

And that’s pretty much all there is to it! Of course you can modify it to accept different folders, change the filename, or anything of that sort. The key is really determining the dimensions from the InputStream. Everything else is gravy.

NOTES: The FileUpload control is only available in .NET 2.0. Also, if your FileUpload control is on an AJAX-ified page, you will need to do some trigger fixes, since you can’t upload a file using AJAX, at least not yet (easily). Lastly, you need to make sure that your server will allow you to write to it from the web. If you’re having problems, talk to your host to have them grant you upload/write access to the server.

kick it on DotNetKicks.com

Advertisements

November 27, 2007 Posted by | ASP.NET, Tips & Tricks | | 11 Comments

ASP.NET Gridview: Merging Cells

I had been searching for a way to merge cells in a GridView, and recently the answer was given to me by Mike Flavin (thanks!). Place the following code, or some variation thereof depending on your requirements, in the RowBinding event of your GridView:

If e.Row.DataItemIndex % 2 = 0 Then
     e.Row.Cells(0).RowSpan = 2
     e.Row.Cells(1).RowSpan = 2
End If
'Remove the extra cells created due to row span for odd rows
If e.Row.DataItemIndex % 2 = 1 Then
     e.Row.Cells.RemoveAt(0)
     e.Row.Cells.RemoveAt(0)
     e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Center
End If

This code should take every cell in the first and second columns and merge it with the cell directly below it. Of course, this should be used very carefully, because you could screw up your data if each record does not have another corresponding record with it. However, this shouldn’t be too difficult to program around by adding data checks.

November 15, 2007 Posted by | ASP.NET, GridView, Tips & Tricks | 5 Comments

ASP.NET: (FREE!) Online Image Editor For Your Site

While reading my regular CodeProject newsletter this morning, I came across an interesting article about an open-source .NET image editor (implementing AJAX) that can be implemented into your website! I have not tried to use it yet, but I most likely will in an upcoming site that I am building.  From the demo version, it appears to have all the basic functionality a regular, non-power user would need to upload and edit pictures for a photo gallery (something that will be necessary in my next project, also open-source and found on CodeProject). Again, I have not tried this yet, but when I do, I will post my experience here, of course.

Wouldn’t it be nice to create a fully-functional,  powerful, feature-rich site in about a day just by dropping in programs like these into a masterpage? I have a feeling that may be the direction people are going, but then again, that’s a great feature of .NET, too.  Mmmm, plug and play.

If you have used any of the software above, please let me know your reaction!

November 6, 2007 Posted by | AJAX, ASP.NET, Tips & Tricks | Leave a comment