1、水印是半透明的一张小LOGO,如何加到任何图片上去,看到的效果还是透明的?
2、水印在图片的定位问题,因为每张图片尺寸大小不一样,所以如何做到水印居中或者在离图片的左下脚相等距离。
3、图片的后缀格式有很多种,比如.jpg,.gif,.png,.bmp 都要支持这些格式。
18 个解决方案
#1
可以采用水印的添加效果如下:
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(".") + "/Alex.gif");
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);
}
}
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(".") + "/Alex.gif");
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);
}
}
#2
刚写的,
这里输出格式我固定用了jpeg的,实际上根据源文件的格式来输出。
这里输出格式我固定用了jpeg的,实际上根据源文件的格式来输出。
System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath("~/dd.jpg"));
Graphics g = Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
System.Drawing.Image imageLogo = System.Drawing.Image.FromFile(Server.MapPath("~/logo.png"));
// Graphics g1 = Graphics.FromImage(imageLogo);
//假如放中间。
int nLeft = Math.Max(0,(image.Width-imageLogo.Width)/2);
int nTop = Math.Max(0, (image.Height - imageLogo.Height) / 2);
g.DrawImage(imageLogo, new Rectangle(nLeft, nTop, imageLogo.Width, imageLogo.Height));
imageLogo.Dispose();
image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
image.Dispose();
#3
我先测试一下,对了就马上给分
#4
public class picmark
{
private string modifyImagePath = null;
private string drawedImagePath = null;
private int rightSpace;
private int bottoamSpace;
private int lucencyPercent = 70;
private string outPath = null;
public picmark()
{
}
public string ModifyImagePath
{
get { return this.modifyImagePath; }
set { this.modifyImagePath = value; }
}
public string DrawedImagePath
{
get { return this.drawedImagePath; }
set { this.drawedImagePath = value; }
}
public int RightSpace
{
get { return this.rightSpace; }
set { this.rightSpace = value; }
}
public int BottoamSpace
{
get { return this.bottoamSpace; }
set { this.bottoamSpace = value; }
}
public int LucencyPercent
{
get { return this.lucencyPercent; }
set
{
if (value >= 0 && value <= 100)
this.lucencyPercent = value;
}
}
public string OutPath
{
get { return this.outPath; }
set { this.outPath = value; }
}
public void DrawImage()
{
Image modifyImage = null;
Image drawedImage = null;
Graphics g = null;
try
{
modifyImage = Image.FromFile(this.ModifyImagePath);
drawedImage = Image.FromFile(this.DrawedImagePath);
g = Graphics.FromImage(modifyImage);
int x = modifyImage.Width - this.rightSpace;
int y = modifyImage.Height - this.BottoamSpace;
float[][] matrixItems ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, (float)this.LucencyPercent/100f, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
ImageAttributes imgAttr = new ImageAttributes();
imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(
drawedImage,
new Rectangle(x, y, drawedImage.Width, drawedImage.Height),
0, 0, drawedImage.Width, drawedImage.Height,
GraphicsUnit.Pixel, imgAttr);
string[] allowImageType ={ ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
FileInfo file = new FileInfo(this.ModifyImagePath);
ImageFormat imageType = ImageFormat.Gif;
switch (file.Extension.ToLower())
{
case ".jpg":
imageType = ImageFormat.Jpeg;
break;
case ".gif":
imageType = ImageFormat.Gif;
break;
case ".png":
imageType = ImageFormat.Png;
break;
case ".bmp":
imageType = ImageFormat.Bmp;
break;
case ".tif":
imageType = ImageFormat.Tiff;
break;
case ".wmf":
imageType = ImageFormat.Wmf;
break;
case ".ico":
imageType = ImageFormat.Icon;
break;
default:
break;
}
MemoryStream ms = new MemoryStream();
modifyImage.Save(ms, imageType);
byte[] imgData = ms.ToArray();
modifyImage.Dispose();
drawedImage.Dispose();
g.Dispose();
FileStream fs = null;
if (this.OutPath == null || this.OutPath == "")
{
File.Delete(this.ModifyImagePath);
fs = new FileStream(this.ModifyImagePath, FileMode.Create, FileAccess.Write);
}
else
{
fs = new FileStream(this.OutPath, FileMode.Create, FileAccess.Write);
}
if (fs != null)
{
fs.Write(imgData, 0, imgData.Length);
fs.Close();
}
}
finally
{
try
{
drawedImage.Dispose();
modifyImage.Dispose();
g.Dispose();
}
catch { ;}
}
}
http://topic.csdn.net/u/20090105/15/b6da3548-d384-403f-9d60-6eb17d1b0ec4.html
{
private string modifyImagePath = null;
private string drawedImagePath = null;
private int rightSpace;
private int bottoamSpace;
private int lucencyPercent = 70;
private string outPath = null;
public picmark()
{
}
public string ModifyImagePath
{
get { return this.modifyImagePath; }
set { this.modifyImagePath = value; }
}
public string DrawedImagePath
{
get { return this.drawedImagePath; }
set { this.drawedImagePath = value; }
}
public int RightSpace
{
get { return this.rightSpace; }
set { this.rightSpace = value; }
}
public int BottoamSpace
{
get { return this.bottoamSpace; }
set { this.bottoamSpace = value; }
}
public int LucencyPercent
{
get { return this.lucencyPercent; }
set
{
if (value >= 0 && value <= 100)
this.lucencyPercent = value;
}
}
public string OutPath
{
get { return this.outPath; }
set { this.outPath = value; }
}
public void DrawImage()
{
Image modifyImage = null;
Image drawedImage = null;
Graphics g = null;
try
{
modifyImage = Image.FromFile(this.ModifyImagePath);
drawedImage = Image.FromFile(this.DrawedImagePath);
g = Graphics.FromImage(modifyImage);
int x = modifyImage.Width - this.rightSpace;
int y = modifyImage.Height - this.BottoamSpace;
float[][] matrixItems ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, (float)this.LucencyPercent/100f, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
ImageAttributes imgAttr = new ImageAttributes();
imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(
drawedImage,
new Rectangle(x, y, drawedImage.Width, drawedImage.Height),
0, 0, drawedImage.Width, drawedImage.Height,
GraphicsUnit.Pixel, imgAttr);
string[] allowImageType ={ ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
FileInfo file = new FileInfo(this.ModifyImagePath);
ImageFormat imageType = ImageFormat.Gif;
switch (file.Extension.ToLower())
{
case ".jpg":
imageType = ImageFormat.Jpeg;
break;
case ".gif":
imageType = ImageFormat.Gif;
break;
case ".png":
imageType = ImageFormat.Png;
break;
case ".bmp":
imageType = ImageFormat.Bmp;
break;
case ".tif":
imageType = ImageFormat.Tiff;
break;
case ".wmf":
imageType = ImageFormat.Wmf;
break;
case ".ico":
imageType = ImageFormat.Icon;
break;
default:
break;
}
MemoryStream ms = new MemoryStream();
modifyImage.Save(ms, imageType);
byte[] imgData = ms.ToArray();
modifyImage.Dispose();
drawedImage.Dispose();
g.Dispose();
FileStream fs = null;
if (this.OutPath == null || this.OutPath == "")
{
File.Delete(this.ModifyImagePath);
fs = new FileStream(this.ModifyImagePath, FileMode.Create, FileAccess.Write);
}
else
{
fs = new FileStream(this.OutPath, FileMode.Create, FileAccess.Write);
}
if (fs != null)
{
fs.Write(imgData, 0, imgData.Length);
fs.Close();
}
}
finally
{
try
{
drawedImage.Dispose();
modifyImage.Dispose();
g.Dispose();
}
catch { ;}
}
}
http://topic.csdn.net/u/20090105/15/b6da3548-d384-403f-9d60-6eb17d1b0ec4.html
#5
g.DrawString(addText, f, b, 10, 10);
这个定位,好像只能是相对于图片的右上角来说的,要是相对于左下角怎么写呢?
这个定位,好像只能是相对于图片的右上角来说的,要是相对于左下角怎么写呢?
#6
这个
是左上角的。
是左上角的。
#7
================
我写错了,应该是
这个定位,好像只能是相对于图片的左上角来说的,要是相对于右下角怎么写呢?
#8
楼主,我blog里面有个向光文章
#9
这个定位,好像只能是相对于图片的左上角来说的,要是相对于右下角怎么写呢?
==>
做个简单的计算就可以了。根据背景的宽度,高度。
如果距右下为10,20,那么这个点的位置就是
width-水印宽度-10,height-水印高度-
20
==>
做个简单的计算就可以了。根据背景的宽度,高度。
如果距右下为10,20,那么这个点的位置就是
width-水印宽度-10,height-水印高度-
20
#11
这里代码多多,我就不贴代码了,楼主主要的问题是在第2个问题,由于尺寸不一样,且要求水印中或者在离图片的左下脚相等距离。
这个就需要再加个判断(居中还是左下角等距),以及图片和水印图片的尺寸的比较,要对水印图片做可能的缩放操作之后再来定位操作。
#12
LZ可以单独写个方法,计算水印位于图片的某个位置。
嘿嘿,我也有问题,顺便在这里问一下:
1.设置字体透明,怎么设置?也是这样设置么?
如果这样设置的话,在把文字水印写到图片上的方法中好像没有【设置是否透明】这个参数的啊?
2.我要给GIF(动态)图片加图片和文字水印,怎么设置,才能够加好水印后,图片还是动态的?
switch (this.comboBox2.SelectedIndex)
{
case 0://图片正中间
ScrX = image.Width / 2 - (int)newSize.Width / 2;
ScrY = image.Height / 2 - (int)newSize.Height / 2 ;
break;
case 1://图片左上角 适当的对ScrX、ScrY设置初始值
ScrX = 10;
ScrY = 20;
break;
case 2://图片右上角
ScrX = image.Width - (int)newSize.Width - 10;
ScrY = 20;
break;
case 3://图片左下角
ScrX = 10;
ScrY = image.Height - (int)newSize.Height - 20;
break;
case 4://图片右下脚
ScrX = image.Width - (int)newSize.Width - 10;
ScrY = image.Height - (int)newSize.Height - 20;
break;
case 5://自定义
ScrX = texScrX;
ScrY = texScrY;
break;
default://图片正中间
ScrX = image.Width / 2 - (int)newSize.Width / 2;
ScrY = image.Height / 2 - (int)newSize.Height / 2;
break;
}
嘿嘿,我也有问题,顺便在这里问一下:
1.设置字体透明,怎么设置?也是这样设置么?
如果这样设置的话,在把文字水印写到图片上的方法中好像没有【设置是否透明】这个参数的啊?
float[][] matrixItems ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, (float)this.LucencyPercent/100f, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
2.我要给GIF(动态)图片加图片和文字水印,怎么设置,才能够加好水印后,图片还是动态的?
#13
这个我的BLOG里有比较详细的介绍
#14
好东西找好久了 \(^o^)/~
#15
谢谢大家。结帖了。
#17
#18
踩一踩
#1
可以采用水印的添加效果如下:
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(".") + "/Alex.gif");
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);
}
}
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(".") + "/Alex.gif");
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);
}
}
#2
刚写的,
这里输出格式我固定用了jpeg的,实际上根据源文件的格式来输出。
这里输出格式我固定用了jpeg的,实际上根据源文件的格式来输出。
System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath("~/dd.jpg"));
Graphics g = Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
System.Drawing.Image imageLogo = System.Drawing.Image.FromFile(Server.MapPath("~/logo.png"));
// Graphics g1 = Graphics.FromImage(imageLogo);
//假如放中间。
int nLeft = Math.Max(0,(image.Width-imageLogo.Width)/2);
int nTop = Math.Max(0, (image.Height - imageLogo.Height) / 2);
g.DrawImage(imageLogo, new Rectangle(nLeft, nTop, imageLogo.Width, imageLogo.Height));
imageLogo.Dispose();
image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
image.Dispose();
#3
我先测试一下,对了就马上给分
#4
public class picmark
{
private string modifyImagePath = null;
private string drawedImagePath = null;
private int rightSpace;
private int bottoamSpace;
private int lucencyPercent = 70;
private string outPath = null;
public picmark()
{
}
public string ModifyImagePath
{
get { return this.modifyImagePath; }
set { this.modifyImagePath = value; }
}
public string DrawedImagePath
{
get { return this.drawedImagePath; }
set { this.drawedImagePath = value; }
}
public int RightSpace
{
get { return this.rightSpace; }
set { this.rightSpace = value; }
}
public int BottoamSpace
{
get { return this.bottoamSpace; }
set { this.bottoamSpace = value; }
}
public int LucencyPercent
{
get { return this.lucencyPercent; }
set
{
if (value >= 0 && value <= 100)
this.lucencyPercent = value;
}
}
public string OutPath
{
get { return this.outPath; }
set { this.outPath = value; }
}
public void DrawImage()
{
Image modifyImage = null;
Image drawedImage = null;
Graphics g = null;
try
{
modifyImage = Image.FromFile(this.ModifyImagePath);
drawedImage = Image.FromFile(this.DrawedImagePath);
g = Graphics.FromImage(modifyImage);
int x = modifyImage.Width - this.rightSpace;
int y = modifyImage.Height - this.BottoamSpace;
float[][] matrixItems ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, (float)this.LucencyPercent/100f, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
ImageAttributes imgAttr = new ImageAttributes();
imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(
drawedImage,
new Rectangle(x, y, drawedImage.Width, drawedImage.Height),
0, 0, drawedImage.Width, drawedImage.Height,
GraphicsUnit.Pixel, imgAttr);
string[] allowImageType ={ ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
FileInfo file = new FileInfo(this.ModifyImagePath);
ImageFormat imageType = ImageFormat.Gif;
switch (file.Extension.ToLower())
{
case ".jpg":
imageType = ImageFormat.Jpeg;
break;
case ".gif":
imageType = ImageFormat.Gif;
break;
case ".png":
imageType = ImageFormat.Png;
break;
case ".bmp":
imageType = ImageFormat.Bmp;
break;
case ".tif":
imageType = ImageFormat.Tiff;
break;
case ".wmf":
imageType = ImageFormat.Wmf;
break;
case ".ico":
imageType = ImageFormat.Icon;
break;
default:
break;
}
MemoryStream ms = new MemoryStream();
modifyImage.Save(ms, imageType);
byte[] imgData = ms.ToArray();
modifyImage.Dispose();
drawedImage.Dispose();
g.Dispose();
FileStream fs = null;
if (this.OutPath == null || this.OutPath == "")
{
File.Delete(this.ModifyImagePath);
fs = new FileStream(this.ModifyImagePath, FileMode.Create, FileAccess.Write);
}
else
{
fs = new FileStream(this.OutPath, FileMode.Create, FileAccess.Write);
}
if (fs != null)
{
fs.Write(imgData, 0, imgData.Length);
fs.Close();
}
}
finally
{
try
{
drawedImage.Dispose();
modifyImage.Dispose();
g.Dispose();
}
catch { ;}
}
}
http://topic.csdn.net/u/20090105/15/b6da3548-d384-403f-9d60-6eb17d1b0ec4.html
{
private string modifyImagePath = null;
private string drawedImagePath = null;
private int rightSpace;
private int bottoamSpace;
private int lucencyPercent = 70;
private string outPath = null;
public picmark()
{
}
public string ModifyImagePath
{
get { return this.modifyImagePath; }
set { this.modifyImagePath = value; }
}
public string DrawedImagePath
{
get { return this.drawedImagePath; }
set { this.drawedImagePath = value; }
}
public int RightSpace
{
get { return this.rightSpace; }
set { this.rightSpace = value; }
}
public int BottoamSpace
{
get { return this.bottoamSpace; }
set { this.bottoamSpace = value; }
}
public int LucencyPercent
{
get { return this.lucencyPercent; }
set
{
if (value >= 0 && value <= 100)
this.lucencyPercent = value;
}
}
public string OutPath
{
get { return this.outPath; }
set { this.outPath = value; }
}
public void DrawImage()
{
Image modifyImage = null;
Image drawedImage = null;
Graphics g = null;
try
{
modifyImage = Image.FromFile(this.ModifyImagePath);
drawedImage = Image.FromFile(this.DrawedImagePath);
g = Graphics.FromImage(modifyImage);
int x = modifyImage.Width - this.rightSpace;
int y = modifyImage.Height - this.BottoamSpace;
float[][] matrixItems ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, (float)this.LucencyPercent/100f, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
ImageAttributes imgAttr = new ImageAttributes();
imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(
drawedImage,
new Rectangle(x, y, drawedImage.Width, drawedImage.Height),
0, 0, drawedImage.Width, drawedImage.Height,
GraphicsUnit.Pixel, imgAttr);
string[] allowImageType ={ ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
FileInfo file = new FileInfo(this.ModifyImagePath);
ImageFormat imageType = ImageFormat.Gif;
switch (file.Extension.ToLower())
{
case ".jpg":
imageType = ImageFormat.Jpeg;
break;
case ".gif":
imageType = ImageFormat.Gif;
break;
case ".png":
imageType = ImageFormat.Png;
break;
case ".bmp":
imageType = ImageFormat.Bmp;
break;
case ".tif":
imageType = ImageFormat.Tiff;
break;
case ".wmf":
imageType = ImageFormat.Wmf;
break;
case ".ico":
imageType = ImageFormat.Icon;
break;
default:
break;
}
MemoryStream ms = new MemoryStream();
modifyImage.Save(ms, imageType);
byte[] imgData = ms.ToArray();
modifyImage.Dispose();
drawedImage.Dispose();
g.Dispose();
FileStream fs = null;
if (this.OutPath == null || this.OutPath == "")
{
File.Delete(this.ModifyImagePath);
fs = new FileStream(this.ModifyImagePath, FileMode.Create, FileAccess.Write);
}
else
{
fs = new FileStream(this.OutPath, FileMode.Create, FileAccess.Write);
}
if (fs != null)
{
fs.Write(imgData, 0, imgData.Length);
fs.Close();
}
}
finally
{
try
{
drawedImage.Dispose();
modifyImage.Dispose();
g.Dispose();
}
catch { ;}
}
}
http://topic.csdn.net/u/20090105/15/b6da3548-d384-403f-9d60-6eb17d1b0ec4.html
#5
g.DrawString(addText, f, b, 10, 10);
这个定位,好像只能是相对于图片的右上角来说的,要是相对于左下角怎么写呢?
这个定位,好像只能是相对于图片的右上角来说的,要是相对于左下角怎么写呢?
#6
这个
是左上角的。
是左上角的。
#7
================
我写错了,应该是
这个定位,好像只能是相对于图片的左上角来说的,要是相对于右下角怎么写呢?
#8
楼主,我blog里面有个向光文章
#9
这个定位,好像只能是相对于图片的左上角来说的,要是相对于右下角怎么写呢?
==>
做个简单的计算就可以了。根据背景的宽度,高度。
如果距右下为10,20,那么这个点的位置就是
width-水印宽度-10,height-水印高度-
20
==>
做个简单的计算就可以了。根据背景的宽度,高度。
如果距右下为10,20,那么这个点的位置就是
width-水印宽度-10,height-水印高度-
20
#10
#11
这里代码多多,我就不贴代码了,楼主主要的问题是在第2个问题,由于尺寸不一样,且要求水印中或者在离图片的左下脚相等距离。
这个就需要再加个判断(居中还是左下角等距),以及图片和水印图片的尺寸的比较,要对水印图片做可能的缩放操作之后再来定位操作。
#12
LZ可以单独写个方法,计算水印位于图片的某个位置。
嘿嘿,我也有问题,顺便在这里问一下:
1.设置字体透明,怎么设置?也是这样设置么?
如果这样设置的话,在把文字水印写到图片上的方法中好像没有【设置是否透明】这个参数的啊?
2.我要给GIF(动态)图片加图片和文字水印,怎么设置,才能够加好水印后,图片还是动态的?
switch (this.comboBox2.SelectedIndex)
{
case 0://图片正中间
ScrX = image.Width / 2 - (int)newSize.Width / 2;
ScrY = image.Height / 2 - (int)newSize.Height / 2 ;
break;
case 1://图片左上角 适当的对ScrX、ScrY设置初始值
ScrX = 10;
ScrY = 20;
break;
case 2://图片右上角
ScrX = image.Width - (int)newSize.Width - 10;
ScrY = 20;
break;
case 3://图片左下角
ScrX = 10;
ScrY = image.Height - (int)newSize.Height - 20;
break;
case 4://图片右下脚
ScrX = image.Width - (int)newSize.Width - 10;
ScrY = image.Height - (int)newSize.Height - 20;
break;
case 5://自定义
ScrX = texScrX;
ScrY = texScrY;
break;
default://图片正中间
ScrX = image.Width / 2 - (int)newSize.Width / 2;
ScrY = image.Height / 2 - (int)newSize.Height / 2;
break;
}
嘿嘿,我也有问题,顺便在这里问一下:
1.设置字体透明,怎么设置?也是这样设置么?
如果这样设置的话,在把文字水印写到图片上的方法中好像没有【设置是否透明】这个参数的啊?
float[][] matrixItems ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, (float)this.LucencyPercent/100f, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
2.我要给GIF(动态)图片加图片和文字水印,怎么设置,才能够加好水印后,图片还是动态的?
#13
这个我的BLOG里有比较详细的介绍
#14
好东西找好久了 \(^o^)/~
#15
谢谢大家。结帖了。
#16
#17
#18
踩一踩