asp. net generates thumbnails and adds copyright instance code

  • 2020-07-21 07:31:31
  • OfStack


// define image The object of the class 
Drawing.Image image,newimage;
// Image path 
protected string imagePath;
// Image type 
protected string imageType;
// Image name 
protected string imageName;
// provide 1 A callback method , Used to determine the Image Object when it performs a thumbnail generation operation 
// If this method determines  GetThumbnailImage  Method should stop execution before it returns  true ; Otherwise returns  false
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 image type 
imageType= imagePath.Substring(imagePath.LastIndexOf(".")+1);
// Get the image name 
imageName = imagePath.Substring(imagePath.LastIndexOf("\\")+1);
// Judge whether or not JPG or GIF The picture , This is just an example , Don't 1 It must be these two kinds of pictures 
if("jpg" != imageType && "gif" != imageType)
{
Response.Write("<script language='javascript'> alert(' I'm sorry! Please you to choose jpg or gif Format the picture! ');</script>");
return;
}
else
{
try
{
// Create a virtual path 
mPath=Server.MapPath("UploadFiles");
// Save to the virtual path 
File1.PostedFile.SaveAs(mPath+"\\"+imageName);
// According to the original , imageSource For picture control 
//imageSource.ImageUrl = "UploadFiles/"+imageName;
// Create a reference for the uploaded image 
image=System.Drawing.Image.FromFile(mPath+"\\"+imageName); 
// Generate thumbnails 
newimage=image.GetThumbnailImage(200,200,callb,new System.IntPtr());
// Saves the thumbnail to the specified virtual path 
newimage.Save(Server.MapPath("UploadFiles")+"\\small"+imageName);
// The release of image The resource occupied by the object 
image.Dispose();
// The release of newimage Object resources 
newimage.Dispose();
// Display thumbnail 
AddTextToImg ("UploadFiles/"+"small"+imageName,"Pic Info"); //  Add information to the picture 
Image1.ImageUrl = "UploadFiles/"+"small"+imageName;
Script.Alert(" Uploaded successfully !");
}
catch
{
Script.Alert(" Upload failed !");
}
} // end else
}
//  Add your own message 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, and I won't 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; // The font size 
float textWidth = text.Length*fontSize; // Length of text  
// The following definitions 1 Rectangle area, and then draw black words on a white background in the rectangle 
float rectX = 0;
float rectY = 0;
float rectWidth = text.Length*(fontSize+18);
float rectHeight = fontSize+18;
// Declare rectangular domain 
RectangleF textArea = new RectangleF(rectX,rectY,rectWidth,rectHeight);
Font font = new Font(" Song typeface ",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 type 
bitmap.Save(ms,ImageFormat.Jpeg);
// Output the processed image, here for demonstration convenience, I will display the image 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 the picture in Image In the control 
g.Dispose();
bitmap.Dispose();
image.Dispose();
}


Related articles: