生成縮略圖及生成水印圖,太帥了.
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;