图片加上水印的代码能不能告诉我,
(C#)给图片加上水印效果
private void Btn_Upload_Click(object sender, System。EventArgs e)
{
if(UploadFile。 PostedFile。FileName。Trim()!="")
{
//上传文件
string extension = Path。 GetExtension(UploadFile。PostedFile。FileName)。ToUpper();
string fileName = DateTime。 Now。Year。ToString() + DateTime。Now。Month。ToStrin...全部
(C#)给图片加上水印效果
private void Btn_Upload_Click(object sender, System。EventArgs e)
{
if(UploadFile。
PostedFile。FileName。Trim()!="")
{
//上传文件
string extension = Path。
GetExtension(UploadFile。PostedFile。FileName)。ToUpper();
string fileName = DateTime。
Now。Year。ToString() + DateTime。Now。Month。ToString() + DateTime。Now。Day。ToString() + DateTime。Now。
Hour。ToString() + DateTime。Now。Minute。ToString() + DateTime。Now。Second。ToString();
string path = Server。
MapPath("。") + "/UploadFile/" + fileName + extension;
UploadFile。PostedFile。SaveAs(path);
//加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
System。
Drawing。Image image = System。Drawing。Image。FromFile(path);
Graphics g = Graphics。
FromImage(image);
g。DrawImage(image, 0, 0, image。Width, image。Height);
Font f = new Font("Verdana", 32);
Brush b = new SolidBrush(Color。
White);
string addText = AddText。Value。Trim();
g。DrawString(addText, f, b, 10, 10);
g。
Dispose();
//加图片水印
System。Drawing。Image image = System。Drawing。
Image。FromFile(path);
System。Drawing。Image copyImage = System。Drawing。Image。FromFile( Server。
MapPath("。") + "/ f");
Graphics g = Graphics。FromImage(image);
g。
DrawImage(copyImage, new Rectangle(image。Width-copyImage。Width, image。Height-copyImage。Height, copyImage。
Width, copyImage。Height), 0, 0, copyImage。Width, copyImage。Height, GraphicsUnit。Pixel);
g。
Dispose();
//保存加水印过后的图片,删除原始图片
string newPath = Server。MapPath("。
") + "/UploadFile/" + fileName + "_new" + extension;
image。Save(newPath);
image。
Dispose();
if(File。Exists(path))
{
File。Delete(path);
}
Response。
Redirect(newPath);
}
}
-----------------------------------------------------
PHP给图片加上水印效果
limit = 500;//长宽限制
$image->wm_text=" ";//水印文字
$image->wm_fontfile="font/ f";//字体文件
$image->wm_color="00cccc";
//$image->save_file = " g";//保存到xx文件
$image->create(" g");//从xx文件创建
/*
+--------------------------------------------------------------------------
| 生成缩略图&加水印的图片类
| ============================
| by Dash
| - 共享互联网
| ============================
| 网址:
| 邮件:Dash@
+--------------------------------------------------------------------------
*/
Class Gimage{
var $input_type = ""; //输入图片的格式
var $output_type = ""; //输出图片的格式
var $limit = 0; //图片大小限制
var $filename = ""; //输入图片的文件名(也可以直接是图片数据)
var $jpeg_quality = 90; //jpeg图片质量
var $save_file = ''; //输出文件名
var $wm_text = ""; //水印文字( 不支持中文:'( )
var $wm_size = 50; //水印文字大小
var $wm_angle = 0; //水印文字角度
var $wm_x = 50; //水印x坐标
var $wm_y = 50; //水印y坐标
var $wm_color = "#cccccc"; //水印颜色
var $wm_fontfile = " f";//水印字体文件
function create($filename="")
{
if ($filename) $this->filename = $filename;
if (!$this->input_type) $this->get_type();
if (!$this->output_type) $this->output_type = $this->input_type;
if ($this->input_type == "jpg") $this->input_type = "jpeg";
if ($this->output_type == "jpg") $this->output_type = "jpeg";
switch ($this->input_type){
case 'gif':
$src_img=ImageCreateFromGIF($this->filename);
break;
case 'jpeg':
$src_img=ImageCreateFromJPEG($this->filename);
break;
case 'png':
$src_img=ImageCreateFromPNG($this->filename);
break;
default:
$src_img=ImageCreateFromString($this->filename);
break;
}
$src_w=ImageSX($src_img);
$src_h=ImageSY($src_img);
if ($src_w>=$src_h){
if ($src_w>$this->limit){
$new_w=$this->limit;
$new_h=($this->limit / $src_w)*$src_h;
}
}
else{
if ($src_h>$this->limit){
$new_h=$this->limit;
$new_w=($this->limit / $src_h)*$src_w;
}
}
if ($new_h){
$dst_img=imagecreatetruecolor($new_w,$new_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
}
else{
$dst_img = $src_img;
}
if ($this->wm_text)
{
if(preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_color, $color))
{
$red = hexdec($color[1]);
$green = hexdec($color[2]);
$blue = hexdec($color[3]);
}
$wm_color = imagecolorallocatealpha($dst_img, $red, $green, $blue, 90);
imagettftext($dst_img, $this->wm_size, $this->wm_angle, $this->wm_x, $this->wm_y, $wm_color, $this->wm_fontfile, $this->wm_text);
}
if ($this->save_file)
{
switch ($this->output_type){
case 'gif':
$src_img=ImagePNG($dst_img, $this->save_file);
break;
case 'jpeg':
$src_img=ImageJPEG($dst_img, $this->save_file, $this->jpeg_quality);
break;
case 'png':
$src_img=ImagePNG($dst_img, $this->save_file);
break;
default:
$src_img=ImageJPEG($dst_img, $this->save_file, $this->jpeg_quality);
break;
}
}
else
{
header("Content-type: image/{$this->output_type}");
switch ($this->output_type){
case 'gif':
$src_img=ImagePNG($dst_img);
break;
case 'jpeg':
$src_img=ImageJPEG($dst_img, "", $this->jpeg_quality);
break;
case 'png':
$src_img=ImagePNG($dst_img);
break;
default:
$src_img=ImageJPEG($dst_img, "", $this->jpeg_quality);
break;
}
}
imagedestroy($dst_img);
}
function get_type()//获取图像文件类型
{
$name_array = explode("。
",$this->filename);
if (preg_match("/\。
(jpg|jpeg|gif|png)$/", $this->filename, $matches))
{
$this->input_type = strtolower($matches[1]);
}
else
{
$this->input_type = "string";
}
}
}
?>。收起