在某些情况下希望处理一些图片,比如
给图片添加一般文字:
原图
![banner.jpg](https://i-blog.csdnimg.cn/blog_migrate/07115fe700613081cb94afbfede3da42.jpeg)
程序处理成
![249banner.jpg](https://i-blog.csdnimg.cn/blog_migrate/fdc4dfa1cce284f46ecf2b8f9c0707c3.jpeg)
给图片添加水印:
原图
![photo.jpg](https://i-blog.csdnimg.cn/blog_migrate/7bd1f1d0cc7091321dfc10d5c8362639.jpeg)
程序处理成
![2414photo.jpg](https://i-blog.csdnimg.cn/blog_migrate/40729cc3cc6aa167285b0f819391c807.jpeg)
还有圆角效果:
原图
![image.jpg](https://i-blog.csdnimg.cn/blog_migrate/a1ff82764f46e08374189ccf0c4e8317.jpeg)
程序处理成
![2419image.jpg](https://i-blog.csdnimg.cn/blog_migrate/d2370b986270f85bcafb313d55bd2dfe.jpeg)
注:
(1)以下程序针对这个应用,也许这些对你没有用,但是基本的原理都差不多
(2)以上图为测试用,应该不算侵犯肖像权吧
程序界面:
![56756761.jpg](https://i-blog.csdnimg.cn/blog_migrate/6e8547578f149a6c5d3294288eef90b2.jpeg)
程序点击这里下载。
如果需要提高图片质量,把
image.Save(sDstFilePath,ImageFormat.Jpeg);
改成
附一些关键代码:
publicclassMyGDI
{
publicstaticvoidCreateWatermark(stringsSrcFilePath,stringsDstFilePath,stringsText1,stringsColor1,stringsSize1,stringsFont1,stringsText2,stringsColor2,stringsSize2,stringsFont2,stringsBgColor,stringsTransparence)
{
System.Drawing.Imageimage=System.Drawing.Image.FromFile(sSrcFilePath);
Graphicsg=Graphics.FromImage(image);
g.SmoothingMode=SmoothingMode.AntiAlias;
g.InterpolationMode=InterpolationMode.HighQualityBicubic;
g.CompositingQuality=CompositingQuality.HighQuality;
g.TextRenderingHint=System.Drawing.Text.TextRenderingHint.AntiAlias;//文字抗锯齿
g.DrawImage(image,0,0,image.Width,image.Height);
Fontf1=newFont(sFont1,float.Parse(sSize1));
Fontf2=newFont(sFont2,float.Parse(sSize2));
Brushbrushfortext1=newSolidBrush(ColorTranslator.FromHtml(sColor1));
Brushbrushfortext2=newSolidBrush(ColorTranslator.FromHtml(sColor2));
Brushbrushforbg=newSolidBrush(Color.FromArgb(Convert.ToInt16(255*float.Parse(sTransparence)),ColorTranslator.FromHtml(sBgColor)));
g.RotateTransform(-20);
Rectanglerect=newRectangle(-image.Width/2-50,image.Height-50,image.Width*2,40);
g.DrawRectangle(newPen(brushforbg),rect);
g.FillRectangle(brushforbg,rect);
Rectanglerectfortext1=newRectangle(-image.Width/2+image.Width/5,image.Height-45,image.Width*2,60);
for(inti=0;i<10;i++)
g.DrawString(sText1,f1,brushfortext1,rectfortext1);
Rectanglerectfortext2=newRectangle(-image.Width/2+image.Width/5,image.Height-25,image.Width*2,60);
for(inti=0;i<10;i++)
g.DrawString(sText2,f2,brushfortext2,rectfortext2);
image.Save(sDstFilePath,ImageFormat.Jpeg);
image.Dispose();
}
publicstaticvoidCreateRoundedCorner(stringsSrcFilePath,stringsDstFilePath,stringsCornerLocation)
{
System.Drawing.Imageimage=System.Drawing.Image.FromFile(sSrcFilePath);
Graphicsg=Graphics.FromImage(image);
g.SmoothingMode=SmoothingMode.HighQuality;
g.InterpolationMode=InterpolationMode.HighQualityBicubic;
g.CompositingQuality=CompositingQuality.HighQuality;
Rectanglerect=newRectangle(0,0,image.Width,image.Height);
GraphicsPathrectPath=CreateRoundRectanglePath(rect,image.Width/10,sCornerLocation);//构建圆角外部路径
Brushb=newSolidBrush(Color.White);//圆角背景白色
g.DrawPath(newPen(b),rectPath);
g.FillPath(b,rectPath);
g.Dispose();
image.Save(sDstFilePath,ImageFormat.Jpeg);
image.Dispose();
}
publicstaticvoidCreatePlainText(stringsSrcFilePath,stringsDstFilePath,stringsText,stringsColor,stringsSize,stringsFont)
{
System.Drawing.Imageimage=System.Drawing.Image.FromFile(sSrcFilePath);
Graphicsg=Graphics.FromImage(image);
g.SmoothingMode=SmoothingMode.AntiAlias;
g.InterpolationMode=InterpolationMode.HighQualityBicubic;
g.CompositingQuality=CompositingQuality.HighQuality;
g.DrawImage(image,0,0,image.Width,image.Height);
g.TextRenderingHint=System.Drawing.Text.TextRenderingHint.AntiAlias;//文字抗锯齿
Fontf=newFont(sFont,float.Parse(sSize));
Brushb=newSolidBrush(ColorTranslator.FromHtml(sColor));
Rectanglerect=newRectangle(10,5,image.Width,image.Height);//适当空开一段距离
for(inti=0;i<30;i++)//加强亮度
g.DrawString(sText,f,b,rect);
image.Save(sDstFilePath,ImageFormat.Jpeg);
image.Dispose();
}
privatestaticGraphicsPathCreateRoundRectanglePath(Rectanglerect,intradius,stringsPosition)
{
GraphicsPathrectPath=newGraphicsPath();
switch(sPosition)
{
case"TopLeft":
{
rectPath.AddArc(rect.Left,rect.Top,radius*2,radius*2,180,90);
rectPath.AddLine(rect.Left,rect.Top,rect.Left,rect.Top+radius);
break;
}
case"TopRight":
{
rectPath.AddArc(rect.Right-radius*2,rect.Top,radius*2,radius*2,270,90);
rectPath.AddLine(rect.Right,rect.Top,rect.Right-radius,rect.Top);
break;
}
case"BottomLeft":
{
rectPath.AddArc(rect.Left,rect.Bottom-radius*2,radius*2,radius*2,90,90);
rectPath.AddLine(rect.Left,rect.Bottom-radius,rect.Left,rect.Bottom);
break;
}
case"BottomRight":
{
rectPath.AddArc(rect.Right-radius*2,rect.Bottom-radius*2,radius*2,radius*2,0,90);
rectPath.AddLine(rect.Right-radius,rect.Bottom,rect.Right,rect.Bottom);
break;
}
}
returnrectPath;
}
}
给图片添加一般文字:
原图
![banner.jpg](https://i-blog.csdnimg.cn/blog_migrate/07115fe700613081cb94afbfede3da42.jpeg)
程序处理成
![249banner.jpg](https://i-blog.csdnimg.cn/blog_migrate/fdc4dfa1cce284f46ecf2b8f9c0707c3.jpeg)
给图片添加水印:
原图
![photo.jpg](https://i-blog.csdnimg.cn/blog_migrate/7bd1f1d0cc7091321dfc10d5c8362639.jpeg)
程序处理成
![2414photo.jpg](https://i-blog.csdnimg.cn/blog_migrate/40729cc3cc6aa167285b0f819391c807.jpeg)
还有圆角效果:
原图
![image.jpg](https://i-blog.csdnimg.cn/blog_migrate/a1ff82764f46e08374189ccf0c4e8317.jpeg)
程序处理成
![2419image.jpg](https://i-blog.csdnimg.cn/blog_migrate/d2370b986270f85bcafb313d55bd2dfe.jpeg)
注:
(1)以下程序针对这个应用,也许这些对你没有用,但是基本的原理都差不多
(2)以上图为测试用,应该不算侵犯肖像权吧
程序界面:
![56756761.jpg](https://i-blog.csdnimg.cn/blog_migrate/6e8547578f149a6c5d3294288eef90b2.jpeg)
程序点击这里下载。
如果需要提高图片质量,把
image.Save(sDstFilePath,ImageFormat.Jpeg);
改成
ImageCodecInfomyImageCodecInfo;
EncodermyEncoder;
EncoderParametermyEncoderParameter;
EncoderParametersmyEncoderParameters;
myImageCodecInfo = ImageCodecInfo.GetImageEncoders()[ 0 ];
myEncoder = Encoder.Quality;
myEncoderParameters = new EncoderParameters( 1 );
myEncoderParameter = new EncoderParameter(myEncoder,1 00L ); // 0-100
myEncoderParameters.Param[ 0 ] = myEncoderParameter;
image.Save(sDstFilePath,myImageCodecInfo,myEncoderParameters);
myEncoderParameter.Dispose();
myEncoderParameters.Dispose();
这是提高质量后的效果:EncodermyEncoder;
EncoderParametermyEncoderParameter;
EncoderParametersmyEncoderParameters;
myImageCodecInfo = ImageCodecInfo.GetImageEncoders()[ 0 ];
myEncoder = Encoder.Quality;
myEncoderParameters = new EncoderParameters( 1 );
myEncoderParameter = new EncoderParameter(myEncoder,1 00L ); // 0-100
myEncoderParameters.Param[ 0 ] = myEncoderParameter;
image.Save(sDstFilePath,myImageCodecInfo,myEncoderParameters);
myEncoderParameter.Dispose();
myEncoderParameters.Dispose();
![1243photo.jpg](https://i-blog.csdnimg.cn/blog_migrate/962c0c4a51f0bb3b2597a16716940c8c.jpeg)
附一些关键代码:
publicclassMyGDI
{
publicstaticvoidCreateWatermark(stringsSrcFilePath,stringsDstFilePath,stringsText1,stringsColor1,stringsSize1,stringsFont1,stringsText2,stringsColor2,stringsSize2,stringsFont2,stringsBgColor,stringsTransparence)
{
System.Drawing.Imageimage=System.Drawing.Image.FromFile(sSrcFilePath);
Graphicsg=Graphics.FromImage(image);
g.SmoothingMode=SmoothingMode.AntiAlias;
g.InterpolationMode=InterpolationMode.HighQualityBicubic;
g.CompositingQuality=CompositingQuality.HighQuality;
g.TextRenderingHint=System.Drawing.Text.TextRenderingHint.AntiAlias;//文字抗锯齿
g.DrawImage(image,0,0,image.Width,image.Height);
Fontf1=newFont(sFont1,float.Parse(sSize1));
Fontf2=newFont(sFont2,float.Parse(sSize2));
Brushbrushfortext1=newSolidBrush(ColorTranslator.FromHtml(sColor1));
Brushbrushfortext2=newSolidBrush(ColorTranslator.FromHtml(sColor2));
Brushbrushforbg=newSolidBrush(Color.FromArgb(Convert.ToInt16(255*float.Parse(sTransparence)),ColorTranslator.FromHtml(sBgColor)));
g.RotateTransform(-20);
Rectanglerect=newRectangle(-image.Width/2-50,image.Height-50,image.Width*2,40);
g.DrawRectangle(newPen(brushforbg),rect);
g.FillRectangle(brushforbg,rect);
Rectanglerectfortext1=newRectangle(-image.Width/2+image.Width/5,image.Height-45,image.Width*2,60);
for(inti=0;i<10;i++)
g.DrawString(sText1,f1,brushfortext1,rectfortext1);
Rectanglerectfortext2=newRectangle(-image.Width/2+image.Width/5,image.Height-25,image.Width*2,60);
for(inti=0;i<10;i++)
g.DrawString(sText2,f2,brushfortext2,rectfortext2);
image.Save(sDstFilePath,ImageFormat.Jpeg);
image.Dispose();
}
publicstaticvoidCreateRoundedCorner(stringsSrcFilePath,stringsDstFilePath,stringsCornerLocation)
{
System.Drawing.Imageimage=System.Drawing.Image.FromFile(sSrcFilePath);
Graphicsg=Graphics.FromImage(image);
g.SmoothingMode=SmoothingMode.HighQuality;
g.InterpolationMode=InterpolationMode.HighQualityBicubic;
g.CompositingQuality=CompositingQuality.HighQuality;
Rectanglerect=newRectangle(0,0,image.Width,image.Height);
GraphicsPathrectPath=CreateRoundRectanglePath(rect,image.Width/10,sCornerLocation);//构建圆角外部路径
Brushb=newSolidBrush(Color.White);//圆角背景白色
g.DrawPath(newPen(b),rectPath);
g.FillPath(b,rectPath);
g.Dispose();
image.Save(sDstFilePath,ImageFormat.Jpeg);
image.Dispose();
}
publicstaticvoidCreatePlainText(stringsSrcFilePath,stringsDstFilePath,stringsText,stringsColor,stringsSize,stringsFont)
{
System.Drawing.Imageimage=System.Drawing.Image.FromFile(sSrcFilePath);
Graphicsg=Graphics.FromImage(image);
g.SmoothingMode=SmoothingMode.AntiAlias;
g.InterpolationMode=InterpolationMode.HighQualityBicubic;
g.CompositingQuality=CompositingQuality.HighQuality;
g.DrawImage(image,0,0,image.Width,image.Height);
g.TextRenderingHint=System.Drawing.Text.TextRenderingHint.AntiAlias;//文字抗锯齿
Fontf=newFont(sFont,float.Parse(sSize));
Brushb=newSolidBrush(ColorTranslator.FromHtml(sColor));
Rectanglerect=newRectangle(10,5,image.Width,image.Height);//适当空开一段距离
for(inti=0;i<30;i++)//加强亮度
g.DrawString(sText,f,b,rect);
image.Save(sDstFilePath,ImageFormat.Jpeg);
image.Dispose();
}
privatestaticGraphicsPathCreateRoundRectanglePath(Rectanglerect,intradius,stringsPosition)
{
GraphicsPathrectPath=newGraphicsPath();
switch(sPosition)
{
case"TopLeft":
{
rectPath.AddArc(rect.Left,rect.Top,radius*2,radius*2,180,90);
rectPath.AddLine(rect.Left,rect.Top,rect.Left,rect.Top+radius);
break;
}
case"TopRight":
{
rectPath.AddArc(rect.Right-radius*2,rect.Top,radius*2,radius*2,270,90);
rectPath.AddLine(rect.Right,rect.Top,rect.Right-radius,rect.Top);
break;
}
case"BottomLeft":
{
rectPath.AddArc(rect.Left,rect.Bottom-radius*2,radius*2,radius*2,90,90);
rectPath.AddLine(rect.Left,rect.Bottom-radius,rect.Left,rect.Bottom);
break;
}
case"BottomRight":
{
rectPath.AddArc(rect.Right-radius*2,rect.Bottom-radius*2,radius*2,radius*2,0,90);
rectPath.AddLine(rect.Right-radius,rect.Bottom,rect.Right,rect.Bottom);
break;
}
}
returnrectPath;
}
}