asp.net
程式碼
<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.FromImage(image);
g.DrawImage(newimage, new Rectangle(image.Width-newimage.Width, image.Height-newimage.Height, newimage.Width, newimage.Height), 0, 0, newimage.Width, newimage.Height, GraphicsUnit.Pixel);
g.Dispose();
//加文字浮水印
//System.Drawing.Image thumbImage = newimage.GetThumbnailImage(image.Width,image.Height,null,new IntPtr());
//image.Dispose();
//thumbImage.Save(Server.MapPath(filename1), ImageFormat.Jpeg);
//加圖片浮水印
image.Save(Server.MapPath("UploadFile/"+filename1));
Image1.Src = filename1;
Caption.Text="";
}