Friday, September 25, 2009

Get image height and width without creating bitmap

By HAFIZ Kashkoush

Usually to resize an image we upload the image ,create bitmap ,resize it
and save the new resized image.
I can use this technique in case that I need a new resized image file otherwise
this technique become very complicated .

In this article I going to
1:-Upload an image to Image control (image1)
2:-Get image info (width and height)
3:-When user click resize button the image will be resized for 50%
of it original size.

First i add to my form 2 controls
A:-Image control -Image ID="Image1"
B:-Button control to resize image - Button ID="BResize"

2:- In my code I declare 3 global variables
Dim ImageWidth As Integer 'Image width
Dim ImageHeight As Integer 'Image height
Dim imageurl As String 'Image URL

3:- In my page load event I load the image to image1 control and get image info.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
'************************************************************************
'******* This string contain image URL
'******* My image name is "1_o.jpg" and it located in my application
'******* directory in this case the image URL="1_o.jpg"

'************************************************************************
imageurl = Server.MapPath("1_o.jpg")
'******** Load the image into image1 control
Image1.ImageUrl = imageurl
'******** Get Image Info
Dim fullSizeImg As System.Drawing.Image = System.Drawing.Image.FromFile(imageurl)
'******** Image Width
ImageWidth = fullSizeImg.Width
'******** Image Height
ImageHeight = fullSizeImg.Height
End Sub

4:-In the button resize click event I resize the image to 50% of it original size
by changing image1 width an height values.
Protected Sub BResize_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Image1.Height = Int(ImageHeight / 2)
Image1.Width = Int(ImageWidth / 2)
End Sub

No comments:

Post a Comment