asp.net in the article thumbnail and add picture information

Source: Internet
Author: User
asp.net| thumbnail image

asp.net in the article thumbnail, and in the picture to add a picture plate and other information


Object defining the Image class
Drawing.image Image,newimage;
Picture path
protected string ImagePath;
Picture type
protected string imagetype;
Picture name
protected string imagename;

Provides a callback method that determines when an image object cancels execution prematurely when performing a build thumbnail operation
Returns True if this method determines that the Getthumbnailimage method should stop in advance;
System.Drawing.Image.GetThumbnailImageAbort callb = null;

private void Sm_click (object sender, System.EventArgs e)
{
String MPath;

if (""!= File1.PostedFile.FileName)//file1 for upload file control
{
ImagePath = File1.PostedFile.FileName;
Get Picture Type
Imagetype= imagepath.substring (Imagepath.lastindexof (".") +1);
Get Picture Name
ImageName = imagepath.substring (Imagepath.lastindexof ("\") +1);
To determine if it is a JPG or GIF picture, here is just an example, not necessarily the two pictures
if ("JPG"!= imagetype && "gif"!= imagetype)
{
Response.Write (' <script language= ' JavaScript ' > alert (' Sorry! Please choose a JPG or GIF format picture! ');</script> ");
Return
}
Else
{
Try
{
Create a virtual path
Mpath=server.mappath ("Uploadfiles");
Save to Virtual path
File1.PostedFile.SaveAs (mpath+ "\" +imagename);

Display artwork, imagesource as Picture control
Imagesource.imageurl = "uploadfiles/" +imagename;

Create a reference for an uploaded picture
Image=system.drawing.image.fromfile (mpath+ "\" +imagename);
Generate thumbnails
Newimage=image. Getthumbnailimage (200,200,callb,new system.intptr ());
Save thumbnails to the specified virtual path
NewImage. Save (Server.MapPath ("uploadfiles") + "\\small" +imagename);
Frees the resources occupied by the image object
Image. Dispose ();
Freeing Resources for NewImage objects
NewImage. Dispose ();
Show thumbnails

Addtexttoimg ("uploadfiles/" + "small" +imagename, "Pic Info"); Add an information description to the picture
Image1.imageurl = "uploadfiles/" + "small" +imagename;

Script.alert ("Upload success!");
}
Catch
{
Script.alert ("Upload failed!");
}

}//End Else
}

Add your own information to the picture,
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 an image type, so here's the

   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
  //below defines a rectangular area, after which the white background black word is painted in the rectangle
   float rectx = 0;
   float recty = 0;
   Float rectwidth = text. length* (fontsize+18);
   Float rectheight = fontsize+18;
  //declaring rectangular fields
   rectanglef textArea = new RectangleF (rectx,recty,rectwidth,rectheight);
   font font = new Font ("XXFarEastFont-Arial", fontsize);//define Font
   Brush whitebrush = new SolidBrush (color.white); br>   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 type
   bitmap. Save (ms,imageformat.jpeg);

Output processing image, here for demonstration convenience, I will display the picture in 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 a picture in an image control
G.dispose ();
Bitmap. Dispose ();
Image. Dispose ();
}




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.