Random Programming

Friday, September 15, 2006

Resizing an image using asp.net

This piece of code can resize an image according to the size we need...
We need to import system.drawing namespace and since images are displayed in the browser as binary data, the need for using BinaryWrite at the end.
If you dont know what type of extension image you are displaying, i would assume you would need additional steps to extract the type of image and maybe use case to sort it out.
***resizeImage.GetThumbnailImage(10, 10, dummyCallBack, New System.IntPtr()) is the key function thats responsible for reducing the imagesize.
 Public Function GetThumbnailImage( _
ByVal thumbWidth As Integer, _
ByVal thumbHeight As Integer, _
ByVal callback As Image.GetThumbnailImageAbort, _
ByVal callbackData As IntPtr _
) As Image

The thumbWidth and thumbHeight methods specify the width and height of the resulting thumbnail image. For the third parameter, callback, you need to provide a delegate. To supply the delegate, you must provide in your ASP.NET Web page a function of the following form:

Public Function FunctionName as Boolean
Return False
End Function
In the example below, dummyCallBack is the function that returns false...

Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbort
dummyCallBack = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
Dim mstream As System.IO.MemoryStream = New System.IO.MemoryStream(byteArray, 0, byteArray.Length)
Dim resizeImage As System.Drawing.Image = System.Drawing.Image.FromStream(New System.IO.MemoryStream(byteArray))
Dim thumbNailImage As System.Drawing.Image = resizeImage.GetThumbnailImage(10, 10, dummyCallBack, New System.IntPtr())
thumbNailImage.Save(mstream,resizeImage.RawFormat)
Dim thumbNailByteArray() As Byte = New Byte(mstream.Length) {}
mstream.Position = 0
mstream.Read(thumbNailByteArray, 0, Convert.ToInt32(mstream.Length))
Response.Clear()
Response.ContentType = "Image/jpeg"
Response.BinaryWrite(thumbNailByteArray)


Courtesy..www.4GuysFromRolla.com

Friday, September 08, 2006

Looping Through Controls in ASP.NET

As I was trying to browse through all the controls in my form in ASP.NET, I found that there could be some controls inside another control and hence just looping through Page.Controls is not sufficient. This is particularly true if I have to loop through all the label controls which could be present in another control.
Hence the need to have a recursive function which will go through all the controls and add the specific type of control that we want and put them in an array list. The function below implements the same logic.
In the example below, the control cc is a child of c and ccc is a child of cc.

Function GetControls(ByVal c As Control, ByVal controlType As System.Type) As ArrayList
Dim result As New ArrayList
Dim temp As New ArrayList
If (Not c Is Nothing AndAlso Not c.Controls Is Nothing AndAlso c.Controls.Count > 0) Then
For Each cc As Control In c.Controls
'If the control cc has sub controls.. then do those first
If (Not cc Is Nothing AndAlso Not cc.Controls Is Nothing AndAlso cc.Controls.Count > 0) Then
'recursion here
temp = GetControls(cc, controlType)
'Copy temp results into main results array
If (Not temp Is Nothing AndAlso temp.Count > 0) Then
For Each ccc As Control In temp
result.Add(ccc)
Next
End If
End If
'Here we check to see if the type matches, and then add it to the results
If (controlType.Equals(cc.GetType())) Then
'Add it to the results
result.Add(cc)
End If
Next
End If
Return result
End Function

Using the above function if i have to find out all the labels in my form, call the function by passing the Label.

Dim labels As ArrayList = GetControls(Page, GetType(System.Web.UI.WebControls.Label))
If (Not labels Is Nothing AndAlso labels.Count > 0) Then
For Each l As Label In labels
Response.Write(l.Text)
Next
End If