In. net, how does one obtain the image height and width to generate a thumbnail of the image?

Source: Internet
Author: User

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! ");
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.