// 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 ();
}