Generate a thumbnail in asp.net and add the copyright instance code

Source: Internet
Author: User

Copy codeThe Code is as follows:
// Define the object of the image class
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 sm_Click (object sender, System. EventArgs e)
{
String mPath;

If (""! = File1.PostedFile. FileName) // File1 is the file upload control.
{
ImagePath = File1.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 ("UploadFiles ");
// Save to the virtual path
File1.PostedFile. SaveAs (mPath + "\" + imageName );

// Display the source image. imageSource is the image control.
// ImageSource. ImageUrl = "UploadFiles/" + imageName;

// Create a reference for the uploaded Image
Image = System. Drawing. Image. FromFile (mPath + "\" + imageName );
// Generate a thumbnail
Newimage = image. GetThumbnailImage (200,200, callb, new System. IntPtr ());
// Save the thumbnail to the specified virtual path
Newimage. Save (Server. MapPath ("UploadFiles") + "\ small" + imageName );
// Release the resources occupied by the image object
Image. Dispose ();
// Release the resource of the newimage object
Newimage. Dispose ();
// Display the thumbnail

AddTextToImg ("UploadFiles/" + "small" + imageName, "Pic Info"); // Add information to the image
Image1.ImageUrl = "UploadFiles/" + "small" + imageName;

Script. Alert ("Upload successful! ");
}
Catch
{
Script. Alert ("Upload Failed! ");
}

} // End else
}

// Add your own information to the image,
// AddTextToImg (physicPath, "Pic Info ");
Private void AddTextToImg (string fileName, string text)
{
// String sss = MapPath (fileName );

If (! File. Exists (fileName )){
Throw new FileNotFoundException ("The file don't exist! ");
}

// You also need to determine whether the file type is of the image type. I will not go into details here.

System. Drawing. Image image = System. Drawing. Image. FromFile (fileName); // MapPath (fileName ));
Bitmap bitmap = new Bitmap (image, image. Width, image. Height );
Graphics g = Graphics. FromImage (bitmap );

Float fontSize = 22.0f; // font size
Float textWidth = text. Length * fontSize; // the Length of the text.
// The following defines a rectangle area, and then draws black characters on the rectangle.
Float rectX = 0;
Float rectY = 0;
Float rectWidth = text. Length * (fontSize + 18 );
Float rectHeight = fontSize + 18;
// Declare the rectangular Field
RectangleF textArea = new RectangleF (rectX, rectY, rectWidth, rectHeight );
Font font = new Font ("", fontSize); // define the Font
Brush whiteBrush = new SolidBrush (Color. White );
Brush blackBrush = new SolidBrush (Color. Black );
G. FillRectangle (blackBrush, rectX, rectY, rectWidth, rectHeight );
G. DrawString (text, font, whiteBrush, textArea );
MemoryStream MS = new MemoryStream ();
// Save as Jpg
Bitmap. Save (MS, ImageFormat. Jpeg );

// Output the processed image. For demonstration convenience, I will display the image on the page
/** // * Response. Clear ();
Response. ContentType = "image/jpeg ";
Response. BinaryWrite (ms. ToArray ());
*/
FileStream fs = new FileStream (fileName, FileMode. OpenOrCreate); //. CreateNew );
Fs. Write (ms. ToArray (), 0, ms. ToArray (). Length );
Fs. Close ();

Image1.ImageUrl = fileName; // display the Image in the Image control
G. Dispose ();
Bitmap. Dispose ();
Image. Dispose ();
}

Related Article

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.