来自aspsnippets.com的Tutrial:http://www.aspsnippets.com/Articles/Create-Add-Watermark-Text-to-Images-Photo-in-ASPNet-using-C-and-VBNet.aspx
包含代码:
protected void Upload(object sender, EventArgs e)
{
string watermarkText = "© ASPSnippets.com";
//Get the file name.
string fileName = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName) + ".png";
//Read the File into a Bitmap.
using (Bitmap bmp = new Bitmap(FileUpload1.PostedFile.InputStream, false))
{
using (Graphics grp = Graphics.FromImage(bmp))
{
//Set the Color of the Watermark text.
Brush brush = new SolidBrush(Color.Red);
//Set the Font and its size.
Font font = new System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel);
//Determine the size of the Watermark text.
SizeF textSize = new SizeF();
textSize = grp.MeasureString(watermarkText, font);
//Position the text and draw it on the image.
Point position = new Point((bmp.Width - ((int)textSize.Width + 10)), (bmp.Height - ((int)textSize.Height + 10)));
grp.DrawString(watermarkText, font, brush, position);
using (MemoryStream memoryStream = new MemoryStream())
{
//Save the Watermarked image to the MemoryStream.
bmp.Save(memoryStream, ImageFormat.Png);
memoryStream.Position = 0;
//Start file download.
Response.Clear();
Response.ContentType = "image/png";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
//Write the MemoryStream to the Response.
memoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.Close();
Response.End();
}
}
}
}
帮助我们下载带水印的文件,但是我想上传到项目网站的文件夹中。抱歉,这是我第一次使用内存流,所以我不知道如何上传。我想我必须改变:
bmp.Save(memoryStream, ImageFormat.Png);
memoryStream.Position = 0;
//Start file download.
Response.Clear();
Response.ContentType = "image/png";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
//Write the MemoryStream to the Response.
memoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.Close();
Response.End();
但我不知道怎么做,请帮助我。谢谢!!!
答案 0 :(得分:2)
protected void UploadFile_Click(object sender, EventArgs e)
{
using (MemoryStream memStream = new MemoryStream(UploadFile.FileBytes))
{
using (FileStream fstream = new FileStream(@"C:/UploadFolder/" +
UploadFile.FileName, FileMode.Create))
{
memStream .WriteTo(fstream);
}
}
}