Friday, December 31, 2010

Showing the size of an image after it has been uploaded using AJAX AsyncFileUpload file control

On Wednesday, I was working on some code that will accept an upload of an image file and after the image upload is complete, the page will display the dimensions of the image that was uploaded.

I had problems getting my original code to work though. I getting this error "The process cannot access the file because it is being used by another process". Below is my original code.

AsyncFileUpload upload = (AsyncFileUpload)sender;
   String filename = System.IO.Path.GetFileName(upload.FileName);
   String filePathName = Server.MapPath("images/") + filename;
   upload.SaveAs(filePathName);

   System.Drawing.Image image = System.Drawing.Image.FromFile(filePathName);
  
   if (image != null){            
     String msg = "Uploaded " + filename + " of size " + 
                  image.Width + "x" + image.Height;
     Label1.Text = msg;
}

After numerous trials and errors and after alot of Googling, I finally figured out that the problem was due to a GDI+ bug that locks up the Image handle.

The thread above suggested some workarounds for the problem, but I didn't try them, instead, I thought since I was encountering the lock problem because one process was trying to save the File, while another process was trying to read the file, maybe I can try to read the file by creating an Image from Stream instead of disk instead.

Sure enough, after I managed to find out how to create an Image object from Stream and modified my code accordingly, the thing worked! ^_^

Anyway, the modified code only needed one change, that is to change the line of code as highlighted below:

protected void AsyncFileUpload1_UploadedComplete(object sender, AsyncFileUploadEventArgs e)
    {
        AsyncFileUpload upload = (AsyncFileUpload)sender;
        String filename = System.IO.Path.GetFileName(upload.FileName);

        String filePathName = Server.MapPath("images/") + filename;
        upload.SaveAs(filePathName);


        System.Drawing.Image image = System.Drawing.Image.FromStream(upload.FileContent);

        if (image != null)
        {            
            String msg = "Uploaded " + filename 
                         + " of size " + image.Width + "x" + image.Height;
            Label1.Text = msg;
        }
    }

Tuesday, December 28, 2010

AJAX Control Toolkit AsyncFileUpload control

I was using AJAX Control Toolkit AsyncFileUpload control just now and hit an error "The file attached is invalid".

Fortunately, the error was easy to solve, just needed to add the following attribute
on the Async Upload Control

clientIdMode="AutoID"

Got the tip from the link below ^_^
ASP Net Forums

Using AJAX Control Toolkit

I wanted to use the AJAX Control Toolkit ComboBox in one of my webforms today.

This link is useful if you don't know how to add the AJAX Control Toolkit to your normal ASP.NET toolbox.


This link is useful if you want a 10 second guide on inserting a combo box.

Pre-populate ReadOnly TextBox in a FormView with current date and time

I had to pre-populate a FormView that contains a textbox with the current date and time.

Googled and found this link which proved to be quite useful, though not exactly what I wanted.

Pre-populate DetailsView InsertItemTemplate TextBox with User.Identity.Name value - ASP.NET Forums

**

Anyway, what you need to do is to first add an event handler for "ondatabound" in the FormView.
Assuming you have a textbox named "TextBoxDataOfEntry" in the InsertItemTemplate of the FormView, this is how I got the current date and time to be populated in the textbox.

protected void FormView1_DataBound(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Insert)
{
DateTime datetime = DateTime.Today;
TextBox tbDateOfEntry = (TextBox)FormView1.FindControl("TextBoxDateOfEntry");
tbDateOfEntry.Text = datetime.ToString();
}
}//end FormView1_DataBound

Tuesday, December 21, 2010

Sorting a GridView

http://geekswithblogs.net/azamsharp/archive/2006/04/27/76612.aspx

This code from Azam Sharp works well ^_^


private const string ASCENDING = " ASC";
 
private const string DESCENDING = " DESC";
 public SortDirection GridViewSortDirection
    {
        
get
        
{
            
if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;

            
return (SortDirection) ViewState["sortDirection"];               
        }
        
set { ViewState["sortDirection"] = value; }
    }


    
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        
string sortExpression = e.SortExpression;

        
if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, DESCENDING);
        }
        
else
        
{
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, ASCENDING);
        }  
      
    }

    
private void SortGridView(string sortExpression,string direction)
    {
        
//  You can cache the DataTable for improving performance
        
DataTable dt = GetData().Tables[0];

        DataView dv = 
new DataView(dt);
        dv.Sort = sortExpression + direction;        

        GridView1.DataSource = dv;
        GridView1.DataBind();        
    }

Monday, November 22, 2010

Get the resolution of a jpeg image using C# and the .NET Environment

Source: http://stackoverflow.com/questions/123838/get-the-resolution-of-a-jpeg-image-using-c-and-the-net-environment

Using System.Drawing.Image;

It depends what you are looking for... if you want the DPI of the image then you are looking for the HorizontalResolution which is the DPI of the image.

Image i = Image.FromFile(@"fileName.jpg");
i.HorizontalResolution;

If you want to figure out how large the image is then you need to calculate the measurements of the image which is:

int docHeight = (i.Height / i.VerticalResolution);
int docWidth = (i.Width / i.HorizontalResolution);

This will give you the document height and width in inches which you could then compare to the min size needed.