{
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) 字体颜色
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)