日期:2011-09-11  浏览次数:20373 次

程序代码
<html><title>如何在图片上写字</title>
<body>
  <asp:Label id="dis" runat=server/>
  <form enctype="multipart/form-data" runat=server ID="Form1">
  选择上传文件:<input id="UploadFile" type=file runat=server NAME="UploadFile">
  <asp:Button Text="Upload Me!" runat=server ID="Button1"/>
  <hr>
  <asp:Panel id="ImageEditor" Visible=false runat=server>
  <img ID="Image1" src="" runat="server"/>
  图象宽度:<asp:TextBox id="Width" runat="server"/>
  图象高度:<asp:TextBox id="Height" runat="server"/>
  文本标题:<asp:TextBox id="Caption" runat="server"/>
  标题字号:<asp:DropDownList id="FontSize" runat="server">
  <asp:ListItem>14</asp:ListItem>
      <asp:ListItem>18</asp:ListItem>
      <asp:ListItem>26</asp:ListItem>
      <asp:ListItem>36</asp:ListItem>
      <asp:ListItem>48</asp:ListItem>
      <asp:ListItem>62</asp:ListItem>
    </asp:DropDownList>
  标题字体:<asp:DropDownList id="FontType" runat="server">
   <asp:ListItem>黑体</asp:ListItem>
      <asp:ListItem>仿宋</asp:ListItem>
      <asp:ListItem>隶书</asp:ListItem>
      <asp:ListItem>楷书</asp:ListItem>
      <asp:ListItem>彩云</asp:ListItem>
      <asp:ListItem>新魏</asp:ListItem>
    </asp:DropDownList>
    <asp:button Text="Update Image" runat="server" ID="Button2"/>
  </asp:Panel>
</form>
</body>
</html> 

后台代码
说明:
1.加图片和加文字不能共存的就是你只能用一种
2.加图片的时候必须要保证你被加的水印的图片3.png在(可以自己改其他,程序中也要改)
必须有这个存放图片的文件夹,uploadfile(可以自己改其他,程序中也要改)
3.要改变文字,图片的位置必须在程序中改.

 程序代码
public void UploadBtn_Click(Object sender,EventArgs e) {
  String filename;
  String filename1;
  String[] filename2;
  int q;
  filename=UploadFile.PostedFile.FileName;
  filename2=filename.Split(new Char[] {'\\'});
  q=filename2.GetUpperBound(0);
  filename1=filename2[q];
  dis.Text="上传文件名:"+filename1+"<br>";
  UploadFile.PostedFile.SaveAs(Server.MapPath(filename1));
  ImageEditor.Visible = true;
  dis.Text+="文件大小:"+UploadFile.PostedFile.ContentLength+"字节数";
  Image1.Src=filename1;
  }
 
  void UpdateBtn_Click(Object sender, EventArgs e) {
  String filename1;
  filename1=Image1.Src;
 
  //加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
  // System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(filename1));
  //System.Drawing.Image newimage = new Bitmap(image.Width,image.Height,PixelFormat.Format32bppRgb);
  //Graphics g = Graphics.FromImage(newimage);
  //g.DrawImage(image,0,0,image.Width,image.Height);
  //Font f= new Font(FontType.SelectedItem.Text, Int32.Parse(FontSize.SelectedItem.Text));
  //Brush b = new SolidBrush(Color.AntiqueWhite);
  //g.DrawString(Caption.Text, f, b, 100, 140);
  //g.Dispose();
 
 
  //加图片水印
  System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(filename1));//原图
  System.Drawing.Image newimage = System.Drawing.Image.FromFile( Server.MapPath("3.png"));//此对象为被加的水印图
  Graphics g = Graphics.From