How to upload asp.net Images
This article mainly explains "ASP.NET picture upload method tutorial," interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "ASP.NET picture upload method tutorial" bar!
First, upload the picture, the code is as follows:
xxx.aspx
The copy code is as follows:
xxx.aspx.cs
The copy code is as follows:
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = 0; i
< Request.Files.Count; i++) { HttpPostedFile file = Request.Files[i]; if (file.ContentLength >0)
{
if (file.ContentType.Contains("image/"))
{
using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream))
{
using (Graphics g = Graphics.FromImage(img))
{
g.DrawString("My Picture", new Font("Song", 14), Brushes.Red, 0, 0);
}
string FileName = System.IO.Path.GetFileName(file.FileName);
string[] SplitFileName = FileName.Split('. ');
string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss") + ". " + SplitFileName[1];
img.Save(Server.MapPath("/upload/" + AtterFileName));
this.Image2.ImageUrl = "upload/" + AtterFileName;
}
}
else
{
Response.Write("alert ('This file is not in picture format! ');");
}
}
else
{
Response.Write("alert ('Please select pictures to upload');");
}
}
}
Third, add the image watermark image upload, the code is as follows:
xxx.aspx
The copy code is as follows:
xxx.aspx.cs
The copy code is as follows:
protected void Button4_Click(object sender, EventArgs e)
{
for (int i = 0; i
< Request.Files.Count; i++) { HttpPostedFile file = Request.Files[i]; if (file.ContentLength >0)
{
if (file.ContentType.Contains("image/"))
{
using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream))
{
using (System.Drawing.Image imgThumb = new Bitmap(200, 100))
{
using (Graphics g = Graphics.FromImage(imgThumb))
{
g.DrawImage(img, new Rectangle(0, 0, imgThumb.Width, imgThumb.Height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
}
string fileName = file.FileName;
string[] SplitFileName = fileName.Split('. ');
string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss") + ". " + SplitFileName[1];
img.Save(Server.MapPath("/upload/" + AtterFileName));
this.Image4.ImageUrl = "upload/" + AtterFileName;
}
}
}
else
{
Response.Write("alert ('This file is not in picture format! ');");
}
}
else
{
Response.Write("alert ('Please select pictures to upload');");
}
}
}
At this point, I believe we have a deeper understanding of the "ASP.NET picture upload method tutorial," may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!