有时候我们在上传完一些自己的特殊图片后,想在别人传载的时候,可以表显出出自何方。这时我们就要在图片上加入一些背景字。下面给出我的代码
前台:
后台:
试一下效果,你可以根据你的需求,来改变字的大小与色彩
前台:
<
div
>
< asp:Image ID ="Image1" runat ="server" Height ="305px" Width ="312px" />
< asp:FileUpload ID ="FileUpload1" runat ="server" Width ="243px" />
< asp:Button ID ="Button1" runat ="server" onclick ="Button1_Click" Text ="上传图片" />
</ div >
< asp:Image ID ="Image1" runat ="server" Height ="305px" Width ="312px" />
< asp:FileUpload ID ="FileUpload1" runat ="server" Width ="243px" />
< asp:Button ID ="Button1" runat ="server" onclick ="Button1_Click" Text ="上传图片" />
</ div >
后台:
protected
void
Button1_Click(
object
sender, EventArgs e)
{
string FileName;
string FileName1;
string FilePath;
FileName = this.FileUpload1.PostedFile.FileName;
FileName1 = FileName.Substring(FileName.LastIndexOf("/")+1);
FilePath = Request.PhysicalApplicationPath + "images/";
this.FileUpload1.PostedFile.SaveAs(FilePath + FileName1);
System.Drawing.Image image1 = System.Drawing.Image.FromFile(Server.MapPath("/images/" + FileName1));
System.Drawing.Image NewImage = new Bitmap(image1.Width, image1.Height, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(NewImage);
g.DrawImage(image1, 0, 0, image1.Width, image1.Height);
Font f = new Font("楷书", int.Parse("28"));
Brush b = new SolidBrush(Color.Red);
g.DrawString("无名背景字", f, b, 10, 140);
g.Dispose();
System.Drawing.Image thubimages = NewImage.GetThumbnailImage(500, 500, null,System.IntPtr.Zero);
image1.Dispose();
thubimages.Save(FilePath + FileName1, ImageFormat.Jpeg);
this.Image1.ImageUrl = "/images/" + FileName1;
}
{
string FileName;
string FileName1;
string FilePath;
FileName = this.FileUpload1.PostedFile.FileName;
FileName1 = FileName.Substring(FileName.LastIndexOf("/")+1);
FilePath = Request.PhysicalApplicationPath + "images/";
this.FileUpload1.PostedFile.SaveAs(FilePath + FileName1);
System.Drawing.Image image1 = System.Drawing.Image.FromFile(Server.MapPath("/images/" + FileName1));
System.Drawing.Image NewImage = new Bitmap(image1.Width, image1.Height, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(NewImage);
g.DrawImage(image1, 0, 0, image1.Width, image1.Height);
Font f = new Font("楷书", int.Parse("28"));
Brush b = new SolidBrush(Color.Red);
g.DrawString("无名背景字", f, b, 10, 140);
g.Dispose();
System.Drawing.Image thubimages = NewImage.GetThumbnailImage(500, 500, null,System.IntPtr.Zero);
image1.Dispose();
thubimages.Save(FilePath + FileName1, ImageFormat.Jpeg);
this.Image1.ImageUrl = "/images/" + FileName1;
}