private void btnOpenFile_Click(object sender, System.EventArgs e)
{
//Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c://" ;
openFileDialog1.Filter= "All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
originalimage = System.Drawing.Image.FromFile(openFileDialog1.FileName.ToString());
Image ithumbnail = originalimage.GetThumbnailImage(200, 200, null, new IntPtr());
pictureBox1.Image=ithumbnail;
btnAddCopyright.Enabled = true;
}
}
private void btnAddCopyright_Click(object sender, System.EventArgs e)
{
int imagewidth;
int imageheight;
int fontsize=300;
int x,y;
int a,re,gr,bl,x1,y1,z1;
int size;
Bitmap pattern;
SizeF sizeofstring;
bool foundfont;
imagewidth=originalimage.Width;
imageheight=originalimage.Height;
size=imagewidth*imageheight;
pattern = new Bitmap(imagewidth,imageheight);
Bitmap temp = new Bitmap(originalimage);
Graphics g = Graphics.FromImage(pattern);
Graphics tempg =Graphics.FromImage(originalimage);
//find a font size that will fit in the bitmap
foundfont=false;
g.Clear(Color.White);
while(foundfont==false)
{
Font fc = new Font("Georgia", fontsize, System.Drawing.FontStyle.Bold);
sizeofstring=new SizeF(imagewidth,imageheight);
sizeofstring=g.MeasureString("DOTNET",fc);
if (sizeofstring.Width<pattern.Width)
{
if (sizeofstring.Height<pattern.Height)
{
foundfont=true;
g.DrawString("DOTNET", fc, new SolidBrush(Color.Black),1,1);
}
}
else
fontsize=fontsize-1;
}
MessageBox.Show("已创建新文件","给图像添加版权信息");
for(x=1;x<pattern.Width;x++)
{
for(y=1;y<pattern.Height;y++)//
{
if (pattern.GetPixel(x,y).ToArgb()==Color.Black.ToArgb())
{
a=temp.GetPixel(x,y).A;
re=temp.GetPixel(x,y).R;
gr=temp.GetPixel(x,y).G;
bl=temp.GetPixel(x,y).B;
x1=re;
y1=gr;
z1=bl;
if (bl+25<255)
bl=bl+25;
if (gr+25<255)
gr=gr+25;
if (re+25<255)
re=re+25;
if (x1-25>0)
x1=x1-25;
if (y1-25>0)
y1=y1-25;
if (z1-25>0)
z1=z1-25;
tempg.DrawEllipse(new Pen(new SolidBrush(Color.Black)),x,y+1,3,3);
tempg.DrawEllipse(new Pen(new SolidBrush(Color.FromArgb(a,x1,y1,z1))),x,y,1,1);
}
}
}
MessageBox.Show("输出文件是c://output.jpeg","给图像添加版权信息");
tempg.Save();
originalimage.Save("c://output.jpeg",ImageFormat.Jpeg);
}
}