asp.net(C#)上传图片时添加文字水印在右下角上

如题,如何解决?... 如题,如何解决? 展开
 我来答
SEGA_Sakura4
2009-03-05 · TA获得超过569个赞
知道小有建树答主
回答量:367
采纳率:0%
帮助的人:449万
展开全部
try
{
string filepath = uploadFile.Value;//上传图片的路径
string newName = DateTime.Now.ToString("yyyyMMddHHmmss");//图片新名字
string filehz = filepath.Substring(filepath.LastIndexOf(".") + 1).ToLowerInvariant();//后缀名
string uploadpath = Server.MapPath("./images/update/" + newName + "." + filehz);//服务器保存图片路径

if (!(uploadFile.PostedFile.ContentLength > 0))
{
lblErrInfo.Text = "没有选择文件";
}
else
{
if (filehz == "jpg" || filehz == "gif" || filehz == "png")
{
if (File.Exists(uploadpath))
{
lblErrInfo.Text = "已经有同名文件";
}
else
{
if (chboxIsInfo.Checked == true)//加版权信息
{
if (txtLeft.Text.Trim() == "" || txtRight.Text.Trim() == "" || txtAddInfo.Text.Trim() == "")
{
Response.Write("<script>alert('请输入信息位置(半角数字)和信息文本!')</script>");
}
else
{
uploadFile.PostedFile.SaveAs(uploadpath);
lbtnDelImage.Visible = true;
btnUpdate.Enabled = false;
uploadFile.Visible = false;
txtGameImage.Text = "images/update/" + newName + "." + filehz;
System.Threading.Thread.Sleep(1000);
System.Drawing.Image img = System.Drawing.Image.FromFile(uploadpath);
lblImgWidth.Text = img.Width.ToString();
lblImgHeight.Text = img.Height.ToString();

int width, height, left, right;
width = Int32.Parse(lblImgWidth.Text);
height = Int32.Parse(lblImgHeight.Text);
left = Int32.Parse(txtLeft.Text.Trim());
right = Int32.Parse(txtRight.Text.Trim());
System.Threading.Thread.Sleep(1000);

//添加信息
string file = Server.MapPath("./" + txtGameImage.Text);
string newfile = "images/update/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
string strAddInfo = txtAddInfo.Text;
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(file);
Response.Clear();
Bitmap output = new Bitmap(oldimage);
Graphics gh = Graphics.FromImage(output);

string ColorHex = SelColor.Value;
int r = Int16.Parse(ColorHex.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
int g = Int16.Parse(ColorHex.Substring(2, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
int b = Int16.Parse(ColorHex.Substring(4, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
Color NewColor = Color.FromArgb(r, g, b);

gh.DrawString(strAddInfo, new Font(ddlFont.SelectedValue, int.Parse(ddlFontSize.SelectedValue)), new SolidBrush(NewColor), left, right);
output.Save(Server.MapPath(newfile), System.Drawing.Imaging.ImageFormat.Jpeg);

Response.ContentType = "image/gif";
ImgPreview.ImageUrl = newfile;
ImgPreview.Visible = true;
oldimage.Dispose();
txtGameImage.Text = newfile;
img.Dispose();
File.Delete(file);
}
}
else//不加版权信息
{
uploadFile.PostedFile.SaveAs(uploadpath);
lbtnDelImage.Visible = true;
btnUpdate.Enabled = false;
uploadFile.Visible = false;
txtGameImage.Text = "images/update/" + newName + "." + filehz;
System.Threading.Thread.Sleep(1000);
System.Drawing.Image img = System.Drawing.Image.FromFile(uploadpath);
lblImgWidth.Text = img.Width.ToString();
lblImgHeight.Text = img.Height.ToString();
Response.ContentType = "image/gif";
ImgPreview.ImageUrl = txtGameImage.Text;
ImgPreview.Visible = true;
img.Dispose();
}
SizeToSize();
}
}
else
{
Response.Write("<script>alert('只能上传jpg|gif|png格式的图片!')</script>");
}
}
}
catch
{
Response.Write("<script>alert('上传图片出错!')</script>");
}
以前写的上传代码
left, right分别代表版权信息的X,Y轴,这里是自己输入的,你可以通过对你输入的版权信息的字符像素长度和图片的宽,高计算得到。
ddlFont.SelectedValue 字体选择
int.Parse(ddlFontSize.SelectedValue)) 字体大小
new SolidBrush(NewColor) 字体颜色
百度网友94f02eb
2009-03-06 · TA获得超过8613个赞
知道大有可为答主
回答量:7955
采纳率:74%
帮助的人:4548万
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式