程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 生成縮略圖及生成水印圖

生成縮略圖及生成水印圖

編輯:.NET實例教程

生成縮略圖及生成水印圖,太帥了.

  private void Button1_Click(object sender, System.EventArgs e)
  {
   string namestr = Path.GetFileName(this.File1.PostedFile.FileName);//提取文件名  
   this.File1.PostedFile.SaveAs(Server.MapPath(".") + @"\" + namestr);

   System.Drawing.Image image, aNewImage;
   image = System.Drawing.Image.FromStream(this.File1.PostedFile.InputStream);
   decimal width = image.Width;
   decimal height = image.Height;
   int newwidth, newheight;
   if (width > height)
   {
    newwidth = 100;
    newheight = (int)(height / width * 100);
   }
   else
   {
    newheight = 100;
    newwidth = (int)(width / height * 100);
   }
   aNewImage = image.GetThumbnailImage(newwidth, newheight, null, IntPtr.Zero);
   Bitmap output = new Bitmap(aNewImage);
   Graphics g = Graphics.FromImage(output);

   //g.DrawString(TextBox1.Text.Trim(), new Font("CourIEr New", 9), new SolidBrush(Color.Red), 60, 60);//寫版權信息及文本格式及位置
   output.Save(Server.MapPath(".") + @"\s_" + namestr, System.Drawing.Imaging.ImageFormat.Jpeg);
  }

  private void Button2_Click(object sender, System.EventArgs e)
  {
   string extension = Path.GetExtension(File1.PostedFile.FileName).ToUpper();
   string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");

   string path = Server.MapPath(".") + "/UploadFile/" + fileName + extension;
   File1.PostedFile.SaveAs(path);
   System.Drawing.Image image = System.Drawing.Image.FromFile(path);
   System.Drawing.Image copyImage = System.Drawing.Image.FromFile( Server.MapPath(".") + "/logo.gif");
&nb

sp;  //Create a new FrameDimension object from this image

   FrameDimension ImgFrmDim = new FrameDimension( image.FrameDimensionsList[0] );

   int nFrameCount = image.GetFrameCount( ImgFrmDim );

   // Save every frame into jpeg format

   for( int i = 0; i < nFrameCount; i++ )
   {
    image.SelectActiveFrame( ImgFrmDim, i );

    image.Save( string.Format( Server.MapPath(".") + "/UploadFile/Frame{0}.jpg", i ), ImageFormat.Jpeg );
   }
   image.Dispose();

   for( int i = 0; i < nFrameCount; i++ )
   {
    string pa = Server.MapPath(".") + "/UploadFile/Frame"+i+".jpg";
    System.Drawing.Image Image = System.Drawing.Image.FromFile(pa);
    Graphics g = Graphics.FromImage(Image);
    g.DrawImage(copyImage, new Rectangle(Image.Width-copyImage.Width, Image.Height-copyImage.Height, 

     copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
    g.Dispose();

    string newPath = Server.MapPath(".") + "/UploadFile/" + fileName + "_new"+i.ToString()+ extension;
    Image.Save(newPath);

    Image.Dispose();

    if(File.Exists(pa))
    {
     File.Delete(pa);
    }
   }

   if(File.Exists(path))
   {
    File.Delete(path);
   }

  }  

需要引入命名空間:

using System.Drawing.Imaging;
using System.IO;


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved