In. NET development, you need to get the height and width of the image. The Code is as follows:
Protected string imgpath;
Protected string fileextname;
Private void btnget_click (Object sender, system. eventargs E)
...{
If (upimage. postedfile. filename! = "")
...{
Imgpath = upimage. postedfile. filename;
Fileextname = imgpath. substring (imgpath. lastindexof (".") + 1, 3 );
If (fileextname! = "GIF" & fileextname! = "Jpg ")
...{
Response. Write ("Select images in GIF and jpg Formats ");
}
Else
...{
System. Drawing. Image image = system. Drawing. image. fromfile (imgpath );
Txtheight. Text = image. Height. tostring ();
Txtwidth. Text = image. Width. tostring ();
}
}
Else
...{
Response. Write ("select an image! ");
}
}
How to generate a thumbnail of an image
// Define the object of the image class
System. Drawing. Image image, newimage;
// Image path
Protected string ImagePath;
// Image type
Protected string imagetype;
// Image name
Protected string imagename;
// Provides a callback method to determine when the image object will be canceled before the thumbnail generation operation is executed.
// If this method determines that the getthumbnailimage method should stop execution in advance, true is returned; otherwise, false is returned.
System. Drawing. image. getthumbnailimageabort callb = NULL;
Private void btnup_click (Object sender, system. eventargs E)
...{
String mpath;
If (""! = Upimage. postedfile. filename)
...{
ImagePath = upimage. postedfile. filename;
// Obtain the image type
Imagetype = ImagePath. substring (ImagePath. lastindexof (".") + 1 );
// Obtain the image name
Imagename = ImagePath. substring (ImagePath. lastindexof ("/") + 1 );
// Determine whether the image is a jpg or GIF image. Here is only an example. It is not necessarily a jpg or GIF image.
If ("jpg "! = Imagetype & "GIF "! = Imagetype)
...{
Response. Write ("<script language = 'javascript '> alert ('Sorry! Select a jpg or GIF image! '); </SCRIPT> ");
Return;
}
Else
...{
Try
...{
// Create a virtual path
Mpath = server. mappath ("upfile ");
// Save to the virtual path
Upimage. postedfile. saveas (mpath + "/" + imagename );
// Display the source Image
Imagesource. imageurl = "upfile/" + imagename;
// Create a reference for the uploaded Image
Image = system. Drawing. image. fromfile (mpath + "/" + imagename );
// Generate a thumbnail
Newimage = image. getthumbnailimage (100,100, callb, new system. intptr ());
// Save the thumbnail to the specified virtual path
Newimage. Save (server. mappath ("upfile") + "/small" + imagename );
// Release the resources occupied by the image object
Image. Dispose ();
// Release the resource of the newimage object
Newimage. Dispose ();
// Display the thumbnail
Imagesmall. imageurl = "upfile/" + "small" + imagename;
Response. Write ("Upload successful! ");
}
Catch
...{
Response. Write ("Upload successful! ");
}
}
}